将Excel工作表转换为XML时出错

时间:2017-10-01 11:23:05

标签: xml excel excel-vba vba

您好我想将大型Excel工作表转换为特定文件夹中的xml文件。要做到这一点,我已经阅读了问题(Convert rows of spreadsheet to separate XML files)。我尝试应用它并将其调整为litle,以便它适合excel格式:

enter image description here

调整代码后,它看起来像这样:

Sub xmlPerRow()

 sTemplateXML = _
        "<?xml version='1.0'?>" + vbNewLine + "<data>" + vbNewLine + "<Grai>" + vbNewLine + "</Grai>" + vbNewLine + _
        "   <DayDateOut>" + vbNewLine + "   </DayDateOut>" + vbNewLine + "   <Filler>" + vbNewLine + "   </Filler>" + vbNewLine + _
        "   <FillerCountry>" + vbNewLine + "   </FillerCountry>" + vbNewLine + "   <Retailer>" + vbNewLine + "   </Retailer>" + vbNewLine + _
        "   <RetailerCountry>" + vbNewLine + "   </RetailerCountry>" + vbNewLine + "   <Days>" + vbNewLine + "   </Days>" + vbNewLine + _
        "   <DayBack>" + vbNewLine + "   </DayBack>" + vbNewLine + "   <DateIn>" + vbNewLine + "   </DateIn>" + vbNewLine + _
        "   <BrokenCode>" + vbNewLine + "   </BrokenCode>" + vbNewLine + "   <Broken>" + vbNewLine + "   </Broken>" + vbNewLine + _
        "   <TotalCycles>" + vbNewLine + "   </TotalCycles>" + vbNewLine + "</data>" + vbNewLine

 Set doc = CreateObject("MSXML2.DOMDocument")
 doc.async = False
 doc.validateOnParse = False
 doc.resolveExternals = False

 With ActiveWorkbook.Worksheets(1)
  lLastRow = .UsedRange.Rows.Count

  For lRow = 2 To lLastRow
   sFile = .Cells(lRow, 1).Value & ".xml"
   sGrai = .Cells(lRow, 1).Value
   sDayDateOut = .Cells(lRow, 2).Value
   sFiller = .Cells(lRow, 3).Value
   sRetailer = .Cells(lRow, 4).Value
   sRetailerCountry = .Cells(lRow, 5).Value
   sDays = .Cells(lRow, 6).Value
   sDayBack = .Cells(lRow, 7).Value
   sDateIn = .Cells(lRow, 8).Value
   sBrokenCode = .Cells(lRow, 9).Value
   sBroken = .Cells(lRow, 10).Value
   sTotalCycles = .Cells(lRow, 11).Value

   doc.LoadXML sTemplateXML
   doc.getElementsByTagName("Grai")(0).appendChild doc.createTextNode(sGrai)
   doc.getElementsByTagName("DayDateOut")(0).appendChild doc.createTextNode(sDayDateOut)
   doc.getElementsByTagName("Filler")(0).appendChild doc.createTextNode(sFiller)
   doc.getElementsByTagName("RetailerCountry")(0).appendChild doc.createTextNode(sRetailerCountry)
   doc.getElementsByTagName("Retailer")(0).appendChild doc.createTextNode(sRetailer)
   doc.getElementsByTagName("Days")(0).appendChild doc.createTextNode(sDays)
   doc.getElementsByTagName("DayBack")(0).appendChild doc.createTextNode(sDayBack)
   doc.getElementsByTagName("DateIn")(0).appendChild doc.createTextNode(sDateIn)
   doc.getElementsByTagName("BrokenCode")(0).appendChild doc.createTextNode(sBrokenCode)
   doc.getElementsByTagName("Broken")(0).appendChild doc.createTextNode(sBroken)
   doc.getElementsByTagName("TotalCycles")(0).appendChild doc.createTextNode(sTotalCycles)
   doc.Save sFile
  Next

 End With
End Sub

但我在这一行上返回错误:

