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

Vba 对象变量或未设置块变量(错误91),vba,ms-publisher,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 = "

我有以下代码:

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

我在带有
hyperLinkText=hprlink.Range
的行中遇到“对象变量或未设置块变量(错误91)”错误。调试时,我可以看到
hprlink.Range
确实有一个值。有没有想过我做错了什么?

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

Set hyperLinkText = hprlink.Range

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

试着写下“Set hyperLinkText=hprlink.Range”就可以了。谢谢!出于好奇,为什么需要这个集合?TextRange是一个对象。它必须实例化(TextRange不可能)或指向('assigned')到现有的TextRange对象,这是通过Set语句完成的。执行时,该语句相当于尝试将hprlink.Range分配给Nothing.Related post,这在VBA-中工作时很重要。