0

在以下代码中,我在 Excel 中使用 VBA 时遇到错误:

Private Sub XMLGen(mapRangeA, mapRangeB, ticketSize, mapping)
    Dim fieldOneArr As Variant
    Dim fieldTwoArr As Variant
    Dim row As Long
    Dim column As Long
    Dim infoCol As Long
    Dim endInfo As Long
    Dim objDom As DOMDocument
    Dim objNode As IXMLDOMNode
    Dim objXMLRootelement As IXMLDOMElement
    Dim objXMLelement As IXMLDOMElement
    Dim objXMLattr As IXMLDOMAttribute
    Set ws = Worksheets("StockData")
    Dim wsName As String

    Set objDom = New DOMDocument

    If ticketSize = 8 Then
        wsName = "A7Tickets"
    ElseIf ticketSize = 16 Then
        wsName = "A8Tickets"
    Else
        wsName = "A5Tickets"
    End If

    Set ps = Worksheets(wsName)

   'create processing instruction
   Set objNode = objDom.createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'")
   objDom.appendChild objNode

   'create root element
   Set objXMLRootelement = objDom.createElement("fields")
   objDom.appendChild objXMLRootelement

   'create Attribute to the Field Element and set value
   Set objXMLattr = objDom.createAttribute("xmlns:xfdf")
   objXMLattr.NodeValue = "http://ns.adobe.com/xfdf-transition/"
   objXMLRootelement.setAttributeNode objXMLattr

   infoCol = 1
   fieldOneArr = Worksheets(mapping).range(mapRangeA)
   fieldTwoArr = Worksheets(mapping).range(mapRangeB)
   For row = 1 To UBound(fieldOneArr, 1)
        For column = 1 To UBound(fieldOneArr, 2)
            'create Heading element
            Set objXMLelement = objDom.createElement(fieldOneArr(row, column))
            objXMLRootelement.appendChild objXMLelement

            'create Attribute to the Heading Element and set value
            Set objXMLattr = objDom.createAttribute("xfdf:original")
            objXMLattr.NodeValue = (fieldTwoArr(row, column))
            objXMLelement.setAttributeNode objXMLattr
            objXMLelement.Text = ps.Cells(row, infoCol)
            infoCol = infoCol + 1
            endInfo = endInfo + 1
            If endInfo = 4 Then
                infoCol = 1
            End If
        Next column
    Next row

    'save XML data to a file
    If ticketSize = 2 Then
        objDom.Save ("C:\ExportTestA5.xml")
        MsgBox "A5 XML created"
    ElseIf ticketSize = 8 Then
        objDom.Save ("C:\ExportTestA7.xml")
        MsgBox "A7 XML created"
    Else
        objDom.Save ("C:\ExportTestA8.xml")
        MsgBox "A8 XML created"
    End If

End Sub

当我点击调试时,它指向这一行:

fieldOneArr = Worksheets(mapping).range(mapRangeA)

我知道 .Range 应该是大写的,但只要我更正它,它就会自动将其设置为小写。

此代码旨在创建一个 XML 文件,然后将所选工作表中的详细信息(基于 ticketSize 变量)写入正确的 XML 字段。因此,我有一个映射工作表,我从中写入字段和属性名称,然后将正确票证大小工作表中的信息写入元素的文本属性。

4

1 回答 1

2

在这种情况下,您应该定义函数参数的类型mapRangeA As String。Office 对象方法和属性通常对它们的错误消息不是很有帮助,因此如果参数有问题,最好出现类型不匹配错误。

于 2013-03-16T13:51:21.390 回答