VBA的Excel中:需要加载一个XML文件,并写入特定的值(从当前得到)Excel然后保存

我完全用VBA,XML和一般编程新,我只是试图从许多网站获得信息,但似乎我无法解决我的问题,这是:

我的目标是:对于最终用户,他必须将框架path和其他有用的值input到excel文件中并select运行testing,然后代码将得到这些值,load和xml文件,将这些值input到xml中,并保存它开始申请。 这些值将被调用到应用程序中的其他脚本使用。

我的PB是,似乎是不是所有的XML加载(甚至继续debugging,他说:“运行时错误,无法find指定的path;下面的代码使用和XML格式:

Option Explicit Private Sub RunTest_Click() Dim envFrmwrkPath As String Dim ApplicationName As String Dim TestIterationName As String Dim objfso, app, Eval As Object Dim i, Msgarea envFrmwrkPath = ActiveSheet.Range("E6").Value ApplicationName = ActiveSheet.Range("E4").Value TestIterationName = ActiveSheet.Range("E8").Value Set EnvVarXML = CreateObject("Microsoft.XMLDOM") EnvVarXML.Load (envFrmwrkPath & "Environment\EnvVar.xml") For Each UIElement In EnvVarXML.SelectNodes("Environment/Variable") Set Field = Eval(objUIElement.SelectSingleNode("Name").Text) Field.Value = UIElement.SelectSingleNode("Value").Text Set EnvVarXML = CreateObject("Microsoft.XMLDOM") EnvVarXML.Load (envFrmwrkPath & "Environment\EnvVar.xml") Next Set EnvVarXML = Nothing Set objfso = CreateObject("Scripting.FileSystemObject") If Not objfso.FolderExists(envFrmwrkPath) Then MsgBox envFrmwrkPath & "- Folder not found. Setting not saved" Exit Sub End If Set objfso = Nothing Set EnvVarXML = CreateObject("Microsoft.XMLDOM") EnvVarXML.Load (envFrmwrkPath & "Environment\EnvVar.xml") For Each UIElement In EnvVarXML.SelectNodes("Environment/Variable") Set Field = Eval(UIElement.SelectSingleNode("Name").Text) UIElement.SelectSingleNode("Value").Text = Field.Value If UIElement.SelectSingleNode("Name").Text = "envFrmwrkPath" Then Application.DisplayAlerts = False Set app = CreateObject("QuickTest.Application") app.Launch app.Visible = True app.WindowState = "Maximized" app.Open envFrmwrkPath & "\Driver", False app.Folders.RemoveAll app.Folders.Add (envFrmwrkPath) app.Test.Settings.Resources.DataTablePath = "<Default>" app.Test.Settings.Resources.Libraries.RemoveAll app.Test.Settings.Resources.Libraries.Add (envFrmwrkPath & _ "FunctionLibrary\VTL_Util_Lib.vbs") app.Test.Settings.Resources.Libraries.Add (envFrmwrkPath & _ "FunctionLibrary\VTL_BP_Lib.vbs") app.Test.Settings.Resources.Libraries.Add (envFrmwrkPath & _ "FunctionLibrary\VTL_Engine_Lib.vbs") app.Test.Settings.Resources.Libraries.Add (Field.Value & _ "FunctionLibrary\RecoveryScenario.qfl") If app.Test.Settings.Recovery.Count > 0 Then app.Test.Settings.Recovery.RemoveAll End If app.Options.Run.RunMode = "Fast" app.Options.Run.ViewResults = False For i = 1 To app.Test.Actions.Count app.Test.Actions(i).ObjectRepositories.RemoveAll app.Test.Actions(i).ObjectRepositories.Add (envFrmwrkPath & _ "Ressources\ObjectRepository\shared_repository.tsr") Next app.Test.Save End If Next ' here i got the error but the pb is earlier i think! EnvVarXML.Save (envFrmwrkPath & "Environment\EnvVar.xml") End Sub 

============

该XML文件如下所示:

 <?xml version="1.0" encoding="UTF-8" standalone="no"?> <Environment> <Variable> <Name>envversion</Name> <Caption>version :</Caption> <Type>TEXT</Type> <Value></Value> <Description>Version</Description> </Variable> <Variable> <Name>envAppName</Name> <Caption>Application name :</Caption> <Type>TEXT</Type> <Value></Value> <Description>Name of the Application Under Test</Description> </Variable> <Variable> <Name>envFrmwrkPath</Name> <Caption>Framework folder path:</Caption> <Type>TEXT</Type> <Value></Value> <Description>Path of the automation framework</Description> </Variable> <Variable> <Name>envIP</Name> <Caption>IP Address:</Caption> <Type>TEXT</Type> <Value></Value> <Description>IP Address</Description> </Variable> <Variable> <Name>envLogin</Name> <Caption>Login:</Caption> <Type>TEXT</Type> <Value></Value> <Description>Login</Description> </Variable> <Variable> <Name></Name> <Caption>Password:</Caption> <Type>TEXT</Type> <Value></Value> <Description>Password</Description> </Variable> <Variable> <Name>envLogs</Name> <Caption>Logs:</Caption> <Type>TEXTAREA</Type> <Value></Value> <Description>logs</Description> </Variable> <Variable> <Name>envTestIteration</Name> <Caption>Test iteration name:</Caption> <Type>TEXT</Type> <Value></Value> <Description>TestIterationName Subfolder to be created</Description> </Variable> </Environment> 

=====================

对不起,很长的文字,要尽可能清楚…我感谢如何解决这个问题的帮助,但特别是要了解如何做到这一点在任何时候。 谢谢。

如果envFrmwrkPath的值不以\结尾,那么您的path很可能会被损坏。 例如,如果envFrmwrkPathC:\Shared Documents那么您尝试加载/保存的path将是:

C:\Shared DocumentsEnvironment\EnvVar.xml

确保单元格E6中的值以\结尾,然后重试