Link to home
Start Free TrialLog in
Avatar of TLN_CANADA
TLN_CANADAFlag for Afghanistan

asked on

Macro Question

I would like to change this macro around so that it fits the format of the list already made.

https://www.experts-exchange.com/questions/26435925/Macro-for-replying-to-Emails-in-Outlook.html?anchorAnswerId=33564683#a33564683

 I tried to fit it in but encountered some errors with it. Here it is:

Sub Artwork()
Dim para As Variant
Dim strArray() As String
Dim mai As Object
Dim strSearchFor() As Variant: strSearchFor = Array("> Email:", "> Shipping Company:", "> Tracking Number:", "> Estimated Arrival Date:", "> Contact:", "> Company Name:")
Dim strResults() As String
Dim elem As Integer
Const str2 As String = ""
Const str3 As String = ""
Const str4 As String = ""
   
    ReDim strResults(UBound(strSearchFor))
    If TypeName(Application.ActiveWindow) = "Explorer" Then
        Set mai = Application.ActiveExplorer.Selection.Item(1)
    ElseIf TypeName(Application.ActiveWindow) = "Inspector" Then
            Set mai = Application.ActiveInspector.CurrentItem
    Else
        Exit Sub
    End If
    With mai
        strArray = Split(.Body, vbCrLf)
        For Each para In strArray
            If para <> "" Then
                For elem = LBound(strSearchFor) To UBound(strSearchFor)
                    If LCase(Left(para, Len(strSearchFor(elem)))) = LCase(strSearchFor(elem)) Then strResults(elem) = Trim(Split(para, ":")(1))
                Next
            End If
        Next
    End With
    Set mai = Nothing
    If strResults(0) = "" Then Exit Sub
    ' Only proceed if we have an email address!
    Set mai = Application.CreateItem(olMailItem)
    With mai
        .To = strResults(0)
        .Subject = mai.Subject
        .Body = "Dear " & Split(strResults(4) & " ", " ")(0) & vbCrLf & vbCrLf & _
            "Please find attached a photo of the first unit our production has made of your order and kindly confirm it is to your satisfaction before we proceed. If you have any other questions with this order, do not hesitate to ask. " & vbCrLf & vbCrLf & _
            "Best regards," & vbCrLf & _
                   "Derek" & vbCrLf & vbCrLf & _
                   "Derek Sheahan | Sales Manager TLN Group| tlngroup.com|" & vbCrLf & _
                   "|derek@tlngroup.com | Phone: (1800) 385 8156 | Fax : (1888) 429 7748" & vbCrLf & _
                   "420-2906 West Broadway, Vancouver , BC, V6K 2G8, Canada"
        .Display
    End With

End Sub

I would like to change this to editone and also make one change to it so that it takes the attachments from the current mail selected and adds them to the new mail being created.


Many thanks,

Derek
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

If I follow then something like this?

Chris
Sub EditOne(mai as variant)
Dim para As Variant
Dim strArray() As String
Dim nuMai As Object
Dim strSearchFor() As Variant: strSearchFor = Array("> Email:", "> Shipping Company:", "> Tracking Number:", "> Estimated Arrival Date:", "> Contact:", "> Company Name:")
Dim strResults() As String
Dim elem As Integer
Const str2 As String = ""
Const str3 As String = ""
Const str4 As String = ""
    
    ReDim strResults(UBound(strSearchFor))
'    If TypeName(Application.ActiveWindow) = "Explorer" Then
'        Set mai = Application.ActiveExplorer.Selection.Item(1)
'    ElseIf TypeName(Application.ActiveWindow) = "Inspector" Then
'            Set mai = Application.ActiveInspector.CurrentItem
'    Else
'        Exit Sub
'    End If
    With mai
        strArray = Split(.Body, vbCrLf)
        For Each para In strArray
            If para <> "" Then
                For elem = LBound(strSearchFor) To UBound(strSearchFor)
                    If LCase(Left(para, Len(strSearchFor(elem)))) = LCase(strSearchFor(elem)) Then strResults(elem) = Trim(Split(para, ":")(1))
                Next
            End If
        Next
    End With
'    Set mai = Nothing
    If strResults(0) = "" Then Exit Sub
    ' Only proceed if we have an email address!
    Set numai = Application.CreateItem(olMailItem)
    With numai
    CopyAttachments maiOrig, numai
        .To = strResults(0)
        .Subject = mai.Subject
        .Body = "Dear " & Split(strResults(4) & " ", " ")(0) & vbCrLf & vbCrLf & _
            "Please find attached a photo of the first unit our production has made of your order and kindly confirm it is to your satisfaction before we proceed. If you have any other questions with this order, do not hesitate to ask. " & vbCrLf & vbCrLf & _
            "Best regards," & vbCrLf & _
                   "Derek" & vbCrLf & vbCrLf & _
                   "Derek Sheahan | Sales Manager TLN Group| tlngroup.com|" & vbCrLf & _
                   "|derek@tlngroup.com | Phone: (1800) 385 8156 | Fax : (1888) 429 7748" & vbCrLf & _
                   "420-2906 West Broadway, Vancouver , BC, V6K 2G8, Canada"
        .Display
    End With

