在MS Word(VBA)中指定inlines形状的位置

时间:2013-07-08 17:13:55

标签: vb.net vba ms-word

我一直在尝试调整此处显示的方法:http://support.microsoft.com/kb/246299以便我可以在word中创建一个命令按钮,该按钮将保存文档并在单击时删除自身。我一直无法弄清楚如何从第一页左上角的默认值更改按钮的位置。理想情况下,我希望在文档末尾生成按钮并居中对齐,或以其他方式放置在光标位置。

非常感谢任何建议:)

谢谢。

到目前为止我的VB.NET项目代码:

Dim shp As Word.InlineShape

shp = wrdDoc.Content.InlineShapes.AddOLEControl(ClassType:="Forms.CommandButton.1")

shp.OLEFormat.Object.Caption = "Save To Disk"

shp.Width = "100"

'Add a procedure for the click event of the inlineshape

Dim sCode As String

sCode = "Private Sub " & shp.OLEFormat.Object.Name & "_Click()" & vbCrLf & _

"ActiveDocument.SaveAs(""" & sOutFile & """)" & vbCrLf & _

"On Error GoTo NoSave" & vbCrLf & _

"MsgBox ""Document Saved Successfully""" & vbCrLf & _

"Dim o As Object" & vbCrLf & _

"For Each o In ActiveDocument.InlineShapes" & vbCrLf & _

"If o.OLEFormat.Object.Name = ""CommandButton1"" Then" & vbCrLf & _

"o.Delete" & vbCrLf & _

"End If" & vbCrLf & _

"Next" & vbCrLf & _

"Exit Sub" & vbCrLf & _

"NoSave:" & vbCrLf & _

"MsgBox ""Document Failed To Save""" & vbCrLf & _

"End Sub"

wrdDoc.VBProject.VBComponents("ThisDocument").CodeModule.AddFromString(sCode)

wrdApp.Visible = True

1 个答案:

答案 0 :(得分:0)

只要其他一切正常,只需设置shp.left = 250和shp.top = 1200

等等。

就像在VB中放置按钮一样。有关确切调用的详细信息,请参阅此页面:http://msdn.microsoft.com/en-us/library/office/hh965406%28v=office.14%29.aspx

但是说要设置一个按钮,你可以将其设置为(doc.width - shape.width)

但是字按钮允许更复杂的样式和设置。