Email a Word document as PDF from within Word

F

fam.hougaard

Is it possible to write af VBA code that converts an open Word
document to a PDF file using a pdf-converter (installed as a printer
driver - its eDoc Printer) and attaches it to an email.

And how?
 
G

Graham Mayor

If the pdf driver is programmable from vba to define the name of the PDF
file created by the driver, OR
it automatically, without user interference, uses the name of the saved
document to create a PDF with the same name, then it should be possible.
Otherwise it is just as simple to attach the PDF to a mail message.
Acrobat provides this function out of the box (at a price).

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
F

fam.hougaard

If the pdf driver is programmable from vba to define the name of the PDF
file created by the driver, OR
it automatically, without user interference, uses the name of the saved
document to create a PDF with the same name, then it should be possible.
Otherwise it is just as simple to attach the PDF to a mail message.
Acrobat provides this function out of the box (at a price).

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>






- Vis tekst i anførselstegn -

It generates a PDF file with the same name as the document.

It does provide the option to attach the pdf to an email, but this is
selected in then "print window":
When i select to print to the PDF printer-driver a box opens where
email attachment can be selected folowed by pressing a Save buttom.

However i would like it all done with just selecting one menu item.
 
G

Graham Mayor

Without the appropriate driver to test, and Outlook as the e-mail app.,
something along the lines of the following might work if the driver creates
pdf files with the same name as the document. If it saves them in the same
folder as the documents you can simplify the pdfname lines to

pdfName = Left(ActiveDocument.Fullname, Len(ActiveDocument.Fullname) - 3)
pdfName = pdfName & "pdf" 'the name and path of the PDF



Sub PDFAs Attachment()
Dim sPrinter As String
Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem
Dim pdfName As String
sPrinter = ActivePrinter

If Len(ActiveDocument.Path) = 0 Then 'Document has not been saved
ActiveDocument.Save 'so save it
End If

pdfName = Left(ActiveDocument.name, Len(ActiveDocument.name) - 3)
pdfName = "c:\pdfpath\" & pdfName & "pdf" 'the name and path of the PDF
ActivePrinter = "PDF Driver Name" 'set the PDF driver name
Application.PrintOut 'print to the PDF driver
ActivePrinter = sPrinter 'restore the original printer
On Error Resume Next

'see if Outlook is running and if so turn your attention there
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then 'Outlook isn't running
'So fire it up
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If
'Open a new e-mail message
Set oItem = oOutlookApp.CreateItem(olMailItem)
With oItem 'and add the detail to it
.To = "(e-mail address removed)" 'send to this address
.Subject = "New subject" 'This is the message subject
.Body = "See attached document" ' This is the message body text
.Attachments.Add Source:=pdfName, Type:=olByValue
.Send
'.Display
'**********************************
'If you want to view the message before it goes
'change the line above from .Send to .Display
'Otherwise the message is sent straight to the Outbox
'and if you have Outlook set to send mail immediately,
'it will simply be Sent
'with no obvious sign that Outlook has operated.
'Apart from the copy in the Outlook Sent folder
'**********************************
End With
If bStarted Then 'If the macro started Outlook, stop it again.
oOutlookApp.Quit
End If
'Clean up
Set oItem = Nothing
Set oOutlookApp = Nothing
End Sub
 
F

fam.hougaard

I get an error "User-defined type not defined" in this line:

Dim oOutlookApp As Outlook.Application

Does the code run OK with you?
 
G

Graham Mayor

Sorry, you'll need to add a reference to Outlook's object library in vba
tools > references. I was forgetting that this is not checked by default.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

I am not sure how that happened. There should be no space in the macro name
thus

Sub PDFAsAttachment()

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
F

fam.hougaard

I am not sure how that happened. There should be no space in the macro name
thus

Sub PDFAsAttachment()

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>





- Vis tekst i anførselstegn -

Thanks Graham, that did the trick

However now it stops at an Filesave As box (probebly from til PDF-
driver) where it waits for the user to press Save and then it
continues creating the email. I would lile there to be no other user-
interaction but to send the mail. Have to figur out if the Save button
kan be presses/activatet automatically. Any ideas?.
 
