这是将excel单元格写入XML文件的主要功能的标题。我想让它调用另一个函数,它可以自己编写一组函数。
Public Sub WriteXML()
Dim Sheet As Worksheet
Dim Cell As Range
Dim xmlFile
xmlFile = ThisWorkbook.Path & "\" & 'Test1' & ".xml"
Set Sheet = ActiveWorkbook.Worksheets("Sht1")
Open xmlFile For Output As #1
Print #1, "<?xml version=" & Chr(34) & "1.0" & Chr(34) & _
" encoding=" & Chr(34) & "UTF-8" & Chr(34) & "?>"
Call WriteCustomer(xmlFile)
这是第二个函数的开始,虽然我得到了一个'找不到对象'的错误。
Sub WriteCustomer(x As Variant)
Print x, " <Customer>"
Print x, " <First>" & 'Bill' & "</First>"
Print x, " <Last>" & 'Johnson' & "</Last>"
Print x, " </Customer>"
Print x, ""
End Sub
如何构建调用和/或变量以将打开的文件作为对象传递给第二个函数?
答案 0 :(得分:3)
您可以按如下方式请求,存储和传递句柄:
Dim handle As Integer
handle = FreeFile()
Open xmlFile For Output As #handle
Print #handle, "<?xml version=" & Chr(34) & "1.0" & Chr(34) & _
...
Call WriteCustomer(handle)
和
Sub WriteCustomer(handle As Integer)
Print #handle, " <Customer>"
答案 1 :(得分:1)
由于您已使用行
在第一个函数中打开了该文件Open xmlFile For Output As #1
任何在打开时引用#1的代码都会写入同一个文件。因此,您只需将第二个函数重写为
即可Sub WriteCustomer()
Print #1, " <Customer>"
Print #1, " <First>" & 'Bill' & "</First>"
Print #1, " <Last>" & 'Johnson' & "</Last>"
Print #1, " </Customer>"
Print #1, ""
End Sub