如何通过VBA在MS Word中定位命令按钮

时间:2015-06-28 19:28:26

标签: vba ms-word word-vba

我目前有一个VBA脚本,除了MS word documant中命令按钮的位置外,它应该正常工作。目前,按钮位于文档的第一位,将现有文本推向右侧。

我用于按钮的VBA代码是:

Dim doc As Word.Document
Dim shp As Word.InlineShape
Set doc = ActiveDocument

Set shp = doc.Content.InlineShapes.AddOLEControl(ClassType:="Forms.CommandButton.1")
shp.OLEFormat.Object.Caption = "Create PDF and print"

如何定位按钮?在同一条线上,但居中会做得很好。中心但在文档的最后(在键入的字母后面),甚至更好。

谢谢。

1 个答案:

答案 0 :(得分:1)

您必须将按钮添加到文档的特定段落。例如:

doc.Content.InsertParagraphAfter
Set shp = doc.Content.InlineShapes.AddOLEControl(ClassType:="Forms.CommandButton.1", _
    Range:=doc.Paragraphs.Last.Range)

因此,您可以根据需要格式化按钮段落。例如:

doc.Paragraphs.Last.Alignment = wdAlignParagraphCenter