对象变量或未设置块变量(错误91)

GBl*_*ney 16 vba ms-publisher

我有以下代码:

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
Run Code Online (Sandbox Code Playgroud)

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

Bar*_*nka 17

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

Set hyperLinkText = hprlink.Range
Run Code Online (Sandbox Code Playgroud)

Set需要因为TextRange是一个类,所以hyperLinkText是一个对象; 因此,如果要分配它,则需要指向所需的实际对象.