简体   繁体   中英

vba excel : need to load an xml file and write specific values (got from current) excel then save it

am totally new with VBA, xml and general programming, i just tried to get information from many sites, but seems like i am unable to solve my problem, which is:

my target is : For the end user, he has to enter framework path and other useful values into excel file and select run test, then the code will get these values, load and xml file, enter these values into the xml, save it and start application. these values will be used by other script called into the Application.

my pb is that seems like excel is not loading at all the xml (and even continue debugging, he is saying "run time error, unable to find the path specified ; below the code am using and also the xml format:

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

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

the xml file is like following:

<?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>

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

Sorry for the very long text, want to be clear as possible ... i appreciate help on how to resolve this but especially on to understand how to do this at any time. Thanks.

If the value of envFrmwrkPath doesn't end with \\ then your path will most likely be mangled. For example, if envFrmwrkPath was C:\\Shared Documents then the path you are trying to load/save would be:

C:\\Shared DocumentsEnvironment\\EnvVar.xml

Make sure the value in cell E6 ends with a \\ and try again

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM