来自 Excel 的 MS Word 格式的 SetPlaceholderText

SetPlaceholderText in MS Word Form from Excel

我正在编写 Excel VBA 宏来翻译 内容控件 中的 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。 (我应该提一下,我仅限于使用 Office 2010,因为这是公司仍然拥有的。)

这是我的代码:

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

当我 运行 这样做时,占位符文本(在原始英文形式中类似于 "Enter text")完全消失,没有被预期的占位符文本替换。

我尝试 运行从另一个 Word 文档(而不是来自 Excel)获取代码——即,不是来自表单本身——并且同样的事情发生了。

但是如果我将此代码插入 原始 形式(进行适当的更改,例如将 "frm" 更改为 "ThisDocument"),它可以正常工作。换句话说,当 VBA 模块位于同一 (Word) 文档中时,我可以成功使用 SetPlaceholderText 方法。但我真的很想 运行 来自 Excel 的这个,因为那是我将列出多个翻译的地方。

这是一个 "oddity" 与 SetPlaceholderText 方法。我不记得我曾见过它如此行为的原因,但以下对我有用。

备注:

只有在 打开设计模式时它才对我有用,所以我在示例代码中更改了它。

SetPlaceholderText 仅在 "parent" 文档之外有效,前提是 所有 参数都已指定。传递给它们的值可以是未定义的 (Nothing),但对象模型需要这三个值。

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