VB6 - Error 91 Object or Block Variable Not Set

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • GlenGage
    New Member
    • Mar 2008
    • 4

    VB6 - Error 91 Object or Block Variable Not Set

    My application creates Word documents. The routine below takes an existing, open document (vobjSourceDocu ment) and makes a new document (robjNewDocumen t) similar to it. The routine works 999+ times out of 1000. I cannot find any pattern regarding document type (template) or qualities of the source document, but once I have a source document that fails it will fail consistenly on any workstation.

    In the code sample below it is trying to write to the new document, but it will fail on any object method I use. When the routine works, the VarType = 8 (string) but when it fails the VarType = 9 (automation object). In either case, IsObject always = True.

    Thank you in advance,

    Glen

    [CODE=vb]
    ' Make an exact duplicate of the currently open document, optionally to a different
    ' template and return reference to the new.

    Public Function DuplicateDocume nt(ByVal vobjSourceDocum ent As Word.Document, _
    ByRef robjNewDocument As Word.Document, _
    Optional strNewTemplate As String = "") As Boolean

    Dim I As Long
    Dim strBookmarkName As String
    Dim strTemplateName As String
    Dim strTemplateLang uage As String
    Dim strArrayBookmar kNames() As String
    Dim strArrayBookmar kValues() As String
    Dim objWordApplicat ion As Word.Applicatio n
    Dim GGADebug As String

    On Error GoTo Error
    DuplicateDocume nt = False
    Set objWordApplicat ion = vobjSourceDocum ent.Application

    ' Store all the bookmark values in temp array
    ReDim strArrayBookmar kNames(vobjSour ceDocument.Book marks.COUNT + 1)
    ReDim strArrayBookmar kValues(vobjSou rceDocument.Boo kmarks.COUNT + 1)
    For I = 1 To vobjSourceDocum ent.Bookmarks.C OUNT
    strBookmarkName = vobjSourceDocum ent.Bookmarks.I tem(I)
    strArrayBookmar kNames(I) = strBookmarkName
    vobjSourceDocum ent.Bookmarks(I ).Select
    strArrayBookmar kValues(I) = Selection.Text
    End If
    Next I

    ' copy body text inc. Annexes
    If strNewTemplate = "" Then
    ' Get the current document's template name
    strTemplateName = vobjSourceDocum ent.AttachedTem plate.FullName
    Else
    strTemplateName = strNewTemplate
    End If

    ' create a new document based on the template name
    blnWordFileDupl icate = True
    'Set robjNewDocument = New Word.Document
    Set robjNewDocument = objWordApplicat ion.Documents.a dd(strTemplateN ame)
    GGADebug = VarType(robjNew Document) 'When this = 8 (string) it works, fails on 9 (Automation Object)
    'How this can sometimes be 8 and sometimes 9 is a mystery
    'Why it works when it is a string and not an object I don't know

    GGADebug = GGADebug & " " & IsObject(robjNe wDocument) ‘IsObject is always true, even when VarType is String
    blnWordFileDupl icate = False

    ' paste existing content into new document - do not copy styles \rt !!!
    For I = 1 To UBound(strArray BookmarkNames() ) - 1
    If strArrayBookmar kNames(I) <> cstrWordBookmar kBody Then
    ‘Fails on the next line
    writeToDocument robjNewDocument , strArrayBookmar kNames(I), _
    strArrayBookmar kValues(I), ""
    'With robjNewDocument
    ' .EditGoto strArrayBookmar kNames(I)
    ' .Insert strArrayBookmar kValues(I)
    'End With
    End If
    Next I

    DuplicateDocume nt = True
    Exit Function

    Error:

    objWordApplicat ion.ScreenUpdat ing = True

    MsgBox LoadDBString(my LanguageOffset + 250) & " !" & vbNewLine & vbNewLine & _
    "Source = " & Err.Source & vbNewLine & vbNewLine & GGADebug & vbNewLine & vbNewLine & _
    " Description = " & Err.Description & " " & Err.Number, vbOKOnly, "Duplicate Document"

    End Function[/CODE]
    Last edited by Killer42; Mar 7 '08, 02:21 AM. Reason: Added CODE=vb tag
  • VBWheaties
    New Member
    • Feb 2008
    • 145

    #2
    It looks like you are passing in (byVal) a Word.Document, then you are using that to obtain an instance of its Word.Applicatio n.
    This will work as long as you have at least one instance of Word running in memory. If you dont, you'll get your error and that explains why it errors sometimes.
    Solution may be to make sure your Word.Applicatio n is running before using it.

    if that doesnt do it, let us know what line the code errors on. Set your Debug to Break On All Errors.
    Last edited by VBWheaties; Mar 6 '08, 02:37 PM. Reason: removed quoted text

    Comment

    • GlenGage
      New Member
      • Mar 2008
      • 4

      #3
      Thanks for a quick response!

      The routine is actually called from the open document. The application is a dll that gets attached to the documents via the templates. The line of code that fails is:

      ‘Fails on the next line

      writeToDocument robjNewDocument , strArrayBookmar kNames(I), _

      However, even asking VB to return the documents name causes the error. I don't know how to set the debug but will ask/look around to see how to do this. I haven't programmed in decades.

      Originally posted by VBWheaties
      It looks like you are passing in (byVal) a Word.Document, then you are using that to obtain an instance of its Word.Applicatio n.
      This will work as long as you have at least one instance of Word running in memory. If you dont, you'll get your error and that explains why it errors sometimes.
      Solution may be to make sure your Word.Applicatio n is running before using it.

      if that doesnt do it, let us know what line the code errors on. Set your Debug to Break On All Errors.

      Comment

      • kadghar
        Recognized Expert Top Contributor
        • Apr 2007
        • 1302

        #4
        Originally posted by GlenGage
        Thanks for a quick response!
        ...
        ‘Fails on the next line

        writeToDocument robjNewDocument , strArrayBookmar kNames(I), _
        Im not familiarized with the WriteToDocument Sub, so i Googled it and found something quite similar to the one you have. Why dont you use this one, with another name, so you can debug it and see where the error ocurs:
        (change its name to MyWriteToDocume nt, copy-paste it anywhere, and use that one on your code, so when the error is shown, it'll be in the Sub's line, and you can have a better idea of what's wrong).

        HTH

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Subscribing .

          Comment

          • GlenGage
            New Member
            • Mar 2008
            • 4

            #6
            I know where it fails. It fails as soon as I try to use the new document after the SET line:
            [color=#b1b100]
            Code:
            [color=#b1b100]Set[/color] robjNewDocument = objWordApplication.[color=#66cc66]Documents[/color].[color=#66cc66]add[/color][color=#66cc66]([/color]strTemplateName[color=#66cc66])[/color]
            [/color]
            [color=#b1b100][/color]
            [color=#b1b100]
            [color=#b1b100]For example, if I place the statement...[/color]
            [/color]
            [color=#b1b100][/color]
            [color=#b1b100]
            Code:
            [color=#b1b100]GGADebug = robjNewDocument.FullName[/color]
            [/color]
            [color=#b1b100][/color]
            [color=#b1b100]
            [color=#b1b100]...after the SET statement it will fail with the same error (91).[/color]
            [/color]


            Originally posted by kadghar
            Im not familiarized with the WriteToDocument Sub, so i Googled it and found something quite similar to the one you have. Why dont you use this one, with another name, so you can debug it and see where the error ocurs:
            (change its name to MyWriteToDocume nt, copy-paste it anywhere, and use that one on your code, so when the error is shown, it'll be in the Sub's line, and you can have a better idea of what's wrong).

            HTH

            Comment

            • GlenGage
              New Member
              • Mar 2008
              • 4

              #7
              QUOTE]
              Thank you all for you help. Debugging is a humbling experience.

              The cause of the error is an incorrect path to the template I was opening the new document with. When I split the creation of the document into 2 steps -- creating it and then associating it with the template VB was quick to point out that it could not attach the template because it couldn't find it.

              If the original SET command had failed for this reason then it would have been obvious. Instead the SET command worked but returned a VARType of 9 instead of 8.

              If you are curious about how the program came up with an incorrect path name then read on, else thanks again for your time and interest.

              We keep the template presented to the users in subfolders of the workgroup location. The subfolders correspond to templates in different languages--a letter is a letter, but it may be in English, Russian, etc. Because we share documents across networks the attached template paths have different physical names from one network to another. We "work around" this potential problem (word not being able to find a documents attached template) by keeping hidden copies of all templates in the root of the workgroup locations folder. These 1 out of a thousand documents have this hidden template as their attached template rather than the same template in the subfolder. The routine that builds the string to use for the new document's template didn't work properly when the target template was in a different language subfolder and the attached template was in the root. This is now fixed.

              Cheers,

              Glen
              [/QUOTE]

              Comment

              Working...