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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open Word document IF there

Status
Not open for further replies.

bubba100

Technical User
Nov 16, 2001
493
US
The following clip of code is OnOpen of a Word document. When opened it will update links to several Templates and documents. The links don't need to repeatedly updated, they list personal information for the individual using that particular machine. The code works fine WHEN the Template/Document is loaded on the machine. When not it errors.
I am sure there is an "easy" way to write if the individual file is not there to move next. I would rather not use "On Error Resume Next". You would think that it was Monday with the blank my mind is drawing.

Sub AutoOpen()
' MacroUpdateLinks Macro

Dim TrkStatus As Boolean
Dim oRange As Object
ChangeFileOpenDirectory "C:\SpecialTax\"
Documents.Open FileName:="""IFTA Follow Up.dot"""
With ActiveDocument
TrkStatus = .TrackRevisions
.TrackRevisions = False
For Each oRange In .StoryRanges
Do
oRange.Fields.Update
Set oRange = oRange.NextStoryRange
Loop Until oRange Is Nothing
Next
.TrackRevisions = TrkStatus
End With
ActiveDocument.Close SaveChanges:=wdSaveChanges

 
bubba100,

Consider using FileSystemObject from Microsoft Srcipting Runtime (scrrun.dll). There are tons of examples on this object at this and VB6 forums.

vladk
 
No need for an expensive FSO, just use something like this ...

Code:
Function FileExists(docFullName As String) As Boolean
    FileExists = False
    If Len(Dir(docFullName)) <> 0 Then FileExists = True
End Function

Function IsDocOpen(docName As String) As Boolean
    On Error Resume Next
    IsDocOpen = Len(Documents(docName).Name)
End Function

Sub testBooleanFx()
    MsgBox FileExists("C:\Document1.doc") & vbNewLine & IsDocOpen("Docuemnt1.doc")
End Sub

-----------
Regards,
Zack Barresse
 
Thanks, I will take a look at both. I have never used MS scripting before.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top