Checking if a word doc exists

G

Guest

I have created some code to create and save a word doc to a specfic location
on my server. Is there an easy way to check to see if the document already
exists before the "AppWord.ActiveDocument.SaveAs" code is run. Have copied
sections of the code which outline the folder loction (myPath) the folder
name (myFolder) and the last section of code which give the word doc its name
and save it to the location. At the moment if the word doc exists it just
overwrites it.

myFolder = Me![txtRef] & "_" & Me![txtCustomerName]
myPath = "\\server\LossFiles\"
AppWord.ActiveDocument.SaveAs myPath & "\" & myFolder & "\" &
Me.txtJobNumber & "_" & "ils"

Thanks Inadvance

Matt
 
N

Nikos Yannacopoulos

Matt,

try this:

myFolder = Me![txtRef] & "_" & Me![txtCustomerName]
myPath = "\\server\LossFiles\"

MyFile = myPath & "\" & myFolder & "\" & Me.txtJobNumber & "_" & "ils"

If Dir(MyFile)<> "" Then
AppWord.ActiveDocument.SaveAs MyFile
Else
'code to run if file exists
End If

The trick is that Dir(MyFile) returns the file name if the file exists,
or a zero-length string if not.

HTH,
Nikos
 
K

Kevin K. Sullivan

Make sure that you account for implicit file extensions! I'm guessing
that Word is adding .doc onto your file name during SaveAs.

If so, use Nikos's code with but add the extension

MyFile = myPath & "\" & myFolder & "\" & Me.txtJobNumber & "_ils.doc"

Dir("C:\a") will not detect the file C:\a.doc


HTH,

Kevin
myFolder = Me![txtRef] & "_" & Me![txtCustomerName]
myPath = "\\server\LossFiles\"

MyFile = myPath & "\" & myFolder & "\" & Me.txtJobNumber & "_" & "ils"


If Dir(MyFile)<> "" Then
AppWord.ActiveDocument.SaveAs MyFile
Else
'code to run if file exists
End If

Nikos said:
Matt,

try this:

myFolder = Me![txtRef] & "_" & Me![txtCustomerName]
myPath = "\\server\LossFiles\"

MyFile = myPath & "\" & myFolder & "\" & Me.txtJobNumber & "_" & "ils"

If Dir(MyFile)<> "" Then
AppWord.ActiveDocument.SaveAs MyFile
Else
'code to run if file exists
End If

The trick is that Dir(MyFile) returns the file name if the file exists,
or a zero-length string if not.

HTH,
Nikos

I have created some code to create and save a word doc to a specfic
location on my server. Is there an easy way to check to see if the
document already exists before the "AppWord.ActiveDocument.SaveAs"
code is run. Have copied sections of the code which outline the folder
loction (myPath) the folder name (myFolder) and the last section of
code which give the word doc its name and save it to the location. At
the moment if the word doc exists it just overwrites it.

myFolder = Me![txtRef] & "_" & Me![txtCustomerName]
myPath = "\\server\LossFiles\"
AppWord.ActiveDocument.SaveAs myPath & "\" & myFolder & "\" &
Me.txtJobNumber & "_" & "ils"

Thanks Inadvance

Matt
 
G

Guest

Thanks for your input

I maybe wrong but this idea does not seem to work because we have used this
line first

MyFile = myPath & "\" & myFolder & "\" & Me.txtJobNumber & "_ils.doc"

The ELSE bit is always run because the MyFile variable always holds a value.
I need something that will check for the actual existance of a word doc.

If Dir(MyFile)<> "" Then
AppWord.ActiveDocument.SaveAs MyFile
Else
code to run if file exists
End If

Cheers


Matt


Kevin K. Sullivan said:
Make sure that you account for implicit file extensions! I'm guessing
that Word is adding .doc onto your file name during SaveAs.

If so, use Nikos's code with but add the extension

MyFile = myPath & "\" & myFolder & "\" & Me.txtJobNumber & "_ils.doc"

Dir("C:\a") will not detect the file C:\a.doc


HTH,

Kevin
myFolder = Me![txtRef] & "_" & Me![txtCustomerName]
myPath = "\\server\LossFiles\"

MyFile = myPath & "\" & myFolder & "\" & Me.txtJobNumber & "_" & "ils"


If Dir(MyFile)<> "" Then
AppWord.ActiveDocument.SaveAs MyFile
Else
'code to run if file exists
End If

