I had a similar problem as the user in this question.
Right now I cannot get my function variable proDocName which is type "DocumentProperty" to be set to wdDoc.BuiltinDocumentProperties.Item(1) in my Excel VBA below. It throws a type mismatch error.
The Excel VBA subroutine should first open an instance of word and then open a Word Document file.
Then I pass ByRef the file (I call it wdDoc in my code) to the function where I then try and grab the document properties' values and names which I want to write to my excel sheet using the function.
I've tested the following code in Word VBA which works fine:
Sub test()
Dim wdApp As Word.Application
Dim wdDocPro As DocumentProperty
Set wdApp = GetObject(, "Word.Application")
Set wdDoc = wdApp.ActiveDocument
Set wdDocPro = wdDoc.BuiltInDocumentProperties.Item(1) ' I get a type mismatch on this line
For Each wdDocPro In wdDoc.BuiltInDocumentProperties
MsgBox (wdDocPro.Name & " , " & wdDocPro.Value)
Next wdDocPro
End Sub
This test allowed me to see the "wdDoc"s document properties so I know it must be a problem with either 1.) the Excel versus Word object model for DocumentProperty or 2.) the function is somehow losing access to the Word.Document BuiltinDocumentProperties collection
This code in excel vba has issues when I get to the line where I set wdDocPro to the BuiltinDocumentProperties item.
Public Sub GetCurrentFolderConstants()
Dim DocVariables() As String
Dim wdApp as Word.Application
Dim wdDoc as Word.Document
Set wdApp = GetObject(, "Word.Application")
Set wdDoc = wdApp.ActiveDocument
DocVariables = DocVarGrabbing(wdDoc)
'Do stuff
wdDoc.Close True
'Do stuff with DocVariables
wdApp.Quit SaveChanges:=False
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function DocVarGrabbing(ByRef wdDoc As Word.Document) As String()
Dim wdDocPro As DocumentProperty 'Problem variable
'Set wdApp = GetObject(, "Word.Application")
'Set wdDoc = wdApp.ActiveDocument
'MsgBox wdDoc.Name
If wdDoc.BuiltinDocumentProperties.Count > 0 Then
Set wdDocPro = wdDoc.BuiltinDocumentProperties.Item(1)'I get a type mismatch at this line
For Each wdDocPro In ActiveDocument.BuiltinDocumentProperties
'Do stuff with Document properties Names and Values and assign to DocVarArray (not needed for debugging)
Next wdDocPro
End If
DocVarGrabbing = DocVarArray
Set wdDocPro = Nothing
End Function