LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #14   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Excel Macro via VBA - XML IMPORT

Try commenting out the line Set xmlDom = Nothing and see if the problem still
exist. I would think a firewall or a virsus protection software package
would behave like this and not a software package like excel/vba. I suspect
that setting an object to nothing may have potential problems and is a more
likely cause. If it does solve the problem I would notify microsoft because
this is a problem that definiely need to be fixed.

"Virgil" wrote:

The final for others is below and works a treat.

HOWEVER i have been driven crazy by Excel or the VBA IDE DELETING lines on
me...

Base file is a DBF file and macro lives in an XLS file.

Every time i would restart the
Set xmlDom = New MSXML2.DOMDocument
line was removed. I went to modify a MsgBox and several of the MsgBox's
where deleted.
Other code lines were randomly deleted between runs.

I am using Excel 2007 and perhaps this has issues ... as i used to use Excel
2003.

Cheers

=================

'
' This function will read an XML file which has DBF updates and
' update the records in the current Sheet with this data.
' This was designed for Trends but could be used for other DBFs
' The column name to identify the row is coded to be "NAME"
'
Sub Excel_Row_Updates()
Dim RowCount As Long, ColCount As Long, RowUpdateCount As Long
' Add a reference to Microsoft XML, v6.0
Dim xmlFile As Variant
Dim xmlrow As Variant
Dim xmlcol As Variant
Dim NameHeader As Variant
Dim NameCol As Variant
Dim LastRow As Variant
Dim c As Variant
Dim NameXmlCol As Long
Dim NameDbfRow As Long
Dim NameDbfCol As Long
Dim FieldUpdates As Long
Dim xmlDom As MSXML2.DOMDocument
Set xmlDom = New MSXML2.DOMDocument

FieldUpdates = 0
RowCount = 0
RowUpdateCount = 0

xmlFile = Application.GetOpenFilename("Select XML file (*.xml), *.xml")

If xmlFile < False Then

xmlDom.load (xmlFile)

' We assume that the macro is run from the main Excel page
With ActiveSheet

Debug.Print ActiveSheet.Name
' Find the column name is in
Set NameHeader = .Rows(1).Find(what:="NAME", _
LookIn:=xlValues, lookat:=xlWhole)
If NameHeader Is Nothing Then
MsgBox ("Error : Cannot find NAME in header row")
Set xmlDom = Nothing
Exit Sub
Else
NameCol = NameHeader.Column
End If

For Each xmlrow In xmlDom.documentElement.childNodes

RowCount = RowCount + 1

' Find the NAME value for each XML record set
For Each xmlcol In xmlrow.childNodes
If xmlcol.tagName = "NAME" Then
Exit For
End If
Next xmlcol

' Find the row that the NAME is in in the DBF / EXCEL

NameDbfRow = 0

Set c = .Columns(NameCol).Find(what:=xmlcol.Text, _
LookIn:=xlValues, lookat:=xlWhole)

If c Is Nothing Then
GoTo Continue ' Row does not exist, consider not fatal as
sheet maybe a subset
Else
' We know the row the "NAME" item value is in
NameDbfRow = c.Row
RowUpdateCount = RowUpdateCount + 1
End If

' OK we have found the row the data is in so lets update the
other columns
For Each xmlcol In xmlrow.childNodes
If xmlcol.tagName < "NAME" Then
' Update the DBF row with the value for this name
Set c = .Rows(1).Find(what:=xmlcol.tagName, _
LookIn:=xlValues, lookat:=xlWhole)

If c Is Nothing Then
' Although the row does not have to exist, if the row
does exist then
' the columns must exist.
MsgBox (xmlcol.tagName & " does not exist in the DBF
file.")
GoTo Abort
Else
'Field column found in DBF record
NameDbfCol = c.Column
If NameDbfRow 1 Then ' Security check, has to be
1

.Cells(NameDbfRow, NameDbfCol) = xmlcol.Text
FieldUpdates = FieldUpdates + 1
End If
End If
End If
Next xmlcol

Continue:
Next xmlrow

End With

MsgBox (RowUpdateCount & " rows updated from " & RowCount & " records,
please save file.")

Abort:
Set xmlDom = Nothing

Else

MsgBox ("Aborting macro as no XML file was slected")

End If

End Sub

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
import from excel to access with macro? J.W. Aldridge Excel Programming 2 April 30th 09 09:05 PM
Excel Import Macro gkarasiewicz Excel Discussion (Misc queries) 5 January 12th 08 06:08 PM
Import Macro in PERSONAL.XLS will not import to my main document mike Excel Programming 8 October 31st 07 09:24 PM
Import Data Excel Macro [email protected] Excel Discussion (Misc queries) 3 August 23rd 06 02:11 PM
Use / Import Win32API in Excel macro Ben Zhu Excel Programming 2 September 10th 03 09:37 PM


All times are GMT +1. The time now is 03:04 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"