简体   繁体   English

如何从另一个脚本访问 VBScript 之外的变量?

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

I created a VBScript that accesses a bookmark in a word document:我创建了一个 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("~") 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("~")

I want this part of the code: (C:\\Users\\1241\\Downloads\\HotelDel__advance.docm) to be a variable that I access outside of this script, so that it would look like:我希望这部分代码: (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("~")`

where Hotel is the variable.其中 Hotel 是变量。

I want this variable to equal the following:我希望这个变量等于以下内容:

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

I need to be able to set this variable outside of this script, and be able to access this variable from within this script.我需要能够在这个脚本之外设置这个变量,并且能够从这个脚本内访问这个变量。

In other words, I want to create a bat or js or a vba or a vbs Script that sets this换句话说,我想创建一个 bat 或 js 或一个 vba 或一个 vbs 脚本来设置这个

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

Variable, so I can access it from my other scripts.变量,所以我可以从我的其他脚本访问它。

How do I make this Variable available to other scripts, and how do I access this variable from within the scripts?如何使此变量可用于其他脚本,以及如何从脚本中访问此变量?

I have been working this issue on my own for about 3 months now, and I have been unable to figure it out.我已经自己解决这个问题大约 3 个月了,但我一直无法弄清楚。 Any ideas?有什么想法吗? Thanks谢谢

You can define Hotel variable in Windows registry.您可以在 Windows 注册表中定义 Hotel 变量。 For example, HKEY_LOCAL_MACHINE\\Software\\Scott\\Hotel例如,HKEY_LOCAL_MACHINE\\Software\\Scott\\Hotel

You need a process to set value to Hotel variable:您需要一个过程来将值设置为 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)

You must adapt your code to read Hotel variable from registry:您必须调整代码以从注册表读取 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