Hi there! I'm quite new to VBA and need help for the following issue
Within olMailItem is the method .Attachments.Add "Datapath"Legends.docx
I'd like to make the path variable, pulling the path (via a Function) out of an table from a word docx, which unfortunately does not work.
When the function pulls out the data from the table, the string is also two signs longer somehow, I'm using Left() to cut it.
Below are the Sub and Function and I've attached the Word file.
Thanks a lot!!
Sub Email_Single_Row()
Dim oOutlook As Outlook.Application
Dim oEmailItem As MailItem
Dim mail_address As String
Dim attach_address As String
Dim sbj As String
Dim Subject As String
Dim text_body As String
attach_address = Left(Attach(2), Len(Attach(2)) - 2)
sbj = Left(Title(1), Len(Title(1)) - 2)
mail_address = Cells(Selection.Row, 10)
On Error Resume Next
Set oOutlook = GetObject(, "Outlook.Application")
If oOutlook Is Nothing Then Set oOutlook = CreateObject("Outlook.Application")
Set oEmailItem = oOutlook.CreateItem(olMailItem)
With oEmailItem
.To = mail_address
.CC = ""
.BCC = ""
.Subject = sbj
.Attachments.Add (attach_address)
.body = Template(1)
.Display
End With
Set oEmailItem = Nothing
Set oOutlook = Nothing
End Sub
Public Function Attach(k As Variant) As Variant
Dim appWD As Word.Application
Set appWD = CreateObject("Word.Application")
appWD.Documents.Open ("C:\Succulent\Legends.docx"), ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
wdOpenFormatAuto
If k = 0 Then
Attach = ""
appWD.Quit
Exit Function
Else
hashtag = appWD.ActiveDocument.Tables(1).Columns(3).Cells(k + 1)
Attach = hashtag
appWD.Quit
End If
End Function
Bookmarks