使用 VBA 将超链接添加到 Word 内容控件中
我有一个 Word 2010 模板.使用时,会向用户显示一个表单,并提示他们在名为 BugNumTextBox 的文本框中输入字符串.根据命令,我希望构造一个 URL,创建一个超链接,并将其插入到文档中.
I have a Word 2010 template. When used, a form is presented to the user and they are prompted to enter a string in a textbox named BugNumTextBox. Upon command, I wish to construct a URL, create a hyperlink, and insert that into the document.
所需的插入点(锚点)是一个标签名为foo"的富文本内容控件.对于我尝试过的 address 参数:
The desired insertion point (anchor) is a Rich Text Content Control with a tag name of "foo". For the address parameter I have attempted:
1) 指定内容控件的范围
2) 指定我在内容控件内创建的书签(名为 BugNum)的范围.
1) To specify the range of the content control
2) To specify the range of a bookmark (named BugNum) that I created inside of the content control.
两者都不起作用,导致运行时错误.
谁能建议一种方法来实现这一目标?
以下是上述方法 2 的非工作示例.
Neither have worked, resulting in run-time errors.
Can anyone suggest a method to achieve this?
Below is a non-working example of method 2 above.
ActiveDocument.Hyperlinks.Add Anchor:=.Bookmark("BugNum").Range, Address:= _
"http://bugs.fubar.net/show_bug.cgi?id=" & BugNumTextBox, _
SubAddress:="", ScreenTip:="", TextToDisplay:=""
非常感谢.
为什么不访问 ContentControls 对象集合.
类似的东西:
Why don't you access the ContentControls Object Collection instead.
Something like:
Dim ccRtxt As ContentControl
Set ccRtxt = ActiveDocument.ContentControls(1)
ccRtxt.Range.Hyperlinks.Add ccRtxt.Range, _
"http://*.com/questions/26538906/" & _
"add-hyperlink-into-word-content-control-with-vba"
以上适用于您描述的只有 1 个 ContentControl 的场景.
如果你有很多这样的对象,你可以这样做:
Above will work on the scenario you describe where you only have 1 ContentControl.
If you have many such objects, you can do this:
Dim ccRtxt As ContentControl
For Each ccRtxt In ActiveDocument.ContentControls
If ccRtxt.Tag = "foo" Then
ccRtxt.Range.Hyperlinks.Add ccRtxt.Range, _
"http://*.com/questions/26538906/" & _
"add-hyperlink-into-word-content-control-with-vba"
End If
Next
这会将此问题作为超链接地址添加到您的富文本内容控件中.
您可以调整地址属性以满足您的需要.哈.
This adds this question as hyperlink address in your Rich Text Content Control.
You can adjust the address property to suit your needs. HTH.