最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【未解决】word vba中用Hyperlinks SubAddress添加页内链接无效

VBA crifan 3689浏览 0评论

【未解决】word vba中用Hyperlinks SubAddress添加页内链接无效

【问题】

在word中,想要通过vba宏,去给选中的文字添加页内链接。

已经实现了如下代码,选中需要的文字:

MsgBox “———-1.add CR”

ActiveDocument.Select
ActiveDocument.GoTo what:=wdLine, which:=wdGoToFirst

Do
With Selection.Find
    .MatchWildcards = True
    .Forward = True
    .ClearFormatting
    .MatchWholeWord = True
    .MatchCase = False
    ‘.Wrap = wdFindContinue
    .Execute FindText:=”【*】”
End With
Loop While (Selection.Font.Superscript = True)

‘MsgBox “now is normal text”

Selection.InsertAfter vbCrLf

Do
With Selection.Find
    .MatchWildcards = True
    .Forward = True
    .ClearFormatting
    .MatchWholeWord = True
    .MatchCase = False
    ‘.Wrap = wdFindContinue
    .Execute FindText:=”【*】”
End With

Selection.InsertAfter vbCrLf
Selection.Start = Selection.End

Loop While (Selection.Find.Found = True)

MsgBox “———-2. set to heading”
ActiveDocument.Select
ActiveDocument.GoTo what:=wdLine, which:=wdGoToFirst

Do
With Selection.Find
    .MatchWildcards = True
    .Forward = True
    .ClearFormatting
    .MatchWholeWord = True
    .MatchCase = False
    ‘.Wrap = wdFindContinue
    .Execute FindText:=”【*】”
End With
Loop While (Selection.Font.Superscript = True)

‘MsgBox “now is normal text”

Selection.Range.Style = wdStyleHeading2

Do
With Selection.Find
    .MatchWildcards = True
    .Forward = True
    .ClearFormatting
    .MatchWholeWord = True
    .MatchCase = False
    ‘.Wrap = wdFindContinue
    .Execute FindText:=”【*】”
End With

Selection.Range.Style = wdStyleHeading2

Loop While (Selection.Find.Found = True)

然后参考了录制宏去添加页内链接生成的代码:

Sub Macro1()

‘ Macro1 Macro
‘ 宏在 2011-1-8 由 USER 录制

    ChangeFileOpenDirectory “E:downloadautoProcessWordFile”
    ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:=””, _
        SubAddress:=”_【2】”, ScreenTip:=””, TextToDisplay:=”【2】”
End Sub

其中【2】,是前面代码已经实现添加了对应的标题的文字,SubAddress:=”_【2】”, 即页内跳转到标题为”【2】”的位置。

照葫芦画瓢,添加了如下代码:

MsgBox “———-3. link to heading”
ActiveDocument.Select
ActiveDocument.GoTo what:=wdLine, which:=wdGoToFirst

Do
With Selection.Find
    .MatchWildcards = True
    .Forward = True
    .ClearFormatting
    .MatchWholeWord = True
    .MatchCase = False
    ‘.Wrap = wdFindContinue
    .Execute FindText:=”【*】”
End With

‘ChangeFileOpenDirectory ActiveDocument.Path & “”
‘ChangeFileOpenDirectory ActiveDocument.Path
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:=””, SubAddress:=”_” & Selection.Text, ScreenTip:=””, TextToDisplay:=Selection.Text
‘Selection.Collapse Direction:=wdCollapseEnd

Loop While (Selection.Font.Superscript = True)

结果,

ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:=””, SubAddress:=”_” & Selection.Text, ScreenTip:=””, TextToDisplay:=Selection.Text
所实现的功能,的确是添加了页内链接了,但是Ctrl+点击该文字后,光标却跳转到文档开头了,而不是跳转到对应的标题位置。

【尝试解决的过程】

1.而自己手动去设置页内跳转的链接,是可以工作的,但是却无法看到其所添加的链接地址是多少,所以没法继续。

2.百度google了很多,发现别人也有在excel中遇到类似问题,用SubAddress添加的业内链接无效。但是没看到有效解决办法。

3.以为是新装的office 2010的问题,所以卸载了office 2010,换上旧的office 2003,重新尝试,问题依旧。

转载请注明:在路上 » 【未解决】word vba中用Hyperlinks SubAddress添加页内链接无效

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (1)

  1. 搜到这个问题,摸索了一下自己基本解决了,主要是需要ActiveDocument.Bookmarks.Add "_[此处替换为标题名称]", Selection.Range。在此记录,希望帮到别人也可能以后自己忘记了能被搜到。我的不常用邮箱[email protected],记录在此作为标记
    oopython4年前 (2020-07-23)回复
84 queries in 0.175 seconds, using 22.17MB memory