Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / March 2005

Tip: Looking for answers? Try searching our database.

Checking if a word doc exists

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Matt - 01 Mar 2005 12:17 GMT
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
Nikos Yannacopoulos - 01 Mar 2005 13:01 GMT
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
[quoted text clipped - 12 lines]
>
> Matt
Kevin K. Sullivan - 01 Mar 2005 22:19 GMT
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

> Matt,
>
[quoted text clipped - 33 lines]
>>
>> Matt
Matt - 02 Mar 2005 11:47 GMT
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.
[quoted text clipped - 58 lines]
> >>
> >> Matt
Nikos Yannacopoulos - 02 Mar 2005 12:34 GMT
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
>
[quoted text clipped - 77 lines]
>>>>
>>>>Matt
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.