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

How to position a command button in MS Word via VBA

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

我用于按钮的 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"

如何放置按钮?在同一条线上但居中就可以了。居中但在文档的最后(在输入字母后),更好。

谢谢。

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

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

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

doc.Paragraphs.Last.Alignment = wdAlignParagraphCenter