簡體   English   中英

如何從另一個腳本訪問 VBScript 之外的變量?

[英]How can I access a Variable outside of a VBScript from another Script?

我創建了一個 VBScript 來訪問 Word 文檔中的書簽:

`Set oShell = CreateObject("WScript.Shell")`

CreateObject("WScript.Shell").Run("""C:\\Users\\1241\\Downloads\\HotelDel__advance.docm""") WScript.Sleep (1000) WScript.CreateObject("WScript.Shell").SendKeys("%m") WScript.CreateObject("WScript.Shell").SendKeys("Beachservices") WScript.CreateObject("WScript.Shell").SendKeys("~")

我希望這部分代碼: (C:\\Users\\1241\\Downloads\\HotelDel__advance.docm)成為我在此腳本之外訪問的變量,因此它看起來像:

`Set oShell = CreateObject("WScript.Shell")
 CreateObject("WScript.Shell").Run(""Hotel"")
 WScript.Sleep (1000)
 WScript.CreateObject("WScript.Shell").SendKeys("%m")
 WScript.CreateObject("WScript.Shell").SendKeys("Beachservices")
 WScript.CreateObject("WScript.Shell").SendKeys("~")`

其中 Hotel 是變量。

我希望這個變量等於以下內容:

`Hotel = C:\Users\1241\Downloads\HotelDel__advance.docm`

我需要能夠在這個腳本之外設置這個變量,並且能夠從這個腳本內訪問這個變量。

換句話說,我想創建一個 bat 或 js 或一個 vba 或一個 vbs 腳本來設置這個

`Hotel = C:\Users\1241\Downloads\HotelDel__advance.docm`

變量,所以我可以從我的其他腳本訪問它。

如何使此變量可用於其他腳本,以及如何從腳本中訪問此變量?

我已經自己解決這個問題大約 3 個月了,但我一直無法弄清楚。 有什么想法嗎? 謝謝

您可以在 Windows 注冊表中定義 Hotel 變量。 例如,HKEY_LOCAL_MACHINE\\Software\\Scott\\Hotel

您需要一個過程來將值設置為 Hotel 變量:

Const HKEY_LOCAL_MACHINE = &H80000002

Dim oReg
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")

strKeyPath = "SOFTWARE\Scott\"
KeyPath = "SOFTWARE\Scott"
strValueName = "Hotel"
' strValue = "C:\Users\1241\Downloads\HotelDel__advance.docm"
strValue =Inputbox("Hotel variable value","Scott program")

' Create key to use
objReg.CreateKey(HKEY_LOCAL_MACHINE, KeyPath)
objReg.SetStringValue(HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue)

您必須調整代碼以從注冊表讀取 Hotel 變量:

Const HKEY_LOCAL_MACHINE = &H80000002

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Scott"
strValueName = "Hotel"
strHotel = ""
oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strHotel

...

CreateObject("WScript.Shell").Run(strHotel)
 WScript.Sleep (1000)
 WScript.CreateObject("WScript.Shell").SendKeys("%m")
 WScript.CreateObject("WScript.Shell").SendKeys("Beachservices")
 WScript.CreateObject("WScript.Shell").SendKeys("~")`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM