2014-02-21 43 views
0

我正在開發一個項目,重新編寫以前創建的內容。基本上它是一個帶有應用程序安裝按鈕選項的HTA。目前它非常長並且重複性很強,所以我正在努力清理它。基於按鈕點擊設置VBscript變量

我有下面的代碼,如果我將FilePath設置爲函數值(FilePath = ApplicationOne()),它將起作用。我需要能夠根據按鈕選項設置FilePath。

我基本上需要一個文件位置分配給FilePath基於單擊按鈕。它可以被設置爲與下面不同,但它確實需要分離,因爲應用程序安裝有更多的內容,然後顯示在下面。這樣做會創建一個更清潔的設計,並使未來的更新變得更容易。

如何做到這一點有什麼想法?

Sub ApplicationInstall 
    Dim FilePath  
    Set WshShell = CreateObject("WScript.Shell") 
    FilePath = "SomethingHere" '<---- Change This 
    WshShell.Run FilePath, 1, true 
End Sub 









Function ApplicationOne() 
    strPath = "\\This\is\a\file.cmd" 
    ApplicationOne = strPath 
End Function 

Function ApplicationTwo() 
    strPath = "\\This\is\a\file.cmd" 
    ApplicationTwo = strPath 
End Function 

Function ApplicationThree() 
    strPath = "\\This\is\a\file.cmd" 
    ApplicationThree = strPath 
End Function 

回答

0

您可以定義一個包含活動應用程序路徑的全局變量(在HTA中的任何函數之外)。您的按鈕單擊事件可以更新此變量的值,並且您的ApplicationInstall()子可以讀取它。它本質上充當你的模塊的一個屬性。

<script language="vbscript"> 

Dim m_strPath ' Page/Module-level variable 

Sub cmdButton1_Click() 
    m_strPath = <application path 1> 
End Sub 

Sub cmdButton2_Click() 
    m_strPath = <application path 2> 
End Sub 

Sub ApplicationInstall() 
    If Len(m_strPath) > 0 Then 
     CreateObject("WScript.Shell").Run Chr(34) & m_strPath & Chr(34), 1, True 
    End If 
End Sub 

</script> 
+0

這工作完美,我知道這將是簡單的事情......非常感謝你 – huviduc

+0

你非常歡迎! – Bond