运行时错误91 - 对象变量或未设置块变量 - 从Excel更新XML

时间:2016-07-19 12:13:49

标签: xml excel vbscript

我收到运行时错误91 - 当我尝试使用Excel Vbscript更新和XML时未设置对象变量或With块变量

    Sub UpdateXML()
    Call fnUpdateXMLByTags
    End Sub

    Function fnUpdateXMLByTags()

    Set xmldoc = CreateObject("Microsoft.XMLDOM")
    xmldoc.Load "F:\XML\XML1.xml"
    oMsgId = "15544216089S01F15544100002396000002"
    Set nMsgId = xmldoc.SelectSingleNode("//AcctSwtchBtch/GrpHdr/MsgId")
    nMsgId.Text = oMsgId
    strResult = xmldoc.Save("F:\XML\XML1.xml")

    End Function

2 个答案:

答案 0 :(得分:0)

函数必须返回一些东西。例如:

Function FileExists(FilePath As String, FileName As String) As Boolean

Dim CurrentDir As String
CurrentDir = CurDir ' Used to set the CurDir back to what it was

If FilePath <> "" Then ChDir FilePath

If Dir(FileName) <> "" Then
    FileExists = True
Else
    FileExists = False
End If
ChDir CurrentDir

End Function

我建议将它从一个函数更改为一个子函数。

答案 1 :(得分:0)

我想你没有昏暗的设置:

Dim xmldoc As Object
Set xmldoc = CreateObject("Microsoft.XMLDOM")
...

我不确定nMsgId类型,所以请注意&#34;设置nMsgId&#34;并且:

Debug.Print TypeName(xmldoc.SelectSingleNode("//AcctSwtchBtch/GrpHdr/MsgId"))

使用正确的类型,您可以创建正确的nMsgId实例