Nikos said:
Matt,

try this:

myFolder = Me![txtRef] & "_" & Me![txtCustomerName]
myPath = "\\server\LossFiles\"

MyFile = myPath & "\" & myFolder & "\" & Me.txtJobNumber & "_" & "ils"

If Dir(MyFile)<> "" Then
AppWord.ActiveDocument.SaveAs MyFile
Else
'code to run if file exists
End If

The trick is that Dir(MyFile) returns the file name if the file exists,
or a zero-length string if not.

HTH,
Nikos

I have created some code to create and save a word doc to a specfic
location on my server. Is there an easy way to check to see if the
document already exists before the "AppWord.ActiveDocument.SaveAs"
code is run. Have copied sections of the code which outline the folder
loction (myPath) the folder name (myFolder) and the last section of
code which give the word doc its name and save it to the location. At
the moment if the word doc exists it just overwrites it.

myFolder = Me![txtRef] & "_" & Me![txtCustomerName]
myPath = "\\server\LossFiles\"
AppWord.ActiveDocument.SaveAs myPath & "\" & myFolder & "\" &
Me.txtJobNumber & "_" & "ils"

Thanks Inadvance

Matt
 
N

Nikos Yannacopoulos

Matt,

I've just noticed that I had the two cases the wrong way around in the
proposed code, sorry about this!
Dir(MyFile) returns the name of the file if the file exists, while it
returns a zero-length string ("") if it does not... so, the code should be:

If Dir(MyFile)<> "" Then
'code to run if file exists
Else
AppWord.ActiveDocument.SaveAs MyFile
End If

The reason you always got the Else part running is most likely because
the filename wasn't assigned correctly to MyFile, so it never found the
file regardless of whether it existed or not. To check the value
assigned to MyFile, precede the If statement with a line:

Debug.Print MyFile

which will print the value of the variable in the immediate window
(Ctrl+G to open if not already open). Chances are you won't see what you
expect!

Nikos

Thanks for your input

I maybe wrong but this idea does not seem to work because we have used this
line first

MyFile = myPath & "\" & myFolder & "\" & Me.txtJobNumber & "_ils.doc"

The ELSE bit is always run because the MyFile variable always holds a value.
I need something that will check for the actual existance of a word doc.

If Dir(MyFile)<> "" Then
AppWord.ActiveDocument.SaveAs MyFile
Else
code to run if file exists
End If

Cheers


Matt


:

Make sure that you account for implicit file extensions! I'm guessing
that Word is adding .doc onto your file name during SaveAs.

If so, use Nikos's code with but add the extension

MyFile = myPath & "\" & myFolder & "\" & Me.txtJobNumber & "_ils.doc"

Dir("C:\a") will not detect the file C:\a.doc


HTH,

Kevin
myFolder = Me![txtRef] & "_" & Me![txtCustomerName]
myPath = "\\server\LossFiles\"

MyFile = myPath & "\" & myFolder & "\" & Me.txtJobNumber & "_" & "ils"


If Dir(MyFile)<> "" Then
AppWord.ActiveDocument.SaveAs MyFile
Else
'code to run if file exists
End If

Nikos said:
Matt,

try this:

myFolder = Me![txtRef] & "_" & Me![txtCustomerName]
myPath = "\\server\LossFiles\"

MyFile = myPath & "\" & myFolder & "\" & Me.txtJobNumber & "_" & "ils"

If Dir(MyFile)<> "" Then
AppWord.ActiveDocument.SaveAs MyFile
Else
'code to run if file exists
End If

The trick is that Dir(MyFile) returns the file name if the file exists,
or a zero-length string if not.

HTH,
Nikos


Matt wrote:


I have created some code to create and save a word doc to a specfic
location on my server. Is there an easy way to check to see if the
document already exists before the "AppWord.ActiveDocument.SaveAs"
code is run. Have copied sections of the code which outline the folder
loction (myPath) the folder name (myFolder) and the last section of
code which give the word doc its name and save it to the location. At
the moment if the word doc exists it just overwrites it.

myFolder = Me![txtRef] & "_" & Me![txtCustomerName]
myPath = "\\server\LossFiles\"
AppWord.ActiveDocument.SaveAs myPath & "\" & myFolder & "\" &
Me.txtJobNumber & "_" & "ils"

Thanks Inadvance

Matt
 

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