Word Documents -> PDF -> Email attachment

Z

zeb

I have a need (programmatically) to produce PDF attached Emails.

My Word VBA code does the following:

1) Prints the PDF document to a specified path using PDF writer
2) Checks that the PDF file exists and is of a reasonable size
3) Uses the Outlook object to create a mail item (with attachment)
4) Sends the document.

And it works – well it does in the VBA development environment!
When I run from a templated document the PDFs don't appear until
after my check code (2) times out i.e. when the task errors

I've tried delays in the check code – but they seem to make matters
worse! I can only guess that the problem is application thread
related.

Anyway, anyone got any ideas how I can solve this problem using VBA
before I resort to other languages
 
J

Jezebel

Doevents is perhaps the instruction you're looking for. It's often what's
missing from code that runs in the IDE but not on its own. Between steps 1
and 2 put a loop that checks if the file exists yet, if not calls DoEvents,
waits for some intereval, and checks again. You'll want some emergency exit
in case the file never appears (like don't go on trying for more than 30
seconds or some such).
 
Z

zeb

Ok I figured it out!

My Print to file routine should have had ActiveDocument.PrintOut
Background property set to FALSE - thus preventing further code
executation during document printing!

For completenes I have included the corrected routine below :)




Function printtofile(Printer, Path, Extn As String) As Boolean

With Dialogs(wdDialogFilePrintSetup)
.Printer = Printer
.DoNotSetAsSysDefault = True
.Execute
End With

'Print to file
Application.ActiveDocument.PrintOut printtofile:=True,
outputfilename:=Path, Background:=False

printtofile = (Dir(Path & Extn) <> "")

End Function
 

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