Search code examples
excelvbams-wordword-contentcontrol

SetPlaceholderText in MS Word Form from Excel


I'm writing an Excel VBA macro to translate an MS Word Form into various languages. The original English phrases are listed in column A, and the corresponding translations are listed in cols B, C, etc. No problem substituting translation for English in the text parts of the form, but I'm struggling with changing the Placeholder text in the content controls. (I should mention I'm confined to using Office 2010, as that's what are company's still got.)

Here's my code:

Dim frm As Word.Document, cc As ContentControl
Set frm = Documents.Open("C:\[document]", False)

If frm.FormsDesign = False Then 'make sure doc is in Design Mode
  frm.ToggleFormsDesign
End If

For Each cc In frm.ContentControls
 If cc.Type = 1 Then    'this is for textboxes
   cc.SetPlaceholderText , , "phldr 1"
 Else  'this is for all other controls:  eg, drop-downs
  cc.SetPlaceholderText , , "phldr 2"
 End If
Next cc

When I run this, the placeholder text (which in the original English form is something like "Enter text") disappears entirely, without being replaced with the intended placeholder text.

I tried running the code from another Word document (rather from Excel)--ie, not from the form itself--and the same thing happens.

But if I insert this code in the original form (with appropriate changes, eg changing "frm" to "ThisDocument"), it works fine. In other words, I can use the SetPlaceholderText method successfully when the VBA module is within the same (Word) document. But I really want to run this from Excel, because that's where I'll be listing multiple translations.


Solution

  • This is an "oddity" with the SetPlaceholderText method. I don't recall that I've ever seen a reason why it behaves as it does, but the following works for me.

    Remarks:

    It only works for me if Design Mode is not on, so I changed that in the sample code.

    SetPlaceholderText only works outside the "parent" document if all the parameters are specified. The value passed to them can be undefined (Nothing), but the object model wants all three.

    If doc.FormsDesign = True Then 'make sure doc is NOT in Design Mode
      doc.ToggleFormsDesign
    End If
    
    For Each cc In doc.Contentcontrols
     If cc.Type = 1 Then    'this is for textboxes
       cc.SetPlaceholderText Range:=Nothing, BuildingBlock:=Nothing, Text:="phldr 1"
     Else  'this is for all other controls:  eg, drop-downs
      cc.SetPlaceholderText Range:=Nothing, BuildingBlock:=Nothing, Text:="phldr 2"
     End If
    Next cc