Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object variable or With block variable not set (Error 91)

I have the following code:

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

I am getting the "Object variable or With block variable not set (Error 91)" error on the line with hyperLinkText = hprlink.Range. When I debug I can see that hprlink.Range does have a value. Any thoughts what I'm doing wrong?

like image 698
GBleaney Avatar asked Dec 19 '13 21:12

GBleaney


People also ask

How do I fix object variable not set error 91?

Error: "Runtime Error 91" is a Visual BASIC error which means "Object variable not set". This indicates that the object was never created using the "Set" command before being used. Remedy: Be sure to use the SET statement to create the new oject.

How do you fix object variables with block variable not set?

To correct this errorMake sure you aren't referring to an object variable that has been set to Nothing . Search your code for the keyword Nothing , and revise your code so that the object isn't set to Nothing until after you have referenced it. Make sure that any array variables are dimensioned before you access them.

What does object variable or with block variable not set mean?

"Object variable or With block variable not set" is a Visual Basic error message. It could originate in Excel itself, in Palisade code, or in your own macros if you have an . XLSM file. Roughly, it means that the VBA code used some variable without first initializing it properly.

What is error 91 Excel?

"Runtime Error 91: Object variable or with block variable not set" is a runtime error that can happen on any Windows-based operating system version. The DCOMCnfg.exe file, which is frequently used to establish rights and set system-wide security settings, is usually the problem.


1 Answers

As I wrote in my comment, the solution to your problem is to write the following:

Set hyperLinkText = hprlink.Range

Set is needed because TextRange is a class, so hyperLinkText is an object; as such, if you want to assign it, you need to make it point to the actual object that you need.

like image 80
Barranka Avatar answered Oct 14 '22 20:10

Barranka