Insure File Creation

C

Clyde

I have a report creating a pdf file by printing to an Adobe pdf driver.
How can do you insure that the file has been created before renaming it?
My current code:
open report
rename file
Works if you step, but when run without stepping, rename fails because file
creation isn't finished???, thus file not found error.
 
B

Brett Kinross

Hi Clyde

I'm not quite sure what you mean. I use the following code to check if a
file exits:

'***************** Code Start *******************
'This code was originally written by Dev Ashish
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code Courtesy of
'Dev Ashish
'
Function IsFileDIR(stPath As String, _
Optional lngType As Long) _
As Integer
'Fully qualify stPath
'To check for a file
' ?IsFileDIR("c:\winnt\win.ini")
'To check for a Dir
' ?IsFileDir("c:\msoffice",vbdirectory)
'
On Error Resume Next
IsFileDIR = Len(Dir(stPath, lngType)) > 0
End Function
'***************** Code End *********************

Hope this helps

Cheers
Brett Kinross
 
B

Brett Kinross

Sorry - just read your post and understand it now. The code is executing
before the previous line has finished.
You could still use the above code and just loop until it is true or until a
certain amount of time has elapsed (so you won't be caught in a perpetual
loop).
I'm sure there will be another way to do it but that should work.
Cheers
Brett
 
C

Clyde

Brett, thanks for the response.
The following code is my original try at the problem and it seems to me
about the same as you suggested, but it doesn't work and I don't know why.

n = 0
Do Until strPDFDone <> ""
'returns "" if file not found
strPDFDone = Dir(mstrPDFDir & "rptInv.pdf")
n = n + 1
Loop
MsgBox "count = " & CStr(n)
strPDFDone = ""
strFile = mstrPDFDir & "INV" & !InvNo & ".pdf" 'desired new name
Name mstrPDFDir & "rptInv.PDF" As strFile

Where mstrPDFDir is the directory ex: c:\Invoice\ and rptInv.pdf is the know
file name that will be created and strFile is the desired new name. All of
this is in a loop of email invoice customers so !InvNo is the invoice number.
I can extract the customer number from the invoice number and thus find the
email address so that when I loop through the files I can attach the correct
pdf to the customer's email.

Thanks for your time.
 
C

Clyde

On the off chance that your suggested code would resolve the issue I tried
this:

Do Until blnFileCreated
blnFileCreated = IsFileDIR(mstrPDFDir & "rptInv.pdf")
'n = n + 1
Loop
Same result at the line where attempt to rename:
"Run/Time error 75 - Path/File access error"
If you then "step" that line in debug it works, so apparently the file isn't
quite available.
 
C

Clyde

OK this worked:
varStart = Timer
Do While Timer < varStart + 2
DoEvents
Loop
Just before renaming.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top