使用 VBA 将文本添加到文档中的特定文本框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18957072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add Text to Specific Text Box in a Document Using VBA
提问by user2411110
Is there a way to name text boxes in MS Word so i can call them in vba and add text to them?For Example,I have a User Form that gets some data from user.after some calculation on this data,i want to show the result on the document in text box or equation... Like,In visual basic we had :
有没有办法在 MS Word 中命名文本框,以便我可以在 vba 中调用它们并向它们添加文本?例如,我有一个用户表单,可以从用户那里获取一些数据。在对这些数据进行一些计算之后,我想显示文本框或方程中文档的结果......就像,在visual basic中我们有:
textbox1.text="My Text Goes Here"
textbox1.text="My Text Goes Here"
Do we have something like that in VBA? Like :
我们在 VBA 中有类似的东西吗?喜欢 :
ActiveDocument.textboxname.text="My Text"
?!!
ActiveDocument.textboxname.text="My Text"
?!!
i dont want to use activeX Controls for this.
我不想为此使用 ActiveX 控件。
回答by David Zemens
In Word and PowerPoint, you can only assign a name to a shape using VBA (in contrast to Excel where you can do this in the formula bar).
在 Word 和 PowerPoint 中,您只能使用 VBA 为形状指定名称(与 Excel 不同,您可以在编辑栏中执行此操作)。
Word does not force shapes to have unique names, so it is possible to have two shapes both named Text Box 2
. You can refer to shapes also by their index position in the ActiveDocument.Shapes
collection.
Word 不会强制形状具有唯一的名称,因此可能有两个形状都命名为Text Box 2
。您也可以通过它们在ActiveDocument.Shapes
集合中的索引位置来引用形状。
Once you identify what Shape
you need to work with, then you can simply manipulate the .TextFrame.TextRange.Text
property:
一旦确定了Shape
需要处理的内容,就可以简单地操作该.TextFrame.TextRange.Text
属性:
Sub Test()
Dim shp As Shape
Dim str As String
For Each shp In ActiveDocument.Shapes
str = "My name is " & shp.Name
str = str & vbNewLine & "My EditID is " & shp.EditID
shp.TextFrame.TextRange.Text = str
Next
End Sub
One other thing you might consider is adding an AlternativeText
property to each shape. Of course this doesn't solve the "non-uniqueness" problem, but you can use this (or CustomerData/CustomXMLParts
to assign some metadatato shapes, as a further means of identifying and differentiating them.
您可能会考虑的另一件事是AlternativeText
为每个形状添加一个属性。当然,这并不能解决“非唯一性”问题,但您可以使用它(或CustomerData/CustomXMLParts
将一些元数据分配给形状,作为识别和区分它们的进一步方法。
回答by Dah Sra
There are few possibilities how you could take advantage of CC but, in my opinion, best option is to wrap each verified paragraph into CC.
如何利用 CC 的可能性很小,但在我看来,最好的选择是将每个经过验证的段落都包装到 CC 中。
Dim par As Paragraph
'set reference to appropriate paragraph
Set par = ActiveDocument.Paragraphs(2)
Dim cc As ContentControl
Set cc = ActiveDocument.ContentControls.Add( _
wdContentControlRichText, par.Range)
cc.Tag = "VERIFIED"
'options
'disable deletion of CC
cc.LockContentControl = True
'disable edition of CC
cc.LockContents = True