Print Page | Close Window

PDF-file is saved too late

Printed From: www.exp-systems.com
Category: PDF reDirect
Forum Name: Programming
Forum Discription: VBA and Batch Tools to control PDF reDirect Pro
URL: http://www.exp-systems.com/Forum_exp/forum_posts.asp?TID=119
Printed Date: 22 Nov 24 at 8:22AM


Topic: PDF-file is saved too late
Posted By: Fons
Subject: PDF-file is saved too late
Date Posted: 26 Aug 05 at 8:01AM

I would like to use the program for the following :

 

In MS Word I use Visual Basic to create a PDF-file and to save it.

 

Also in the same code , as next step, I open Outlook to make a new message and to attach the

 

PDF-file.

 

But at the moment a new message is been created, the PDF-file is not available, and is only

 

saved a few seconds later.

 

All efforts to create and to save the file before Outlook open automatically are in vain.

 

Do someone have a solution?




Replies:
Posted By: Michel_K17
Date Posted: 26 Aug 05 at 8:46AM
   What you need to do is to monitor the PDF being created, and maybe even show a progress bar to provide feedback for the user. If you don't want the user to have to wait, you will need to write a separate program that runs in it's own thread that does the same thing.

   Assuming you want it all within MS Word and using VBA, you can use the "rename" function to check to see if the file has been created, and released. Here is some sample code:

'---------------------------------------------------------------------------------------
' Procedure : CheckFileCreated
' Author    : Michel_K17
' Purpose   : To check if a File Exists and is not "busy" (being written to my another process)
'
' History   : v1 (7/10/04)
'           : Make sure I can RENAME file (to itself). If it fails, then file still "busy".
'---------------------------------------------------------------------------------------
Public Function CheckFileCreated(FileName As String) As Boolean
    On Error GoTo ErrorHandler
    CheckFileCreated = False
    Name FileName As FileName
    CheckFileCreated = True
