2014-01-28 44 views
2

與VBA,xml和一般編程完全是新的,我只是試圖從很多網站獲取信息,但似乎像我我無法解決我的問題,它是:vba excel:需要加載一個xml文件並寫入特定的值(從當前獲得)然後保存它

我的目標是:對於最終用戶,他必須輸入框架路徑和其他有用的值到Excel文件並選擇運行測試,然後代碼將得到這些值,加載和xml文件,將這些值輸入到xml中,保存並啓動應用程序。這些值將被調用到應用程序中的其他腳本使用。

我的pb就像是excel沒有加載所有的xml(甚至繼續調試,他說「運行時錯誤,無法找到指定的路徑;下面的代碼正在使用以及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> 

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

對不起,很長的文字,希望儘可能清楚...我感謝如何解決此問題的幫助,但尤其要了解如何在任何時間執行此操作。謝謝。

回答

0

如果envFrmwrkPath的值不以\結尾,那麼您的路徑很可能會被損壞。例如,如果envFrmwrkPathC:\Shared Documents,那麼你要加載路徑/保存將是:

C:\Shared DocumentsEnvironment\EnvVar.xml

確保值在單元格E6與\結束,然後再試一次

+0

感謝您的幫助...我改變了你的建議,但它沒有奏效,同樣的情況下...我複製了一些f代碼以上只測試加載,我添加了一個conditonnel msgbox來顯示它是否加載或不加載,總是這樣指向非負載。我還禁用了框架路徑聲明,並將完整路徑添加到加載函數,並且文件加載正確。我不知道爲什麼它不與第一個案件... – user3232996

+0

它的工作,你是對的...我是完全錯誤的...真的很感謝你「barrowc」...其實,2種方式解決了pb:EnvVarXML.Load(envFrmwrkPath&「\ Environment \ EnvVar.xml」)而不是EnvVarXML.Load(envFrmwrkPath&「Environment \ EnvVar.xml」)(這是我將使用的方式,因爲我無法找到如何強制最終用戶在文件夾路徑的末尾輸入\(即使我相信這是可行的)...再次感謝! – user3232996

相關問題