End Sub

Open in new window

Avatar of TLN_CANADA

ASKER

Thanks Chris,

It is giving an error saying object required and pointing to the line :

 For Each objAtt In objSourceItem.Attachments

of this sub :

Sub CopyAttachments(objSourceItem, objTargetItem)
' See outlookcode.com
Dim FSO As Object
Dim fldTemp As Object
Dim strPath As String
Dim strFile As String
Dim objAtt As Object
Dim fileType As String
 
   Set FSO = CreateObject("Scripting.FileSystemObject")
   Set fldTemp = FSO.GetSpecialFolder(2) 'Temp
   strPath = fldTemp.Path & "\"
   For Each objAtt In objSourceItem.Attachments
      fileType = LCase(Right(objAtt.FileName, Len(objAtt.FileName) - InStrRev(objAtt.FileName, ".")))
        strFile = strPath & objAtt.FileName
        objAtt.SaveAsFile strFile
        objTargetItem.Attachments.Add strFile, , , objAtt.DisplayName
        FSO.DeleteFile strFile
   Next
 
   Set fldTemp = Nothing
   Set FSO = Nothing
End Sub


I think you added this sub also on a previous question?

Thanks,

Derek
>>> I think you added this sub also on a previous question?

Indeed we did.

WHat is the object being 'processed'? a mail item an invite or what?

Chris
OOOOOOOOPS!

Too literal a cut and paste.

Chris
Sub EditOne(mai as variant)
Dim para As Variant
Dim strArray() As String
Dim nuMai As Object
Dim strSearchFor() As Variant: strSearchFor = Array("> Email:", "> Shipping Company:", "> Tracking Number:", "> Estimated Arrival Date:", "> Contact:", "> Company Name:")
Dim strResults() As String
Dim elem As Integer
Const str2 As String = ""
Const str3 As String = ""
Const str4 As String = ""
    
    ReDim strResults(UBound(strSearchFor))
'    If TypeName(Application.ActiveWindow) = "Explorer" Then
'        Set mai = Application.ActiveExplorer.Selection.Item(1)
'    ElseIf TypeName(Application.ActiveWindow) = "Inspector" Then
'            Set mai = Application.ActiveInspector.CurrentItem
'    Else
'        Exit Sub
'    End If
    With mai
        strArray = Split(.Body, vbCrLf)
        For Each para In strArray
            If para <> "" Then
                For elem = LBound(strSearchFor) To UBound(strSearchFor)
                    If LCase(Left(para, Len(strSearchFor(elem)))) = LCase(strSearchFor(elem)) Then strResults(elem) = Trim(Split(para, ":")(1))
                Next
            End If
        Next
    End With
'    Set mai = Nothing
    If strResults(0) = "" Then Exit Sub
    ' Only proceed if we have an email address!
    Set numai = Application.CreateItem(olMailItem)
    With numai
    CopyAttachments mai, numai
        .To = strResults(0)
        .Subject = mai.Subject
        .Body = "Dear " & Split(strResults(4) & " ", " ")(0) & vbCrLf & vbCrLf & _
            "Please find attached a photo of the first unit our production has made of your order and kindly confirm it is to your satisfaction before we proceed. If you have any other questions with this order, do not hesitate to ask. " & vbCrLf & vbCrLf & _
            "Best regards," & vbCrLf & _
                   "Derek" & vbCrLf & vbCrLf & _
                   "Derek Sheahan | Sales Manager TLN Group| tlngroup.com|" & vbCrLf & _
                   "|derek@tlngroup.com | Phone: (1800) 385 8156 | Fax : (1888) 429 7748" & vbCrLf & _
                   "420-2906 West Broadway, Vancouver , BC, V6K 2G8, Canada"
        .Display
    End With

End Sub

Open in new window

Excellent ! Working now :) One final change to this.

If I could change the subject to the following that would be great :

SUPPLIER NAME - COMPANY NAME - First Unit Confirmation Needed

The lines in the email where I am taking this from are:

> Supplier Order Number: FTN-A135

> Company Name: Test

So the subject would look like: FTN-A135 - Test - First Unit Confirmation Needed

You did something very similar to this on the last macro. I think this is the last thing and then all done. I'll have to ask my boss to add you to the payroll you've helped us out so much!!

Derek
ASKER CERTIFIED SOLUTION
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you so much, this is very helpful!