doc.getElementsByTagName("Grai")(0).appendChild doc.createTextNode(sGrai)

评论:

Error code 91: Object variable or bock variable is not set

你能帮帮我吗?

1 个答案:

答案 0 :(得分:0)

基于Ambi的评论,纠正和工作的vba脚本是:

Sub xmlPerRow()

 sTemplateXML = _
        "<?xml version='1.0'?>" + vbNewLine + "<data>" + vbNewLine + "<Grai>" + vbNewLine + "</Grai>" + vbNewLine + _
        "   <DayDateOut>" + vbNewLine + "   </DayDateOut>" + vbNewLine + "   <Filler>" + vbNewLine + "   </Filler>" + vbNewLine + _
        "   <FillerCountry>" + vbNewLine + "   </FillerCountry>" + vbNewLine + "   <Retailer>" + vbNewLine + "   </Retailer>" + vbNewLine + _
        "   <RetailerCountry>" + vbNewLine + "   </RetailerCountry>" + vbNewLine + "   <Days>" + vbNewLine + "   </Days>" + vbNewLine + _
        "   <DayBack>" + vbNewLine + "   </DayBack>" + vbNewLine + "   <DateIn>" + vbNewLine + "   </DateIn>" + vbNewLine + _
        "   <BrokenCode>" + vbNewLine + "   </BrokenCode>" + vbNewLine + "   <Broken>" + vbNewLine + "   </Broken>" + vbNewLine + _
        "   <TotalCycles>" + vbNewLine + "   </TotalCycles>" + vbNewLine + "</data>" + vbNewLine

 Set doc = CreateObject("MSXML2.DOMDocument")
 doc.async = False
 doc.validateOnParse = False
 doc.resolveExternals = False

 With ActiveWorkbook.Worksheets(1)
  lLastRow = .UsedRange.Rows.Count

  For lRow = 2 To lLastRow
   sFile = "Grai_" & .Cells(lRow, 1).Value & ".xml"
   Dim sGrai As String
   sGrai = CStr(.Cells(lRow, 1).Value)
   sDayDateOut = .Cells(lRow, 2).Value
   sFiller = .Cells(lRow, 3).Value
   sFillerCountry = .Cells(lRow, 4).Value
   sRetailer = .Cells(lRow, 5).Value
   sRetailerCountry = .Cells(lRow, 6).Value
   sDays = .Cells(lRow, 7).Value
   sDayBack = .Cells(lRow, 8).Value
   sDateIn = .Cells(lRow, 9).Value
   sBrokenCode = .Cells(lRow, 10).Value
   sBroken = .Cells(lRow, 11).Value
   sTotalCycles = .Cells(lRow, 12).Value

   doc.LoadXML sTemplateXML
   doc.getElementsByTagName("Grai")(0).appendChild doc.createTextNode(sGrai)
   doc.getElementsByTagName("DayDateOut")(0).appendChild doc.createTextNode(sDayDateOut)
   doc.getElementsByTagName("Filler")(0).appendChild doc.createTextNode(sFiller)
   doc.getElementsByTagName("RetailerCountry")(0).appendChild doc.createTextNode(sRetailerCountry)
   doc.getElementsByTagName("Retailer")(0).appendChild doc.createTextNode(sRetailer)
   doc.getElementsByTagName("Days")(0).appendChild doc.createTextNode(sDays)
   doc.getElementsByTagName("DayBack")(0).appendChild doc.createTextNode(sDayBack)
   doc.getElementsByTagName("DateIn")(0).appendChild doc.createTextNode(sDateIn)
   doc.getElementsByTagName("BrokenCode")(0).appendChild doc.createTextNode(sBrokenCode)
   doc.getElementsByTagName("Broken")(0).appendChild doc.createTextNode(sBroken)
   doc.getElementsByTagName("TotalCycles")(0).appendChild doc.createTextNode(sTotalCycles)
   doc.Save sFile
  Next

 End With
End Sub