ErrorHandler:
End Function

   All you need to do afterwards is to create a Loop that waits until the PDF is available. Consider having a counter that allows you to leave the Loop after a few minutes (ie don't create an infinite loop).

   I may have some sample VBA code that does that. I'll see if I can dig it up later today.


Posted By: Fons
Date Posted: 30 Aug 05 at 4:58AM

I thank toy for your answer, but :

 

I have already tried to create a control to look if the file already exists.

 

BUT as long as the Loop runs, the PDF-program waits to create the file :

 

The PDF-file is only created after the VBA code has stopped.

 



Posted By: Michel_K17
Date Posted: 30 Aug 05 at 8:35AM
Hi Fons,

   This is curious. THe PDF is created in a separate thread, and the loop should not prevent creation. Can you send me a copy of your VBA code by e-mail? I can take a look and try to fix it for you.

   Cheers!


Posted By: Michel_K17
Date Posted: 05 Sep 05 at 9:28PM
Thank you for the VBA code you sent. There were a couple of errors I found, which I have fixed and sent back to you by e-mail.

For everybody else, the sample code I provide for v2.1.x now includes the code that checks that the PDF has been created. You can download this code [ http://www.exp-systems.com/forum_exp/forum_posts.asp?TID=39&PN=1 - here ].


Posted By: yyzmxs
Date Posted: 27 Feb 06 at 2:58PM

Hi Michel,

I just started to experience the same problem. Once or twice in 10 attempts (I tried both excel and word) my code hangs on the loop below. I see a window telling me that my system is creating .... .pdf file, but that's it. Looks like the loop is going and going without noticing that the file has been already created.

        Dim MaxTime2Unlock As Single
        MaxTime2Unlock = Timer() + 60

        ' Start the loop
        Do While MaxTime2Unlock >= Timer And Not CheckFileCreated(sPath & "\" & sFilename)
            ' Share the CPU with other programs
            Sleep (100)
            DoEvents
        Loop

What's weird is that sometimes it hangs and other times it doesn't. I made sure I wasn't running any system demanding processes at the time of trial.

 

Thanks

Marek

 

 



Posted By: yyzmxs
Date Posted: 27 Feb 06 at 3:17PM

I've just noticed another symptom.

If I leave the application (in my case it's either Word or Excel) I print from visible (normally I'd like it hidden so the user is not bothered with windows flashing on his screen) it runs faster and never hangs. Weird isn't it or not?

Thanks

Marek



Posted By: Michel_K17
Date Posted: 27 Feb 06 at 4:57PM
Hi there,

   That is weird indeed. I will do some testing and get back to you. Which version of Windows and Office are you using?

Michel


Posted By: yyzmxs
Date Posted: 28 Feb 06 at 8:45AM

Hi Michel,

I am using MS Office 2003 and Win XP Proffesional SP1.

Marek



Posted By: Michel_K17
Date Posted: 01 Mar 06 at 1:05AM
Thanks. One of my machines is Office 2003 with Win XP Pro SP2. So far, I have not been able to duplicate the problem, but I will be trying more complex files to see if that makes a difference.


Posted By: yyzmxs
Date Posted: 01 Mar 06 at 8:53AM

Maybe the difference is whether the print command is given from excel app and sheet objects created on the fly compared to printing directly from withing excel sheet.

Try let say write a routine in Excel vba to open and print a Word document and see what that does.

Marek



Posted By: yyzmxs
Date Posted: 02 Mar 06 at 4:23PM

Michel,

I think I got it figured out. It looks like if you don't (and I didn't) use background:=false parameter for your .printout function that's what causing the code to hang. I've added it in and since then haven't had one problem.

Regards

Marek



Posted By: Michel_K17
Date Posted: 06 Mar 06 at 1:06AM
Hello Marek,

   Sorry for the delay: I was finishing the new web site. Anyway, I was looking at your posting. I could not find background:=false in the .PrintOut command for Excel, only for MS Word. In your posting, you said the problem was with both Word and Excel. Is the problem still occuring with Excel?

   Good catch though. Very weird though. If it was set to "False", the macro is supposed to pause while the document is printing.

   Thanks for the update!

Michel


Posted By: Spyryl
Date Posted: 19 Apr 06 at 2:53AM
Greetings Michel,

I just had the same thing happen to me (from Access not Excel). The report spools as the code runs but it doesn't seem to be turned into a pdf until the code has stopped running.

Any ideas on this one???

C!!!


Posted By: Michel_K17
Date Posted: 20 Apr 06 at 1:20AM
   Well, beyond the ideas mentioned above in this forum post, you should make sure that you share the CPU with the other programs. So, if you have setup a routine (and more specifically a loop) that checks for the PDF to be created, make sure you use the "Do Events" command so that your code does not take over all the CPU cycles.


Posted By: DiscoPymp
Date Posted: 20 Apr 06 at 11:28AM

Did you try something as simple as this to pause until the pdf is actually there?

Dim MyFile As String
'PAUSE UNTIL THE PDF HAS BEEN CREATED
MyFile = ""
Do Until MyFile <> ""
    MyFile = Dir$("C:\test\test.pdf")
    DoEvents
Loop



-------------
DiscoPymp


Posted By: Michel_K17
Date Posted: 20 Apr 06 at 11:25PM
Hi,

   Actually that won't work because the PDF will be partially created, so your code will show that the file is there, but you can't load it into the Reader until the PDF has been fully written. Therefore, you need to test for the file to be released and available. So, one test you can do is to try to Open the file for writing to it, or, (see post #2 above) is to try to rename it (using the same target name). Either way work fine.

   Now, if all you want to do is check that the file will be created, then yes, your code will work fine, with one caveat is that you are setting yourself up for an infinite loop. So, you may want to have a timer or some other exit mechanism so that your code does run forever.

Michel



Print Page | Close Window