Hi,
Thanks for taking the time to look at this. After spending the better part of a day on it, its time for me to call the calvary.
I'm working with a procedure that resets a Content Control to its placeholder text with the following code:
Public Function DepopulatedCc(ByVal PopulatedCc As Object) As Object ' Word.ContentControl
#If LateBinding Then
Dim oWdApp As Object
Dim oWdCc As Object
#Else
Dim oWdApp As Word.Application ' Requires a reference: Microsoft Word 14.0 Object Library
Dim oWdCc As Word.ContentControl ' Requires a reference: Microsoft Word 14.0 Object Library
#End If
On Error Resume Next
Set oWdApp = GetObject(, "Word.Application")
If Err Then
' Word is not running.
GoTo SingleExit
End If
On Error GoTo 0
Dim bLockContents As Boolean
Dim bLockContentControl As Boolean
Dim sPlaceHolderText As String
Set oWdCc = PopulatedCc
With oWdCc
' Grab the existing property values to be restored later.
bLockContents = .LockContents
bLockContentControl = .LockContentControl
' We need the Content Control and its Contents to be changeable.
.LockContents = False
.LockContentControl = False
Select Case .Type
Case wdContentControlText
sPlaceHolderText = .PlaceholderText
.Range.Text = vbNullString
.SetPlaceholderText , , sPlaceHolderText ' Error here: Run-time error '13', Type mismatch
Case wdContentControlRichText
sPlaceHolderText = .PlaceholderText
.Range.Text = vbNullString
.SetPlaceholderText , , sPlaceHolderText
Case wdContentControlComboBox
sPlaceHolderText = .DropdownListEntries(1).Text
.DropdownListEntries(1).Select ' Don't know if this is working yet
.SetPlaceholderText , , sPlaceHolderText
Case wdContentControlDropdownList
sPlaceHolderText = .DropdownListEntries(1).Text
.DropdownListEntries(1).Select
.SetPlaceholderText , , sPlaceHolderText
Case wdContentControlBuildingBlockGallery
MsgBox "This is a Building Block Gallery Content Control. " & vbCrLf _
& "The Application cannot reset this type of control (yet)."
Case wdContentControlDate
MsgBox "This is a Date Content Control. " & vbCrLf _
& "The Application cannot reset this type of control (yet)."
Case wdContentControlGroup
MsgBox "This is a Content Control Group. " & vbCrLf _
& "The Application cannot reset this type of control (yet)."
Case wdContentControlCheckBox
.Checked = False
End Select
' Restore the properties to their original values.
.LockContents = bLockContents
.LockContentControl = bLockContentControl
End With
Set DepopulatedCc = oWdCc
SingleExit:
Set oWdCc = Nothing
Set oWdApp = Nothing
End Function
The code that calls this function assembles a collection of Content Controls in a document or the selected portion of a document and then runs through that collection to remove applied content and redisplay the PlaceholderText.
If LateBinding = True, I'm getting "Run-time error '13' -- Type mismatch" error at the statement indicated.
When the error is occurring I've used the Intermediate Window to diagnose the conditions as follows:
Debug.Print TypeName(oWdCc)
ContentControl
Debug.Print TypeName(PopulatedCc)
ContentControl
Debug.Print TypeName(wdContentControlText)
Long
Debug.Print wdContentControlText
1
Debug.Print TypeName(sPlaceHolderText)
String
Debug.Print sPlaceHolderText
DocumentAuthorPrincipal
If LateBinding = False, the code does not produce and error, and the results in the Intermediate Window are exactly the same as those produced with LateBinding = True. The results in the Intermediate Window are exactly what I intended for them to be and
seem to be as required for the SetPlaceholderText method. So I'm suspecting ignorance on my part or a blunder to which I am blind.
I've used MZ-Tools to Clean the code and rebooted the machine. Just to be sure to be sure and I get the same results.
I've included a compiler instruction at the beginning of the module to facilitate switching between Early and Late Binding as follows:
#Const LateBinding = False
The code does work with Early Binding; but does not work with Late Binding. I confirmed that the values for the variables I thought would be important by including a Stop before the error-prone statement and repeating the diagnostic Debug.Print statements
described above and got the same results.
Additionally, you may need to know that I have another module containing the following:
#If LateBinding Then
Public Enum WdContentControlType
wdContentControlRichText = 0 ' Word.WdContentControlType
wdContentControlText = 1 ' Word.WdContentControlType
wdContentControlPicture = 2 ' Word.WdContentControlType
wdContentControlComboBox = 3 ' Word.WdContentControlType
wdContentControlDropdownList = 4 ' Word.WdContentControlType
wdContentControlBuildingBlockGallery = 5 ' Word.WdContentControlType
wdContentControlDate = 6 ' Word.WdContentControlType
wdContentControlGroup = 7 ' Word.WdContentControlType
wdContentControlCheckBox = 8 ' Word.WdContentControlType
End Enum
#End If
I'd like to use Late Binding when distributing the code to users on various versions of Word 2007+ and because I include the same modules in my Personal.xlsb and Excel add-ins. I hope that this saves me a lot of code management trouble for my applications
that control Word from Excel or control Excel from Word; but, there are times when I wonder it it is a good strategy. I will say that I've been learning a lot to make it work and it has been working for me -- at least most of the time.
So, wassup doc?
Bruce