Active TopicsActive Topics  Display List of Forum MembersMemberlist  Search The ForumSearch  HelpHelp
  RegisterRegister  LoginLogin
Programming
 EXP Systems Forum : PDF reDirect : Programming
Topic: PDF-file is saved too late Post Reply Post New Topic
Author Message
Fons
Newbie
Newbie


Joined: 26 Aug 05
Posts: 2
Quote Fons Replybullet Topic: PDF-file is saved too late
    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?

IP IP Logged
Michel_K17
Moderator Group
Moderator Group
Avatar
Forum Administrator

Joined: 25 Jan 03
Posts: 1673
Quote Michel_K17 Replybullet 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.
IP IP Logged
Fons
Newbie
Newbie


Joined: 26 Aug 05
Posts: 2
Quote Fons Replybullet 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.

 

IP IP Logged
Michel_K17
Moderator Group
Moderator Group
Avatar
Forum Administrator

Joined: 25 Jan 03
Posts: 1673
Quote Michel_K17 Replybullet 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!
IP IP Logged
Michel_K17
Moderator Group
Moderator Group
Avatar
Forum Administrator

Joined: 25 Jan 03
Posts: 1673
Quote Michel_K17 Replybullet 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 [here].

Edited by Michel_K17
IP IP Logged
yyzmxs
Newbie
Newbie


Joined: 06 Feb 06
Posts: 11
Quote yyzmxs Replybullet 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

 

 

IP IP Logged
yyzmxs
Newbie
Newbie


Joined: 06 Feb 06
Posts: 11
Quote yyzmxs Replybullet 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

IP IP Logged
Michel_K17
Moderator Group
Moderator Group
Avatar
Forum Administrator

Joined: 25 Jan 03
Posts: 1673
Quote Michel_K17 Replybullet 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
IP IP Logged
yyzmxs
Newbie
Newbie


Joined: 06 Feb 06
Posts: 11
Quote yyzmxs Replybullet Posted: 28 Feb 06 at 8:45AM

Hi Michel,

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

Marek

IP IP Logged
Michel_K17
Moderator Group
Moderator Group
Avatar
Forum Administrator

Joined: 25 Jan 03
Posts: 1673
Quote Michel_K17 Replybullet 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.
IP IP Logged
yyzmxs
Newbie
Newbie


Joined: 06 Feb 06
Posts: 11
Quote yyzmxs Replybullet 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

IP IP Logged
yyzmxs
Newbie
Newbie


Joined: 06 Feb 06
Posts: 11
Quote yyzmxs Replybullet 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

IP IP Logged
Michel_K17
Moderator Group
Moderator Group
Avatar
Forum Administrator

Joined: 25 Jan 03
Posts: 1673
Quote Michel_K17 Replybullet 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
IP IP Logged
Spyryl
Newbie
Newbie
Avatar

Joined: 16 Apr 06
Location: Australia
Posts: 6
Quote Spyryl Replybullet 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!!!
IP IP Logged
Michel_K17
Moderator Group
Moderator Group
Avatar
Forum Administrator

Joined: 25 Jan 03
Posts: 1673
Quote Michel_K17 Replybullet 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.
IP IP Logged
DiscoPymp
Newbie
Newbie


Joined: 17 Apr 06
Posts: 2
Quote DiscoPymp Replybullet 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
IP IP Logged
Michel_K17
Moderator Group
Moderator Group
Avatar
Forum Administrator

Joined: 25 Jan 03
Posts: 1673
Quote Michel_K17 Replybullet 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
IP IP Logged
Post Reply Post New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum