简体   繁体   English

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

[英]Specifying the location of an inlineshape in MS Word (VBA)

I've been trying to adapt the method shown here: http://support.microsoft.com/kb/246299 so that I can create a command button in word which will save the document and remove itself when clicked. 我一直在尝试调整此处显示的方法: http : //support.microsoft.com/kb/246299,以便我可以用Word创建一个命令按钮,该按钮将保存文档并在单击时将其自身删除。 I've been unable to figure out how to change the position of the button from the default of the top left of the first page however. 我一直无法弄清楚如何从第一页左上角的默认位置更改按钮的位置。 Ideally I'd like the button to be generated at the end of the document and be centre aligned, or otherwise placed at the cursor position. 理想情况下,我希望按钮在文档末尾生成并居中对齐,或者将其放置在光标位置。

Any advice would be very much appreciated :) 任何建议将不胜感激:)

Thank You. 谢谢。

My VB.NET project code so far: 到目前为止,我的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

As long as everything else is working just set the shp.left = 250 and shp.top = 1200 只要其他一切正常,只需将shp.left = 250和shp.top = 1200设置为

etc etc. 等等等

Just like in VB when you place a button. 就像在VB中放置按钮一样。 For more details on the exact calls you should reference this page: http://msdn.microsoft.com/en-us/library/office/hh965406%28v=office.14%29.aspx 有关确切呼叫的更多详细信息,您应该参考以下页面: http : //msdn.microsoft.com/zh-cn/library/office/hh965406%28v=office.14%29.aspx

But say to center a button you can set left to be the (doc.width - shape.width) 但是说要居中放置一个按钮,您可以将其设置为(doc.width-shape.width)

But word buttons allow for much more complex styling and setup. 但是,文字按钮允许更复杂的样式和设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM