excel to create pdf-s and attach to mail

shefter

New Member
Joined
Mar 22, 2011
Messages
6
hello everyone.

im new to the forums as You can see. hope you all take me in nicely.

anyway im working on my first macro and ran into a little trouble. i cant seem to find a solution either.

the idea of my code is to take data (name and email) from one sheet, open up a word template and paste that data (name and email) to specific bookmarks on the template. then save the edited template as in pdf format(different file for each row of name and email). the problem i am having is that i cant seem to get outlook to attach the recently saved pdf file.

another problem im having is closing the winword app after running the macro(i have to close it in task manager)

thanks in advance for help

Code:
Sub macro()

Dim objDoc As Word.Document
Dim filepath As String
Dim nimi As String
Dim loc As String

filepath = ThisWorkbook.Path


FinalRow = Range("A99").End(xlUp).Row
For i = 2 To FinalRow

Set objWordApp = New Word.Application

objWordApp.Visible = False

objWordApp.Documents.Open Filename:=filepath & "\" & "BLANK.doc"

On Error GoTo 0

Set objDoc = objWordApp.Documents(1)

nimi = "Title " & Sheets("firmad").Range("A" & i)
loc = filepath & "\"
str1 = Sheets("firmad").Range("A" & i)
str2 = Sheets("firmad").Range("B" & i)

'// Goto a pre-defined bookmark and type//
objWordApp.Selection.Goto What:=wdGoToBookmark, Name:="firma"
objWordApp.Selection.TypeText Text:=str1

objWordApp.Selection.Goto What:=wdGoToBookmark, Name:="mail"
objWordApp.Selection.TypeText Text:=str2


objDoc.ExportAsFixedFormat Outputfilename:=filepath & "\" & nimi, ExportFormat:=wdExportFormatPDF

objWordApp.Documents.Close

    Dim OutApp As Object
    Dim OutMail As Object
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    On Error Resume Next
    With OutMail
        .To = str2
        .CC = ""
        .BCC = ""
        .Subject = "hi lala"
        .Body = "Say something in email body if you want."
        .Attachments.Add koht & nimi 
        .Display
    End With
   
On Error GoTo 0

Set objDoc = Nothing
Set objWordApp = Nothing
    Set OutMail = Nothing
    Set OutApp = Nothing

Next i


End Sub

as you probably notice im not too good at it. i would also be greatful for tips abt fixing any other problems in the code

Silver
 
Last edited:

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Forum statistics

Threads
1,224,586
Messages
6,179,729
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top