Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 尝试将数据从XLS导出到命名XML文件时出现运行时错误_Excel_Vba - Fatal编程技术网

Excel 尝试将数据从XLS导出到命名XML文件时出现运行时错误

Excel 尝试将数据从XLS导出到命名XML文件时出现运行时错误,excel,vba,Excel,Vba,我已经编写了我的第一个VBA宏,将数据行从XLS导出到特定位置的单独命名XML文件。我的初始简化测试进行得很顺利,但完整版本出现错误,消息如下: 运行时错误91:未设置对象变量或带块变量 宏: Sub XML() sTemplateXML = _ "<?xml version='1.0'?>" + vbNewLine + _ "<FromEmail>" + "</FromEmail>" + vbNewLine + _ "<FromName>

我已经编写了我的第一个VBA宏,将数据行从XLS导出到特定位置的单独命名XML文件。我的初始简化测试进行得很顺利,但完整版本出现错误,消息如下:

运行时错误91:未设置对象变量或带块变量

宏:

  Sub XML()
 sTemplateXML = _
"<?xml version='1.0'?>" + vbNewLine + _
 "<FromEmail>" + "</FromEmail>" + vbNewLine + _
 "<FromName>" + "</FromName>" + vbNewLine + _
 "<ToEmail>" + "</ToEmail>" + vbNewLine + _
 "<CCAddresses>" + "</CCAddresses>" + vbNewLine + _
 "<BCCAddresses>" + "</BCCAddresses>" + vbNewLine + _
 "<ReplyTo>" + "</ReplyTo>" + vbNewLine + _
 "<Subject>" + "</Subject>" + vbNewLine + _
 "<Body>" + "</Body>" + vbNewLine + _
 "</EmailValues>" + vbNewLine

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

With ActiveWorkbook.ActiveSheet
lLastRow = ActiveWorkbook.ActiveSheet.UsedRange.Rows.Count

For lRow = 2 To lLastRow
sFile = ActiveWorkbook.ActiveSheet.Cells(lRow, 13).Value
sfromemail = ActiveWorkbook.ActiveSheet.Cells(lRow, 3).Value
sFromname = ActiveWorkbook.ActiveSheet.Cells(lRow, 2).Value
sToEmail = ActiveWorkbook.ActiveSheet.Cells(lRow, 5).Value
sCCAddresses = ActiveWorkbook.ActiveSheet.Cells(lRow, 7).Value
sBCCAddresses = ActiveWorkbook.ActiveSheet.Cells(lRow, 8).Value
sReplyTo = ActiveWorkbook.ActiveSheet.Cells(lRow, 4).Value
sSubject = ActiveWorkbook.ActiveSheet.Cells(lRow, 11).Value
sBody = ActiveWorkbook.ActiveSheet.Cells(lRow, 12).Value


doc.LoadXML sTemplateXML
doc.getElementsbyTagName("FromEmail")(0).appendChild 
doc.createTextNode(sfromemail) (ERROR POPS UP HERE)
doc.getElementsbyTagName("FromName")(0).appendChild 
doc.createTextNode(sFromname)
doc.getElementsbyTagName("ToEmail")(0).appendChild 
doc.createTextNode(sToEmail)
doc.getElementsbyTagName("CCAddresses")(0).appendChild 
doc.createTextNode(sCCAddresses)
doc.getElementsbyTagName("BCCAddresses")(0).appendChild 
doc.createTextNode(sBCCAddresses)
doc.getElementsbyTagName("ReplyTo")(0).appendChild 
doc.createTextNode(sReplyTo)
doc.getElementsbyTagName("Subject")(0).appendChild 
doc.createTextNode(sSubject)
doc.getElementsbyTagName("Body")(0).appendChild doc.createTextNode(sBody)

doc.Save sFile
Next

