VBScript 从桌面运行文件(任何用户) [英] VBScript to run a file from desktop (any user)

查看:49
本文介绍了VBScript 从桌面运行文件(任何用户)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个 VBScript 来在不打开文件的情况下从 excel 运行宏.

I've created a VBScript to run a macro from excel without opening the file.

Set objExcel = CreateObject("Excel.Application")  
objExcel.Application.Run "'C:\Users\MyUser\Desktop\RM.xlsm'!Module4.Email"  
objExcel.DisplayAlerts = False  
objExcel.Application.Quit  
Set objExcel = Nothing

我想使用这个 VBScript &XLSM 文件在不同的计算机上,那么如何更改此脚本以使其工作而无需每次都编辑路径?
(可能是从当前文件夹运行的代码或从任何用户桌面运行的代码)

I want to use this VBScript & XLSM file on different computers, so how can i change this script to work without editing the path every time?
(Maybe a code to run from current folder or a code to run from any user desktop)

推荐答案

如果文件将始终在每个用户的桌面上,那么您可以使用环境变量来确定位置.

If the file will always be on the desktop of every user then you can use environment variables to determine the location.

Set wshShell = CreateObject( "WScript.Shell" )
userName = wshShell.ExpandEnvironmentStrings( "%UserName%" )

path = "'C:\Users\" + userName + "\Desktop\RM.xlsm'!Module4.Email"
Set objExcel = CreateObject("Excel.Application")  
objExcel.Application.Run path
objExcel.DisplayAlerts = False  
objExcel.Application.Quit  
Set objExcel = Nothing

(未经测试的代码)

如果文件不在每个用户的桌面上,那么您需要将它存储在网络上的一个公共位置,并让每个用户从那里访问它,例如在 .

If the file is not on each user's desktop then you'll need to store it in a common location on the network and have each user access it from there, e.g. in .

\\YourFileSever\SharedFiles\RM.xlsm

实际上,后者更可取,因为这意味着工作簿只在一个地方,而在发布新版本时,您只需更新一份

In practical terms the latter is preferable as it means the workbook is in only one place and when it comes to releasing a new version you only have to update one copy

这篇关于VBScript 从桌面运行文件(任何用户)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