Microsoft Office Tutorials and References
In Depth Information
Activating the Word Application
For demonstration purposes, say you maintain a Word document named myWordDoc.docx in the
folder path C:\Your\File\Path\. Before you attempt to open the document, check the directory to
make sure it resides in the expected folder path. If the Word document is not where your macro
thinks it should be, you’ll want to exit the macro with a Message Box informing the user why the
process could not be completed.
Finally, your macro will need to keep its eyes on the prize: the Word document that might already
be open if Word was already open. A lot to remember but that’s what macros are for…tell them once
and they do what they’re told. Here is the complete modification of the ActivateWord macro that
wraps it all up into a single package:
Sub ActivateWord()
‘Declare Object variables for the Word application and document.
Dim WdApp As Object, wddoc As Object
‘Declare a String variable for the example document’s
‘name and folder path.
Dim strDocName As String
‘On Error statement if Word is not already open.
On Error Resume Next
‘Activate Word if it is already open.
Set WdApp = GetObject(, “Word.Application”)
If Err.Number = 429 Then
Err.Clear
‘Create a Word application if Word is not already open.
Set WdApp = CreateObject(“Word.Application”)
End If
‘Make sure the Word application is visible.
WdApp.Visible = True
‘Define the strDocName String variable.
strDocName = “C:\Your\File\Path\myWordDoc.docx”
‘Check the directory for the presence of the document
‘name in the folder path.
‘If it is not recognized, inform the user of that
‘fact and exit the macro.
If Dir(strDocName) = “” Then
MsgBox “The file myWordDoc.docx” & vbCrLf & _
“was not found in the folder path” & vbCrLf & _
“C:\Your\File\Path\.”, _
vbExclamation, _
“Sorry, that document name does not exist.”
Exit Sub
End If
‘Activate the Word application.
WdApp.Activate
‘Set the Object variable for the Word document’s full
‘name and folder path.
Set wddoc = WdApp.Documents(strDocName)
‘If the Word document is not already open, then open it.
If wddoc Is Nothing Then Set wddoc = WdApp.Documents.Open(strDocName)
Search JabSto ::




Custom Search