G

Graham Mayor

Thanks Graham, that did the trick

However now it stops at an Filesave As box (probebly from til PDF-
driver) where it waits for the user to press Save and then it
continues creating the email. I would lile there to be no other user-
interaction but to send the mail. Have to figur out if the Save button
kan be presses/activatet automatically. Any ideas?.

As I said at the outset, the macro will only work if the driver is
programmable or if the PDF driver saves the PDF without user interaction.
You will have to ask that of the PDF creation software people.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
F

fam.hougaard

Just got back to my project.

Your code dosnt attach the pdf file to the email.

Any ideas?
 
F

fam.hougaard

Just got back to my project.

Your code dosnt attach thepdffile to theemail.

Any ideas?

Figured it out - it was due to the location of the pdf was in another
place ;-)
 
T

Tushar P

I use Adobe 9 version. However I am not able to locate the printer driver. Can u guide me where it can be?



Graham Mayor wrote:

Without the appropriate driver to test, and Outlook as the e-mail app.
30-Jan-09

Without the appropriate driver to test, and Outlook as the e-mail app.,
something along the lines of the following might work if the driver creates
pdf files with the same name as the document. If it saves them in the same
folder as the documents you can simplify the pdfname lines to

pdfName = Left(ActiveDocument.Fullname, Len(ActiveDocument.Fullname) - 3)
pdfName = pdfName & "pdf" 'the name and path of the PDF



Sub PDFAs Attachment()
Dim sPrinter As String
Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem
Dim pdfName As String
sPrinter = ActivePrinter

If Len(ActiveDocument.Path) = 0 Then 'Document has not been saved
ActiveDocument.Save 'so save it
End If

pdfName = Left(ActiveDocument.name, Len(ActiveDocument.name) - 3)
pdfName = "c:\pdfpath\" & pdfName & "pdf" 'the name and path of the PDF
ActivePrinter = "PDF Driver Name" 'set the PDF driver name
Application.PrintOut 'print to the PDF driver
ActivePrinter = sPrinter 'restore the original printer
On Error Resume Next

'see if Outlook is running and if so turn your attention there
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then 'Outlook isn't running
'So fire it up
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If
'Open a new e-mail message
Set oItem = oOutlookApp.CreateItem(olMailItem)
With oItem 'and add the detail to it
.To = "(e-mail address removed)" 'send to this address
.Subject = "New subject" 'This is the message subject
.Body = "See attached document" ' This is the message body text
.Attachments.Add Source:=pdfName, Type:=olByValue
.Send
'.Display
'**********************************
'If you want to view the message before it goes
'change the line above from .Send to .Display
'Otherwise the message is sent straight to the Outbox
'and if you have Outlook set to send mail immediately,
'it will simply be Sent
'with no obvious sign that Outlook has operated.
'Apart from the copy in the Outlook Sent folder
'**********************************
End With
If bStarted Then 'If the macro started Outlook, stop it again.
oOutlookApp.Quit
End If
'Clean up
Set oItem = Nothing
Set oOutlookApp = Nothing
End Sub




(e-mail address removed) wrote:

Previous Posts In This Thread:

If the pdf driver is programmable from vba to define the name of the PDF file
If the pdf driver is programmable from vba to define the name of the PDF
file created by the driver, OR
it automatically, without user interference, uses the name of the saved
document to create a PDF with the same name, then it should be possible.
Otherwise it is just as simple to attach the PDF to a mail message.
Acrobat provides this function out of the box (at a price).

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


(e-mail address removed) wrote:

Without the appropriate driver to test, and Outlook as the e-mail app.
Without the appropriate driver to test, and Outlook as the e-mail app.,
something along the lines of the following might work if the driver creates
pdf files with the same name as the document. If it saves them in the same
folder as the documents you can simplify the pdfname lines to

pdfName = Left(ActiveDocument.Fullname, Len(ActiveDocument.Fullname) - 3)
pdfName = pdfName & "pdf" 'the name and path of the PDF



Sub PDFAs Attachment()
Dim sPrinter As String
Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem
Dim pdfName As String
sPrinter = ActivePrinter

