• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1342
  • Last Modified:

Run-time error 91 - Opening form

Hi Experts,

I have created a Word template containing a macro userform where the user is supposed to set a document type. I am using a XML file to populate the ListBox with available document types. Everything seems to be running fine when the template is already opened - but if I double click on the template to create a new document I get an Run-time error 91 Object variable or With block variable not set.

When running in debug I can see that it is the UserForm.Show command that is causing the error. In the UserForm I have created a sub that loads the file and populates the ListBox:

Private Sub UserForm_Initialize()
    OpenXmlDocument
    'Populate ListBox with available document types
    Call XmlDocTypes(listDocType)
End Sub

Open in new window


If I comment out the XmlDocTypes the form is being displayed without any errors - though it being blank.

This is the code for XmlDocTypes:

Public Function XmlDocTypes(pListBox As ListBox)
    Dim lNodeList As IXMLDOMNodeList
    Dim lNode As IXMLDOMNode
    'Set XML node list where text is retrieved from
    Set lNodeList = mXmlDocument.SelectNodes("/" & xmlRoot & _
        "/" & xmlSetTypes & _
        "/" & xmlSetDoc)
    'Check if XML document is loaded and can be opened
    If OpenXmlDocument = True Then
        'Add each text entry as separate items in ComboBox
        For Each lNode In lNodeList
            With pListBox
                .AddItem
                .Column(0, pListBox.ListCount - 1) = lNode.ChildNodes.Item(0).Text
                .Column(1, pListBox.ListCount - 1) = lNode.ChildNodes.Item(1).Text
            End With
        Next
    End If
End Function

Open in new window


It seems that the template needs to be opened for it to work - is there a way to avoid this? I would prefer not to put the template in the Word startup folder as the code is only supposed to be used whenever a document built on the template is opened.

For your reference I am also adding the code for checking if the XML document is loaded:

Public Function OpenXmlDocument() As Boolean
    On Error GoTo errHandler
    Dim lXmlDocument As DOMDocument
    Set lXmlDocument = New DOMDocument
    'Disable asynchronous loading in order to ensure file is fully loaded before continuing
    lXmlDocument.async = False
    If Len(lFile) > 0 Then
        If lXmlDocument.Load(lFile) Then
            'File loaded successfully
            OpenXmlDocument = True
            Set mXmlDocument = lXmlDocument
        Else
            'File load failed
            Set lXmlDocument = Nothing
            OpenXmlDocument = False
            MsgBox msgFileErr & vbCr & lFile, vbExclamation, msgTitle
        End If
    End If
errHandler:
    If Err.Number <> 0 Then
        Set lXmlDocument = Nothing
        OpenXmlDocument = False
    End If
End Function

Open in new window


Thanks.
0
jchristensen
Asked:
jchristensen
  • 5
  • 5
1 Solution
 
GrahamSkanCommented:
I suspect mXmlDocument. It might be a global object variable, defined and instantiated elsewhere, but that can't be seen in the code.
0
 
jchristensenAuthor Commented:
Hi Graham,

The mXmlDocument is defined in the XML-module where the other two functions are created:

Private mXmlDocument As DOMDocument

Open in new window


In the AutoOpen module the lFile is set:

Sub Main()
    'Set XML path and file name
    lFile = "C:\Users\UserId\Desktop\Xmlfile.xml"
End Sub

Open in new window

0
 
GrahamSkanCommented:
Have you tried stepping through to find exactly which line raises the error? It might help to disable your error handling while developing.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
GrahamSkanCommented:
Sorry, I have to go now. If this is still open, I'll be back in about 8 hours.
0
 
jchristensenAuthor Commented:
I don't think there is an error as such - if I save the document from .dotm to .docm the .docm version works. I guess it must be the variables that are not being set/file being loaded when openening a document based on the .dotm file.
0
 
GrahamSkanCommented:
How do you start the code running?
0
 
jchristensenAuthor Commented:
I have created a AutoNew module with the following code:
Public Sub Main()
    Application.Run "DeclarationDoctype"
End Sub

Open in new window

The DeclarationDoctype is calling the UserForm:
Sub DeclarationDoctype()
    frmDocType.Show
End Sub

Open in new window

I have looked further into the code and it seems to "fail" alrady when running the OpenXmlDocument (as listed above) - but only if the actual template is not loaded beforehand.
0
 
jchristensenAuthor Commented:
Hi Graham,

I think I found a solution myself - apparently I cannot use the AutoOpen module with documents based on the template with the code. The AutoOpen only executes when opening the actual template. I have moved the instantiation of the lFile string to the XML-code handling module and now it seems to be working.

Thanks for your help anyway.
0
 
GrahamSkanCommented:
That was the reason that I asked how you started the code running.
0
 
jchristensenAuthor Commented:
Code is now working after moving the AutoOpen part to another module.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now