Open Word document if exist, otherwise create one

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I've tried the following with limited success. It seems as if there is a
timing issue or something that causes it not to work as desired. Using the
first example, sometimes the file exists but won't open and causes an error
whcih causes the file to be recreated.

On Error GoTo 100
Application.FollowHyperlink "G:\New\" & recordid & ".doc"
Exit Sub
100 DoCmd.OutputTo acOutputTable, "Supporting Document", acFormatHTML,
"G:\New\" & recordid & ".doc", True

This second attempt sees the file sometimes but gives an error that it can
not open it. While it doesn't overwrite the original file, I still need the
file to open if there. Here's the code:

ChkName = "G:\New\ & recordid & ".doc"
If Dir(ChkName,vbNormal) = "" Then
DoCmd.OutputTo acOutputTable, "Supporting Document", acFormatHTML,chkNAme,
True
Else
Application.FollowHyperlink ChkName
End If

What am I missing?
 
Try

ChkName = "G:\New\" & recordid & ".doc"
If Len(Dir(ChkName)) > 0 Then
' The File Exist
Application.FollowHyperlink ChkName
Else
'File Does Not Already Exist
DoCmd.OutputTo acOutputTable, "Supporting Document",
cFormatHTML,chkNAme, True
End If

You had a missing "
I have tested it successfully at my end and the code works fine for me.
 
Thanks Daniel, the missing " was a typo when I moved the code to here. It is
correct in my Access VB code.

I'll try using Len and see if that helps. However, my code using = "" would
work correctly when I added MsgBoxes where you have ' The File Exist and
'File Does Not Already Exist. the problem is that the FollowHyperlink somehow
timed out and I got an error message saying the file could not be opened.

I'll let you know if the Len changes anything.

Thanks
 
OK, after changing the code to Len and trying it that way and adding MsgBoxes
so that I can see if the file exists or not, the code will not follow any
hyperlinks. It does see the file using the len function because it doesn't go
to "Else" and create a new one. The error, I get now is:

Run-Time Error '165388':
The hyperlink cannot be followed to the destination.

An example of my filename and path are as follows:
ChkName = "G:\Joe & Tom - Brown\letters\" & recordid & ".doc"

Is there a problem with either an & or an - in a hyperlink?

The path, directory and file does exist.

Thanks
 
Back
Top