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
确实有一个值。任何想法我做错了什么?
尝试编写'Set hyperLinkText = hprlink.Range' – Barranka
这样做了。谢谢!出于好奇,为什么需要Set? – GBleaney
TextRange是一个对象,它必须被实例化(不可能使用TextRange)点('分配')到现有的TextRange对象,这是通过Set语句完成的。执行时,语句等同于试图将hprlink.Range分配给Nothing。 –