Microsoft Office Tutorials and References
In Depth Information
Try It
Declare a String type variable for the anticipated InputBox entry:
Dim myFileName As String
Show the InputBox to ask the user for the name of the Word document to be opened from
the predetermined folder path:
myFileName = InputBox _
(“Enter the full Word document name to be opened” & Chr(10) & _
“from the folder path “ & myPath & “:”, _
“What file name with extension do you wish to open?”, _
Exit the macro if nothing is entered or if the Cancel button is clicked:
If myFileName = “” Then Exit Sub
Declare a String type variable for the combined folder path and document name:
Dim myDocName As String
Define the String type variable for the combined folder path and document name:
myDocName = myPath & myFileName
Check to see if the Word document name exists in the folder path. If it does not, advise the
user and exit the macro.
If Dir(myDocName) = “” Then
MsgBox “The file “ & myFileName & vbCrLf & _
“was not found in the folder path” & vbCrLf & _
myPath & “.”, _
vbExclamation, _
“No such animal.”
Exit Sub
End If
At this point, the Word document is determined to exist in the folder. Declare Object
variables for the Word application and the Word document:
Dim appWord As Object, wdDoc As Object
Using late binding in this example, create a Word application:
Set appWord = CreateObject(“Word.Application”)
Make the created Word application visible:
appWord.Visible = True
Open the requested Word document name:
Set wdDoc = appWord.Documents.Open(myDocName)
Search JabSto ::

Custom Search