Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

waiting for file to be created before continuing code

Status
Not open for further replies.

ejmiller2

Programmer
Jun 3, 2003
38
US
hi all...

I have a code that changes the printer to a pdf writer...creates the pdf file...then e-mails this file to a person. problem is that it prints the file to pdf writer but tries to send the file before the file is actually created. i dont want to just put a generic pause in place in case for some reason the pdf writer takes longer for one output than the other (that would cause the person to get incorrect information)

so...there are two ways i can think about getting around this...but im not sure how to do them.

1. tell the code to pause UNTIL the printing is completed. but i am very unclear how to go about this...

or

2. somehow get the date(time) of the pdf file. if i can do this then i can compare to the date before the file was created to see if it is the correct file.

any suggustions on how i do that?..
thanks

eric
 
I would just use a timer and wait
much simpler than the others
So wait 10 or 20 seconds is that such a big deal, sounds like it is as automated as it can get now.

besides you need a loop anyway with a timer or file date

'put the following two line at the top under the other lines that start with "DIM"

Dim sngTime As Single
sngTime = Timer

put these three lines under the "printwhat" but above the "Next" statement

' Wait 2 seconds.
Do While Timer - sngTime < 2
Loop

DougP, MCP
 
Since VBA is single-thread only, it is difficult, but not impossible, to wait for a something to end before going on. It is beyond the scope here for me to give a complete explanation, but there are several books that go into this. One good one is the &quot;VBA Developer's Handbook&quot; by Ken Getz. Mine is an older version, but is still useful. It's ISBN is 0-7821-1951-4. Another book that will help is also an old version (there may be updated versions of it): Dan Appleman's &quot;Visual Basic 5.0 Programmer's Guide to the Win32 API&quot;, ISBN: 1-56276-446-2.

You will need to use some API's. Specifically, check out:

Code:
' File change notification functions
Private Declare Function FindFirstChangeNotification Lib &quot;kernel32&quot; _
 Alias &quot;FindFirstChangeNotificationA&quot; _
 (ByVal lpPathName As String, ByVal bWatchSubtree As Long, _
 ByVal dwNotifyFilter As Long) As Long
Private Declare Function FindNextChangeNotification Lib &quot;kernel32&quot; _
 (ByVal hChangeHandle As Long) As Long
Private Declare Function FindCloseChangeNotification Lib &quot;kernel32&quot; _
 (ByVal hChangeHandle As Long) As Long

' File change notification flags
Private Const FILE_NOTIFY_CHANGE_FILE_NAME = &H1
Private Const FILE_NOTIFY_CHANGE_DIR_NAME = &H2
Private Const FILE_NOTIFY_CHANGE_ATTRIBUTES = &H4
Private Const FILE_NOTIFY_CHANGE_SIZE = &H8
Private Const FILE_NOTIFY_CHANGE_LAST_WRITE = &H10
Private Const FILE_NOTIFY_CHANGE_SECURITY = &H100
Private Const INFINITE = &HFFFF      '  Infinite timeout

' Functions that wait for events
Private Declare Function WaitForSingleObject Lib &quot;kernel32&quot; _
 (ByVal hHandle As Long, ByVal dwMilliseconds As Long) _
 As Long
Private Declare Function WaitForMultipleObjects _
 Lib &quot;kernel32&quot; (ByVal nCount As Long, _
 lpHandles As Long, ByVal bWaitAll As Long, _
 ByVal dwMilliseconds As Long) As Long

' WaitFor... constants
Private Const WAIT_FAILED = -1&
Private Const WAIT_OBJECT_0 = 0
Private Const WAIT_ABANDONED = &H80&
Private Const WAIT_ABANDONED_0 = &H80&
Private Const WAIT_TIMEOUT = &H102&

You can set the timeout period to how long you want to wait before going on. If you set it to INFINITE, you may never get your application to continue! Your best option is to use a short timeout period, check for the result from WaitForSingleObject (for example) and if it's not what you wanted, call WaitForSingleObject again (in a loop) until you get the change you're looking for or you decide you've waited long enough (say three times through the loop and then exit with an error).

Just some thoughts. It's a lot of work, but will give you more control than just waiting for a while and hoping the file is completed!

Best of luck!

Ted
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top