2013-12-19 66 views
8

我有以下代码:对象变量或带块变量未设置(错误91)

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确实有一个值。任何想法我做错了什么?

+5

尝试编写'Set hyperLinkText = hprlink.Range' – Barranka

+0

这样做了。谢谢!出于好奇,为什么需要Set? – GBleaney

+0

TextRange是一个对象,它必须被实例化(不可能使用TextRange)点('分配')到现有的TextRange对象,这是通过Set语句完成的。执行时,语句等同于试图将hprlink.Range分配给Nothing。 –

回答

9

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

Set hyperLinkText = hprlink.Range 

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

相关问题