以标题名称作为书签名称,以编程方式在Word 2010中生成书签 [英] Generate bookmarks in Word 2010 programmatically, with the header name as the bookmark name

查看:388
本文介绍了以标题名称作为书签名称,以编程方式在Word 2010中生成书签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Word 2010中以编程方式生成书签,并将标头名称作为书签名称.

I need to generate bookmarks in Word 2010 programmatically, with the header name as the bookmark name.

我有以下代码使单词成为书签,但是书签名称与字符串Heading 1相同,仅在名称变量中可用:

I have the following code which makes a word a bookmark, but the bookmark name remains the same as the string Heading 1 is only available in the name variable:

Sub bookmarking()
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    With ActiveDocument.Bookmarks
        .Add Range:=Selection.Range, Name:=" Heading 1"
        .DefaultSorting = wdSortByName
        .ShowHidden = False
    End With
End Sub

我想要剪贴板中的内容,而不是name变量中的Heading 1.请帮助我用剪贴板内容替换Heading 1.

Instead of the Heading 1 in the name variable, I want content from the clipboard. Please help me replace that Heading 1 with clipboard content.

推荐答案

使用Microsoft Forms 2.0 Object Library中的DataObject:

Private Function GetClipboardData()
    Dim objDataObject As MSForms.DataObject ''need to add reference in Tools |References
    Set objDataObject = New MSForms.DataObject

    objDataObject.GetFromClipboard
    On Error Resume Next
    GetClipboardData = objDataObject.GetText
    If Err.Number = -2147221404 Then
       MsgBox "Error: current clipboard data is either empty or is not text. Clibpoard must contain text."
    End If
End Function

然后,返回您的主代码,使书签名称为剪贴板数据:

Then, back your main code, have the bookmark name be this clipboard data:

...
.Add Range:=Selection.Range, Name:=GetClipboardData()
...

这对您来说是一个好的开始吗?根据您的需求,还有其他一些方法可能会更可靠.但是,这应该是一个很好的概念证明.

Is this a good start for you? There are other ways which may be more robust depending on your needs. However this should serve as good proof-of-concept.

这篇关于以标题名称作为书签名称,以编程方式在Word 2010中生成书签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