首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >让MS-Word vba ActiveDocument.Paragraphs(2).Select只选择第1段?

让MS-Word vba ActiveDocument.Paragraphs(2).Select只选择第1段?
EN

Stack Overflow用户
提问于 2017-04-25 16:00:18
回答 1查看 879关注 0票数 1

我想通过VBA为我的MS-Word文件添加许多超链接,对于第1段,超链接是"./index/1.doc",第二段是"./index/2.doc",等等。简单的过程是1)选择一个段落2)添加超链接,正如下面的代码所述。但是,VBA代码为每个段落提供了相同的超链接,这应该是最后一段的超链接。那么,在VBA中是否有任何方法可以取消对这种情况的选择?谢谢

顺便说一句,VBA可以在任何有超过1段的MS-Word文件上进行测试.

代码语言:javascript
复制
Sub addHypertext()
    For countParagraph = 1 To ActiveDocument.Paragraphs.Count
        Selection.Collapse Direction:=wdCollapseEnd    
        set para = ActiveDocument.Paragraphs(countParagraph)
        para.Select
        Set paraStyle = para.Style 
        ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, \
            Address:="./index/" & countParagraph & ".doc"    

        Rem this does not work 
        Rem Selection.Range.Style = paraStyle 

        Rem this does not work 
        Selection.Style = paraStyle 

        Rem this does not work too 
        Rem para.Style = paraStyle 

        Rem this produces "run-time error" 
        Rem para.Range.Style = "text"
    Next
End Sub
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-25 16:45:38

我知道发生了什么。它突出显示段落标记,并使其成为超链接的一部分。我很好奇,也许你只是想在每一段的末尾加上一个引用。看看这是否是你想尝试的东西:

代码语言:javascript
复制
Sub addHypertext()
    Dim para As Paragraph

    For countParagraph = 1 To ActiveDocument.Paragraphs.Count
        Selection.Collapse Direction:=wdCollapseEnd
        Set para = ActiveDocument.Paragraphs(countParagraph)
        para.Range.Select
        Selection.MoveRight 1
        Selection.MoveLeft 1
        Selection.Font.Superscript = True
        Selection.TypeText "[" + Trim(Str(countParagraph)) + "]"
        Selection.MoveLeft Count:=Len("[" + Trim(Str(countParagraph)) + "]"), Extend:=wdExtend

        para.Range.Hyperlinks.Add Anchor:=Selection.Range, _
            Address:="./index/" & countParagraph & ".doc"

        Selection.Font.Superscript = False
    Next

End Sub

上面的链接会像维基百科在他们的网站上所做的那样,在每一段的末尾加上一个参考链接。

以下内容将得到整个段落作为链接,就像您最初想要的那样:

代码语言:javascript
复制
Sub addHypertext()
    Dim para As Paragraph

    For countParagraph = 1 To ActiveDocument.Paragraphs.Count
        Selection.Collapse Direction:=wdCollapseEnd
        Set para = ActiveDocument.Paragraphs(countParagraph)
        para.Range.Select
        Selection.MoveEnd Count:=-1

        para.Range.Hyperlinks.Add Anchor:=Selection.Range, _
            Address:="./index/" & countParagraph & ".doc"

    Next

End Sub

编辑

要回答您添加的样式,下面仍然保留链接并更改样式。

代码语言:javascript
复制
Sub addHypertext()
    Dim para As Paragraph

    For countParagraph = 1 To ActiveDocument.Paragraphs.Count
        Selection.Collapse Direction:=wdCollapseEnd
        Set para = ActiveDocument.Paragraphs(countParagraph)
        para.Range.Select
        Selection.MoveEnd Count:=-1

        para.Range.Hyperlinks.Add Anchor:=Selection.Range, _
            Address:="./index/" & countParagraph & ".doc"

        para.Range.Select
        Selection.MoveEnd Count:=-1

        'Per your comments below for future visitors
        Selection.Style = "Normal"
        Selection.Style = "My Custom Style"

    Next

End Sub
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43615665

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档