【问题标题】:SetPlaceholderText in MS Word Form from Excel来自 Excel 的 MS Word 表单中的 SetPlaceholderText
【发布时间】:2019-05-05 15:29:25
【问题描述】:

我正在编写一个 Excel VBA 宏来将 MS Word Form 翻译成各种语言。原始的英语短语列在 A 列中,相应的翻译列在列 B、C 等中。在表格的文本部分中用英语替换翻译没有问题,但我在 内容控制。 (我应该提一下,我仅限于使用 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

当我运行它时,占位符文本(在原始英文形式中类似于“输入文本”)完全消失,没有被预期的占位符文本替换。

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

但是,如果我在 original 表单中插入此代码(进行适当的更改,例如将“frm”更改为“ThisDocument”),它可以正常工作。换句话说,当 VBA 模块在同一个(Word)文档中时,我可以成功使用 SetPlaceholderText 方法。但我真的很想从 Excel 中运行它,因为我将在其中列出多个翻译。

【问题讨论】:

    标签: excel vba ms-word word-contentcontrol


    【解决方案1】:

    这是 SetPlaceholderText 方法的“奇怪”。我不记得我曾经看到过它的行为原因,但以下内容对我有用。

    备注:

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

    SetPlaceholderText 仅在 all 指定参数的情况下在“父”文档之外工作。传递给它们的值可以是未定义的 (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
    

    【讨论】:

    • 非常感谢辛迪。我已经尝试过包含“Nothing”(使用替代语法:cc.SetPlaceholderText Nothing,Nothing,“phldr 1”),但我从未想过设计模式需要关闭!现在很好用,再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-07
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    相关资源
    最近更新 更多