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

2019-05-12 06:28发布

我有以下代码:

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

我正在“对象变量或With块变量未设置(错误91)”上与线错误hyperLinkText = hprlink.Range 。 当我调试,我可以看到hprlink.Range确实有一个值。 任何想法就是我做错了吗?

Answer 1:

正如我在评论中写道,解决你的问题是编写如下:

Set hyperLinkText = hprlink.Range

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



文章来源: Object variable or With block variable not set (Error 91)