Click to See Complete Forum and Search --> : Manipulating XML with VBA


Paro
June 24th, 2008, 08:31 AM
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


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?

dglienna
June 24th, 2008, 08:54 AM
You need to SET a reference to MSXML first. Here are some VB6 samples:

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.

Paro
June 24th, 2008, 09:23 AM
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?


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 :(

Paro
June 24th, 2008, 09:29 AM
Oh got it now I think :) Thanks a lot


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


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