chrismc
05-17-2009, 02:22 PM
Hi,
I want to have Word print the open document as a PDF and then attach the PDF to a more or less blank email. Unfortunately, even the smallest of PDFs takes some time for my PC to produce and so there is no file ready to attach when the vba looks for it.
I have tried the approach of checking for the existence of the file:
While Checkfile = ""
Checkfile= Dir(FileName)
Wend
but that seems to bring the PC pretty much to a halt.
So I have looked at using Ontime to build in delays to allow the printing to PDF and then check. It simply doesn't work. The Subs are in the same module in normal.dot. Here's the code so far:
Sub EmailPDF()
'START
If MsgBox("Do you want to email a PDF version?", vbYesNo) = vbNo Then Exit Sub
'get filename and folder
StartFile = ActiveDocument.Name
MyFolder = ActiveDocument.Path
If MsgBox("Do you want to change the file name from " & ActiveDocument.Name & "?", vbYesNo) = vbYes Then
ActiveDocument.SaveAs InputBox("Check file name", , MyFolder & "\" & ActiveDocument.Name)
End If
MyFile = ActiveDocument.Name
'copy settings.ini to runonce.ini
IniSource = "C:\Documents and Settings\ChrisMc\Application Data\Bullzip\PDF Printer\settings.ini"
IniDest = "C:\Documents and Settings\ChrisMc\Application Data\Bullzip\PDF Printer\runonce.ini"
FileCopy IniSource, IniDest
'update runonce.ini with folder details
System.PrivateProfileString(IniDest, "PDF Printer", "output") = MyFolder & "\" & MyFile & ".pdf"
System.PrivateProfileString(IniDest, "PDF Printer", "ShowPDF") = "No"
System.PrivateProfileString(IniDest, "PDF Printer", "ShowSaveAS") = "nofile"
System.PrivateProfileString(IniDest, "PDF Printer", "ShowSettings") = "never"
'print with bullzip
CurrentPrinter$ = ActivePrinter
ActivePrinter = "Bullzip PDF Printer on NE04:"
ActiveDocument.PrintOut
ActivePrinter = CurrentPrinter$
Application.OnTime Now + TimeValue("00:00:30"), "EmailPDF2"
test
End Sub
Private Sub test()
MsgBox "test"
End Sub
Private Sub EmailPDF2()
CheckFile = Dir(MyFolder & "\" & MyFile & ".pdf")
If CheckFile = "" Then
Application.OnTime Now + TimeValue("00:00:10"), "EmailPDF2"
Else
EmailPDF3
End If
End Sub
Private Sub EmailPDF3()
'write email
MyBody = MyBody & "Many thanks." & vbCr & vbCr & "Chris McCarthy" & vbCr
Set olApp = GetObject(, "Outlook.Application")
Set olMail = olApp.createitem(olMailItem)
Set olNamespace = olApp.GetNamespace("MAPI")
With olMail
.body = MyBody
.Attachments.Add MyFolder & "\" & MyFile & ".pdf"
.Display 'This will display the message for you to check and send yourself
End With
Set olApp = Nothing
Set olMail = Nothing
End Sub
The code works fine in the first Sub in as much as it runs all the way through and calls the test Sub. I put that in because Sub EmailPDF2 doesn't run and I wanted to see if branch would work.
If I put in a couple of subs in a module with the first one just using Ontime to call the second one that works.
Is there anything obvious that I have got wrong?
Using Office 2003 on Windows XP on a tired old PC.
Thanks
Chris
I want to have Word print the open document as a PDF and then attach the PDF to a more or less blank email. Unfortunately, even the smallest of PDFs takes some time for my PC to produce and so there is no file ready to attach when the vba looks for it.
I have tried the approach of checking for the existence of the file:
While Checkfile = ""
Checkfile= Dir(FileName)
Wend
but that seems to bring the PC pretty much to a halt.
So I have looked at using Ontime to build in delays to allow the printing to PDF and then check. It simply doesn't work. The Subs are in the same module in normal.dot. Here's the code so far:
Sub EmailPDF()
'START
If MsgBox("Do you want to email a PDF version?", vbYesNo) = vbNo Then Exit Sub
'get filename and folder
StartFile = ActiveDocument.Name
MyFolder = ActiveDocument.Path
If MsgBox("Do you want to change the file name from " & ActiveDocument.Name & "?", vbYesNo) = vbYes Then
ActiveDocument.SaveAs InputBox("Check file name", , MyFolder & "\" & ActiveDocument.Name)
End If
MyFile = ActiveDocument.Name
'copy settings.ini to runonce.ini
IniSource = "C:\Documents and Settings\ChrisMc\Application Data\Bullzip\PDF Printer\settings.ini"
IniDest = "C:\Documents and Settings\ChrisMc\Application Data\Bullzip\PDF Printer\runonce.ini"
FileCopy IniSource, IniDest
'update runonce.ini with folder details
System.PrivateProfileString(IniDest, "PDF Printer", "output") = MyFolder & "\" & MyFile & ".pdf"
System.PrivateProfileString(IniDest, "PDF Printer", "ShowPDF") = "No"
System.PrivateProfileString(IniDest, "PDF Printer", "ShowSaveAS") = "nofile"
System.PrivateProfileString(IniDest, "PDF Printer", "ShowSettings") = "never"
'print with bullzip
CurrentPrinter$ = ActivePrinter
ActivePrinter = "Bullzip PDF Printer on NE04:"
ActiveDocument.PrintOut
ActivePrinter = CurrentPrinter$
Application.OnTime Now + TimeValue("00:00:30"), "EmailPDF2"
test
End Sub
Private Sub test()
MsgBox "test"
End Sub
Private Sub EmailPDF2()
CheckFile = Dir(MyFolder & "\" & MyFile & ".pdf")
If CheckFile = "" Then
Application.OnTime Now + TimeValue("00:00:10"), "EmailPDF2"
Else
EmailPDF3
End If
End Sub
Private Sub EmailPDF3()
'write email
MyBody = MyBody & "Many thanks." & vbCr & vbCr & "Chris McCarthy" & vbCr
Set olApp = GetObject(, "Outlook.Application")
Set olMail = olApp.createitem(olMailItem)
Set olNamespace = olApp.GetNamespace("MAPI")
With olMail
.body = MyBody
.Attachments.Add MyFolder & "\" & MyFile & ".pdf"
.Display 'This will display the message for you to check and send yourself
End With
Set olApp = Nothing
Set olMail = Nothing
End Sub
The code works fine in the first Sub in as much as it runs all the way through and calls the test Sub. I put that in because Sub EmailPDF2 doesn't run and I wanted to see if branch would work.
If I put in a couple of subs in a module with the first one just using Ontime to call the second one that works.
Is there anything obvious that I have got wrong?
Using Office 2003 on Windows XP on a tired old PC.
Thanks
Chris