If Len(ActiveDocument.Path) = 0 Then 'Document has not been saved
ActiveDocument.Save 'so save it
End If

pdfName = Left(ActiveDocument.name, Len(ActiveDocument.name) - 3)
pdfName = "c:\pdfpath\" & pdfName & "pdf" 'the name and path of the PDF
ActivePrinter = "PDF Driver Name" 'set the PDF driver name
Application.PrintOut 'print to the PDF driver
ActivePrinter = sPrinter 'restore the original printer
On Error Resume Next

'see if Outlook is running and if so turn your attention there
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then 'Outlook isn't running
'So fire it up
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If
'Open a new e-mail message
Set oItem = oOutlookApp.CreateItem(olMailItem)
With oItem 'and add the detail to it
.To = "(e-mail address removed)" 'send to this address
.Subject = "New subject" 'This is the message subject
.Body = "See attached document" ' This is the message body text
.Attachments.Add Source:=pdfName, Type:=olByValue
.Send
'.Display
'**********************************
'If you want to view the message before it goes
'change the line above from .Send to .Display
'Otherwise the message is sent straight to the Outbox
'and if you have Outlook set to send mail immediately,
'it will simply be Sent
'with no obvious sign that Outlook has operated.
'Apart from the copy in the Outlook Sent folder
'**********************************
End With
If bStarted Then 'If the macro started Outlook, stop it again.
oOutlookApp.Quit
End If
'Clean up
Set oItem = Nothing
Set oOutlookApp = Nothing
End Sub




(e-mail address removed) wrote:

Sorry, you'll need to add a reference to Outlook's object library in vba tools
Sorry, you'll need to add a reference to Outlook's object library in vba
tools > references. I was forgetting that this is not checked by default.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


(e-mail address removed) wrote:

I am not sure how that happened.
I am not sure how that happened. There should be no space in the macro name
thus

Sub PDFAsAttachment()

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


(e-mail address removed) wrote:

Re: Email a Word document as PDF from within Word
(e-mail address removed) wrote:


As I said at the outset, the macro will only work if the driver is
programmable or if the PDF driver saves the PDF without user interaction.
You will have to ask that of the PDF creation software people.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Email a Word document as PDF from within Word
Is it possible to write af VBA code that converts an open Word
document to a PDF file using a pdf-converter (installed as a printer
driver - its eDoc Printer) and attaches it to an email.

And how?

Re: Email a Word document as PDF from within Word

It generates a PDF file with the same name as the document.

It does provide the option to attach the pdf to an email, but this is
selected in then "print window":
When i select to print to the PDF printer-driver a box opens where
email attachment can be selected folowed by pressing a Save buttom.

However i would like it all done with just selecting one menu item.

Re: Email a Word document as PDF from within Word
Thanks I will try the code, but isnt this line wrong:

-

Re: Email a Word document as PDF from within Word
I get an error "User-defined type not defined" in this line:

Dim oOutlookApp As Outlook.Application

Does the code run OK with you?

Re: Email a Word document as PDF from within Word
me

Thanks Graham, that did the trick

However now it stops at an Filesave As box (probebly from til PDF-
driver) where it waits for the user to press Save and then it
continues creating the email. I would lile there to be no other user-
interaction but to send the mail. Have to figur out if the Save button
kan be presses/activatet automatically. Any ideas?.

Just got back to my project.Your code dosnt attach the pdf file to the email.
Just got back to my project.

Your code dosnt attach the pdf file to the email.

Any ideas?

Re: Email a Word document as PDF from within Word
Figured it out - it was due to the location of the pdf was in another
place ;-)


Submitted via EggHeadCafe - Software Developer Portal of Choice
WPF Report Engine, Part 4
http://www.eggheadcafe.com/tutorial...45-8b37cb7f3186/wpf-report-engine-part-4.aspx
 
G

Graham Mayor

For Adobe Acrobat (full version not the reader) change the line
ActivePrinter = "PDF Driver Name" 'set the PDF driver name
to
ActivePrinter = "Adobe PDF"

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top