Since you're running this from Excel, instead of employing ThisDocument, which isn't where your code will reside, you should not only instantiate Word(e.g. Dim objWd as Object), but also explicitly tell objWd what document to work with - whether by opening that document directly or looping through the open ones to test which (if any) is the document you want, then set a reference to that document (e.g. Set objDoc = objWd.Documents(i)) then pass objDoc to your functions. For example:
Sub Demo()
Dim ObjWd As Object, ObjDoc As Object, StrDocNm As String
Dim bStrt As Boolean, bFound As Boolean, StrNm As String
StrNm = Trim(InputBox(prompt:=promptStr, Default:=defaultStr, Title:="Set name for selected Shape"))
If StrNm = "" Then Exit Sub
'Check whether the document exists
StrDocNm = "C:\Users\" & Environ("Username") & "\Documents\Document Name.doc"
If Dir(StrDocNm) = "" Then
MsgBox "Cannot find the designated document: " & StrDocNm, vbExclamation
Exit Sub
End If
' Test whether Word is already running.
On Error Resume Next
bStrt = False ' Flag to record if we start Word, so we can close it later.
Set ObjWd = GetObject(, "Word.Application")
'Start Word if it isn't running
If ObjWd Is Nothing Then
Set ObjWd = CreateObject("Word.Application")
If ObjWd Is Nothing Then
MsgBox "Can't start Word.", vbExclamation
Exit Sub
End If
' Record that we've started Word, so we can terminate it later.
bStrt = True
End If
On Error GoTo 0
'Check if the document is open.
bFound = False
With ObjWd
'Hide our Word session
If bStrt = True Then .Visible = False
For Each ObjDoc In .Documents
If ObjDoc.FullName = StrDocNm Then ' We already have it open
bFound = True
Exit For
End If
Next
' If not open by the current user.
If bFound = False Then
' Check if another user has it open.
If IsFileLocked(StrDocNm) = True Then
' Report and exit if true
MsgBox "The Word document is in use." & vbCr & "Please try again later.", vbExclamation, "File in use"
If bStrt = True Then .Quit
Exit Sub
End If
' The file is available, so open it.
Set ObjDoc = .Documents.Open(Filename:=StrDocNm)
If ObjDoc Is Nothing Then
MsgBox "Cannot open:" & vbCr & StrDocNm, vbExclamation
If bStrt = True Then .Quit
Exit Sub
End If
End If
With ObjDoc
'Only now can we can process the document!!!
'Your functions can now be called
MsgBox CountShapeTitles(ObjDoc, StrNm)
.Save
'Close the document if we opened it
If bFound = False Then .Close
End With
If bStrt = True Then .Quit
End With
End Sub
Function IsFileLocked(strFileName As String) As Boolean
On Error Resume Next
Open strFileName For Binary Access Read Write Lock Read Write As #1
Close #1
IsFileLocked = Err.Number
Err.Clear
End Function
Private Function CountShapeTitles(ObjDoc As Object, StrNm As String) As Long
Dim i As Long, ObjShp As Object: i = 0
With ObjDoc
For Each ObjShp In .Shapes
If ObjShp.Title = StrNm Then i = i + 1
Next
For Each ObjShp In .InlineShapes
If ObjShp.Title = testName Then i = i + 1
Next
End With
CountShapeTitles = i
End Function
Personally, though, I'd use early binding as it's much faster than late binding.
Bookmarks