17

我有以下代码:

Sub AddSources()
    Dim pubPage As Page
    Dim pubShape As Shape
    Dim hprlink As Hyperlink
    Dim origAddress() As String
    Dim exportFileName As String
    exportFileName = "TestResume"
    Dim linkSource As String
    linkSource = "TestSource2"
    Dim hyperLinkText As TextRange



    For Each pubPage In ActiveDocument.Pages
        For Each pubShape In pubPage.Shapes
            If pubShape.Type = pbTextFrame Then
                For Each hprlink In pubShape.TextFrame.TextRange.Hyperlinks
                    If InStr(hprlink.Address, "http://bleaney.ca") > 0 Then
                        hyperLinkText = hprlink.Range
                        origAddress = Split(hprlink.Address, "?source=")
                        hprlink.Address = origAddress(0) + "?source=" + linkSource
                        hprlink.Range = hyperLinkText
                    End If
                Next hprlink
            End If
        Next pubShape
    Next pubPage
    ThisDocument.ExportAsFixedFormat pbFixedFormatTypePDF, "C:\" + exportFileName + ".pdf"
End Sub

我收到“对象变量或未设置块变量(错误91)”错误hyperLinkText = hprlink.Range。当我调试时,我可以看到它hprlink.Range确实有价值。有什么想法我做错了吗?

4

1 回答 1

19

正如我在评论中所写,解决您的问题的方法是编写以下内容:

Set hyperLinkText = hprlink.Range

Set因为TextRange是类,所以hyperLinkText需要对象;因此,如果你想分配它,你需要让它指向你需要的实际对象。

于 2013-12-19T21:59:43.417 回答