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

时间:2013-12-19 21:45:26

标签: 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

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

1 个答案:

答案 0 :(得分:14)

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

Set hyperLinkText = hprlink.Range

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