CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2008
    Posts
    7

    Manipulating XML with VBA

    I'm using Microsoft Visual Basic 6.3

    I'm not sure if this is the right place to ask this question, but here it goes.

    I'm going to make a small Word application which can manipulate a XML file

    Code:
    Private Sub ManipulateXLM()
    
    Dim doc As MSXML2.DOMDocument
    doc.Load ("C:\Documents and Settings\z3xld\Desktop\2Usecases.xml")
    
    End Sub
    It says:
    runtime error '91':
    Object variable or With block variable not set
    Can anyone see what might be the problem?

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Manipulating XML with VBA

    You need to SET a reference to MSXML first. Here are some VB6 samples:
    Code:
    Option Explicit
    ' These are both examples of Late Binding
    
    Public Sub RunAccessMacro(strDB As String, strMacro As String)
    '================================================================
    'for late binding declare it As Object and use CreateObject() function
      Dim AccessDB As Object
      Set AccessDB = CreateObject("Access.Application")
    
        With AccessDB
            .OpenCurrentDatabase strDB
            .DoCmd.RunMacro strMacro, 1
            '.Visible = True    'you decide
            .CloseCurrentDatabase
        End With
        Set AccessDB = Nothing
    
    End Sub
    
    Public Sub ExcelMacro()
      Dim excl As Object
      Dim wrbk As Object
    
        Set excl = CreateObject("Excel.Application")
        excl.DisplayAlerts = False
        Set wrbk = excl.Workbooks.Open("myfile", , True, , "mypassword")
        excl.Run "MacroName", "arg1", "arg2"
    End Sub
    It should work in VBA, but you might have to use VARIANT instead of OBJECT.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jun 2008
    Posts
    7

    Re: Manipulating XML with VBA

    Thanks, but could you maybe edit my example? I haven't really coded VBA before, so I'm not used to the syntax.

    is it something like this?

    Code:
    Private Sub ManipulateXLM()
    
    Dim doc As MSXML2.DOMDocument
    Set doc = Load("C:\Documents and Settings\z3xld\Desktop\2Usecases.xml")
    
    With doc
    //Do stuff
    End With
    
    End Sub
    It won't work though

  4. #4
    Join Date
    Jun 2008
    Posts
    7

    Re: Manipulating XML with VBA

    Oh got it now I think Thanks a lot

    Code:
    Private Sub ManipulateXLM()
    
    Dim doc As New MSXML2.DOMDocument
    doc.Load("C:\Documents and Settings\z3xld\Desktop\2Usecases.xml")
    
    With doc
    //Do stuff
    End With
    
    End Sub
    Or

    Code:
    Private Sub ManipulateXLM()
    
    Dim doc As MSXML2.DOMDocument
    Set doc = new MSXML2.DOMDocument
    doc.Load("C:\Documents and Settings\z3xld\Desktop\2Usecases.xml")
    
    With doc
    //Do stuff
    End With
    
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured