My aim is to create an array of various global templates which I define in a table. My macro reads the table. If the name specifies an available template my array should hold the object. If the template can't be found the array should hold the name that couldn't be processed. ThisDocument is of docm type. It's assigned to Sfs(0) as the default. Here is an excerpt from my code.
Private Sub TestSetSfs()
Dim Sfs() As Variant
SetSfs Sfs
Debug.Print Sfs(0).Name
Debug.Print VarType(Sfs(0)) ' returns vbString
End Sub
Function SetSfs(Sfs() As Variant) As Long
Dim Tbl As Table
ReDim Sfs(20) ' max 20 references
Set Sfs(0) = ThisDocument
Debug.Print Sfs(0).Bookmarks.Count ' works as expected
Debug.Print VarType(Sfs(0)) ' returns vbString
Debug.Print GetTextTbl(Tbl, Sfs(0), "SomeName")
End Function
Function GetTextTbl(Tbl As Table, Doc As Document, Tn As String) As Boolean
GetTextTbl = True
End Function
My problem is with the line Set Sfs(0) = ThisDocument. In the next line of the test procedure Sfs(0) works correctly as an object, permitting a Bookmarks.Count. It's also shown as an object in the Locals window. However, the function GetTextTbl rejects it as "ByRef argument type mismatch" which it doesn't do when replaced with ThisDocument in the function call. This prompted me to look at its VarType which returns VbString instead of vbObject.
Let me add that I already have this idea working in Excel (with addins) but I need an equivalent in Word where I think the global template would meet my requirements if I could assign objects to the Sfs() variant. Any idea how to do that?
Debug.Print Sfs(0).Bookmarks.Count
- and that should prove that it's assigning the document object as you want. – braXFunction GetTextTbl(Tbl As Table, Doc As Variant, Tn As String) As Boolean
and it will work. That doesnt exactly answer your question, but it works if you putMsgBox Doc.Bookmarks.Count
in yourGetTextTbl()
function. – braXObject
instead ofVariant
-Dim Sfs() As Object
andFunction SetSfs(Sfs() As Object) As Long
- then you can useFunction GetTextTbl(Tbl As Table, Doc As Document, Tn As String) As Boolean
– braX