vba excel:需要加载一个xml文件并写入特定值(从当前获取)excel然后保存它

时间:2014-01-28 01:16:18

标签: xml excel vba excel-vba

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

我的目标是:对于最终用户,他必须在excel文件中输入框架路径和其他有用的值并选择运行测试,然后代码将获取这些值,加载和xml文件,将这些值输入到xml中,保存并启动应用程序。这些值将被调用到Application中的其他脚本使用。

我的pb似乎是excel没有加载所有的xml(甚至继续调试,他说“运行时错误,无法找到指定的路径;在代码下面使用,也是xml格式:< / p>

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>

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

对于很长的文字感到抱歉,希望尽可能清楚...我很感激如何解决这个问题的帮助,尤其是了解如何在任何时候这样做。感谢。

1 个答案:

答案 0 :(得分:0)

如果envFrmwrkPath的值不以\结尾,那么您的路径很可能会被破坏。例如,如果envFrmwrkPathC:\Shared Documents,则您尝试加载/保存的路径为:

C:\Shared DocumentsEnvironment\EnvVar.xml

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