End With
End Sub
Sub-XML()
sTemplateXML=_
“”+vbNewLine+_
“”+“”+vbNewLine+_
“”+“”+vbNewLine+_
“”+“”+vbNewLine+_
“”+“”+vbNewLine+_
“”+“”+vbNewLine+_
“”+“”+vbNewLine+_
“”+“”+vbNewLine+_
“”+“”+vbNewLine+_
“”+vbNewLine
Set doc=CreateObject(“MSXML2.DOMDocument”)
doc.async=False
doc.validateOnParse=False
doc.resolveExternals=False
使用ActiveWorkbook.ActiveSheet
lLastRow=ActiveWorkbook.ActiveSheet.UsedRange.Rows.Count
对于lRow=2至lLastRow
sFile=ActiveWorkbook.ActiveSheet.Cells(lRow,13).Value
sfromemail=ActiveWorkbook.ActiveSheet.Cells(lRow,3).Value
sFromname=ActiveWorkbook.ActiveSheet.Cells(lRow,2).Value
sToEmail=ActiveWorkbook.ActiveSheet.Cells(lRow,5).Value
SCCADRESS=Active工作簿.ActiveSheet.Cells(lRow,7).Value
sbccaddress=ActiveWorkbook.ActiveSheet.Cells(lRow,8).Value
sReplyTo=ActiveWorkbook.ActiveSheet.Cells(lRow,4).Value
SSObject=ActiveWorkbook.ActiveSheet.Cells(lRow,11).Value
sBody=ActiveWorkbook.ActiveSheet.Cells(lRow,12).Value
doc.LoadXML sTemplateXML
doc.getElementsbyTagName(“FromEmail”)(0.appendChild
doc.createTextNode(sfromemail)(此处弹出错误)
doc.getElementsbyTagName(“FromName”)(0.appendChild
doc.createTextNode(sFromname)
doc.getElementsbyTagName(“ToEmail”)(0.appendChild
doc.createTextNode(sToEmail)
doc.getElementsbyTagName(“CCAddresss”)(0.appendChild
doc.createTextNode(SCCADRESS)
doc.getElementsbyTagName(“BCCadAddresses”)(0.appendChild
doc.createTextNode(SBCCAddresss)
doc.getElementsbyTagName(“ReplyTo”)(0.appendChild
doc.createTextNode(sReplyTo)
文件getElementsbyTagName(“主题”)(0).appendChild
doc.createTextNode(SSObject)
doc.getElementsbyTagName(“正文”)(0).appendChild doc.createTextNode(sBody)
保存文件
下一个
以
端接头

只需进行一点重构,就可以更加整洁:

Sub SaveRowsToXml()
    Dim lLastRow As Long, lrow As Long
    With ActiveWorkbook.ActiveSheet
        lLastRow = .UsedRange.Rows.Count
        For lrow = 2 To lLastRow
            SaveToXml .Rows(lrow)
        Next lrow
    End With
End Sub

'save a single row of data as XML
Sub SaveToXml(rw As Range)
    Dim doc As Object, r
    Set doc = CreateObject("MSXML2.DOMDocument")
    doc.appendChild doc.createProcessingInstruction("xml", "version='1.0'")
    Set r = doc.createElement("EmailValues") '<< create the root element
    doc.appendChild r
    'append child nodes
    r.appendChild NodeWithContent(doc, "FromEmail", rw.Cells(3).Value)
    r.appendChild NodeWithContent(doc, "FromName", rw.Cells(2).Value)
    r.appendChild NodeWithContent(doc, "ToEmail", rw.Cells(5).Value)
    r.appendChild NodeWithContent(doc, "CCAddresses", rw.Cells(7).Value)
    'etc
    'etc
    'Debug.Print doc.XML
    doc.Save rw.Cells(13).Value
    Set doc = Nothing
End Sub

'return a named element with content
Function NodeWithContent(doc, nodename, content)
    Dim el
    Set el = doc.createElement(nodename)
    el.appendChild doc.createTextNode(content)
    Set NodeWithContent = el
End Function
子存储行stoxml()
昏暗的人影和长的一样,低的和长的一样
使用ActiveWorkbook.ActiveSheet
lLastRow=.UsedRange.Rows.Count
对于lrow=2至lLastRow
SaveToXml.Rows(lrow)
下一条路
以
端接头
'将单行数据另存为XML
Sub SaveToXml(rw As范围)
作为对象的Dim doc,r
Set doc=CreateObject(“MSXML2.DOMDocument”)
doc.appendChild doc.createProcessingInstruction(“xml”,“版本='1.0'))

设置r=doc.createElement(“EmailValues”)'警报,警报!-有一种更简单的方法可以将Excel转换为XML—我建议您看一看,并根据其中编写的样板代码修改代码。@dwirony嘿,谢谢您的回答:)如果我想将数据行转换为单独的XML文件,这是否适用?是的。我们的想法是,不要使用元素/childs/nodes,而是一行一行地打印XML文件。@dIrony嗨,对不起,我对此非常陌生。我是否可以在该站点上根据我在excel(电子邮件)中输入的数据运行marco并运行宏?抱歉,超级新的仍在尝试学习:)!您的文档缺少一个期初