Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Set xmlDoc_pvt = CreateObject("MSXML2.DOMDocument")
Set rootNode = xmlDoc_pvt.createElement("HP_Scan_Iterm")
Set xmlDoc_pvt.DocumentElement = rootNode
Set Header = xmlDoc_pvt.createProcessingInstruction("xml", "version='1.0' encoding='Unicode'")
xmlDoc_pvt.InsertBefore Header, xmlDoc_pvt.ChildNodes(0)
xmlDoc_pvt.Save ThisWorkbook.Path & "\HP_Scan_Iterm.xml"
Set xmlDoc_pvt = Nothing
writeSummaryData (ThisWorkbook.Path & "\HP_Scan_Iterm.xml")
Call StrConv(ThisWorkbook.Path & "\HP_Scan_Iterm.xml", vbUnicode)
End Sub
Public Function writeSummaryData(strXMLfileSpec)
Set xmlDoc_pvt = CreateObject("MSXML2.DOMDocument")
xmlDoc_pvt.Load strXMLfileSpec
Set Root = xmlDoc_pvt.DocumentElement
maxCol = Worksheets(1).Range("IV1").End(xlToLeft).Column
For colIndex = 2 To maxCol
strTestSum = "OS"
strAtt = Worksheets(1).Cells(1, colIndex).Value
'Set parent_Comp = xmlDoc_pvt.DocumentElement.SelectSingleNode(strTestSum)
Set new_node = xmlDoc_pvt.createElement(strTestSum)
Set Att = xmlDoc_pvt.createAttribute("Version")
Att.Value = strAtt
new_node.setAttributeNode (Att)
Set parent_Comp = xmlDoc_pvt.DocumentElement.appendChild(new_node)
sheetCount = Worksheets.Count
For i = 1 To sheetCount
Dim tabName As String
tabName = Worksheets(i).Name
Call writeArgField(xmlDoc_pvt, parent_Comp, tabName, strAtt)
Next
Next
xmlDoc_pvt.Save strXMLfileSpec
Set xmlDoc_pvt = Nothing
End Function
Public Sub writeArgField(xmlDoc_pvt, parent_Comp, tabName, deviceName)
'Create the new Field node.
Set new_node = xmlDoc_pvt.createElement(tabName)
Set parent_Step = parent_Comp.appendChild(new_node)
' Create the sub-Field nodes.
maxRow = Worksheets(tabName).Range("A65535").End(xlUp).Row
colIndex = getColNumber(deviceName)
For i = 2 To maxRow
strTestSum = "Value"
strAtt = Worksheets(tabName).Cells(i, 1).Value
' Set Parent = xmlDoc_pvt.DocumentElement.SelectSingleNode(strTestSum)
Set MyNode = xmlDoc_pvt.createElement(strTestSum)
Set Att = xmlDoc_pvt.createAttribute("Name")
Att.Value = strAtt
MyNode.setAttributeNode (Att)
MyNode.Text = Worksheets(tabName).Cells(i, colIndex).Value
parent_Step.appendChild MyNode
Next
End Sub
Public Function getColNumber(DName)
maxCol = Worksheets(1).Range("IV1").End(xlToLeft).Column
CIndex = 1
For i = 2 To maxCol
CIndex = CIndex + 1
If DName = Worksheets(1).Cells(1, i).Value Then
getColNumber = CIndex
Exit Function
End If
Next
End Function
联系客服