PDA

View Full Version : Solved: Ontime Problem



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

mdmackillop
05-17-2009, 03:44 PM
How about something like

ActiveDocument.PrintOut
Do
DoEvents
Loop until len(dir(MyFileName))>0
ActivePrinter = CurrentPrinter$

chrismc
05-18-2009, 01:39 PM
Thanks for having a look.

I've tried your code and my PC still grinds to a halt. The main problem seems to be associated with the Dir command. The PDF print job sticks spooling while the Dir is looping. If I do CTRL-BREAK to stop the loop then the PDF print completes.

I put a counter in the loop and it does increment so the Dir is being repeated.

Any thoughts?

Cheers


Chris

chrismc
06-07-2009, 02:34 AM
As it happens, I had to replace my PC and am now using Vista and Office 2007. My problem has gone away - both the OnTime and Dir approaches seem to work.

Thanks

mdmackillop
06-07-2009, 03:03 AM
A rather extreme solution. :igiveup:
I hope the problem doesn't lie in a template which you might import into your new setup.
Regards
MD

chrismc
06-07-2009, 04:52 AM
A rather extreme solution.

MD

It was the solution to having an old and tired PC. Getting the macro to work was an added bonus :biggrin: