gpt4 book ai didi

excel - 从Windows批处理脚本以安全模式启动Excel文件,使用默认文件关联

转载 作者:可可西里 更新时间:2023-11-01 11:24:18 26 4
gpt4 key购买 nike

问题摘要:
我可以在安全模式下从Windows批处理脚本启动Excel文件installer.xlsm,而不提供Excel.exe安装路径吗?
细节
我有一个windows批处理脚本,它可以从远程服务器下载一系列excel加载项的最新版本,将它们放在一个目录(c:\appname\add ins)中,并调用excel文件installer.xlsm。
加载时,installer.xlsm执行VBA宏,该宏卸载旧版本的加载项并安装其新版本。
当前,我使用以下命令启动installer.xlsm:

start "Launching installer file" /wait "<Path to file>\Installer.xlsm"

它的优点在于它使用windows的文件关联来打开excel,而且我不需要提供excel.exe安装路径(多个用户具有不同的计算机映像和ms-office版本)。
现在我想在安全模式下加载installer.xlsm,以确保在installer.xlsm尝试使用外接程序时没有加载任何外接程序,也没有运行任何其他代码。
我知道我可以使用 "<PathToExcel>excel" /safemode "<PathToXls>Installer.xlsm",如 this answer中所述,但此方法不使用windows的文件关联,并且要求我提供路径。
我们有不同机器图像的用户,使用不同版本的ms office,所以我不想进入硬编码所有可能的excel安装位置。
我能做如下的事情吗:
start "Launching installer file" /wait "<Path to file>\Installer.xlsm /safemode"

我尝试了不同的组合但没有成功。你会怎么做?

最佳答案

首先,我建议阅读Microsoft TechNet关于Application Registration的文章。它解释了应用程序或应用程序套件(如Microsoft Office)的安装程序应如何注册已安装的应用程序,以便其他应用程序可以找到应用程序的可执行文件。
建议在注册表项下创建

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths

一个可执行文件名为 excel.exe的子键,默认字符串值是具有完整路径的可执行文件名,还可以选择添加一个名为 Path的字符串值,其中仅包含可执行文件的路径。 Path字符串可以但大多数不存在,并且可以但不能以反斜杠结尾。
命令start还使用此键查找应用程序,如 Where is “START” searching for executables?
各种版本的Microsoft Office的安装程序也在此注册表项下注册 excel.exe注册表项。
因此,在Windows Vista和更高版本的Windows上,获取Microsoft Excel安装位置的最简单方法是:
@echo off
for /F "skip=1 tokens=2*" %%A in ('%SystemRoot%\System32\reg.exe QUERY "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\excel.exe" /ve 2^>nul') do set "ExcelApp=%%~B"
echo ExcelApp=%ExcelApp%
pause

但在Windows XP上, reg.exe的输出不同,因此需要以下批处理代码:
@echo off
for /F "skip=3 tokens=3*" %%A in ('%SystemRoot%\System32\reg.exe QUERY "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\excel.exe" /ve 2^>nul') do set "ExcelApp=%%~B"
echo ExcelApp=%ExcelApp%
pause

在为获取包含空格的注册表项的默认字符串的字符串值而编写的批处理代码中,在 Read words separated by space & string value also contains space in a batch script上对不同的输出进行了解释。
最好的编码实践是添加额外的代码来处理一个错误情况,比如注册表项根本不存在,因为根本没有安装Microsoft Excel。
但是,使用批处理代码可以分别执行哪些命令启动windows shell函数 ShellExecuteEx在命令提示符窗口中使用命令行吗?
start "Launching installer file" "C:\Path to file\Installer.xlsm"

是的,正如下面注释的批处理代码所示。
@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem First query default string value of HKEY_CLASSES_ROOT\.xlsm from registry.
call :GetDefaultRegValue "HKCR\.xlsm"

rem Is there no key HKEY_CLASSES_ROOT\.xlsm or was the default string empty?
if not defined RegValue goto GetFromAppPaths

SET RegValue
rem Get the shell command line used for opening a *.xlsm file.
call :GetDefaultRegValue "HKCR\%RegValue%\shell\open\command"

rem Could the command line not read successfully from Windows registry?
if not defined RegValue goto GetFromAppPaths

SET RegValue
rem The command line contains as first string usually enclosed in double
rem quotes EXCEL.EXE with full path enclosed in double quotes. And there
rem can be even more arguments on the command line which are not needed
rem here. The command line below is used to get just first string of
rem the command line which should be EXCEL.EXE with full path.
for %%I in (%RegValue%) do set "RegValue=%%~I" & goto CheckExcelExistence

rem It is not good when both registry queries above fail. This means
rem either Microsoft Excel is not installed at all or a version of
rem Excel is installed which does not support *.xlsm files like Excel
rem of MS Office 2003, MS Office 2000 or MS Office 97.

rem However, perhaps just *.xlsm is not correct registered and therefore
rem get full path to excel.exe from application registration key.

:GetFromAppPaths
call :GetDefaultRegValue "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\excel.exe"
if defined RegValue goto CheckExcelExistence

echo Failed to determine installation location of Microsoft Excel.
echo/
endlocal
pause
goto :EOF

:CheckExcelExistence
SET RegValue
rem Remove surrounding double quotes if the Excel executable file name
rem read from Windows registry is still enclosed in double quotes.
set "RegValue=%RegValue:"=%"
if exist "%RegValue%" goto :RunInstall

echo Registered "%RegValue%" does not exist.
echo/
endlocal
pause
goto :EOF

:RunInstall
SET RegValue
ECHO start "Launching installer file" /wait "%RegValue%" "%~dp0Installer.xlsm" /safemode
endlocal
goto :EOF


rem This subroutine queries from Windows registry the default string value of
rem the key passed to the subroutine as first and only parameter and assigns
rem this value to environment variable RegValue. Environment variable RegValue
rem is deleted and therefore is not defined after subroutine exits on failure
rem to get the registry value or when the default value is an empty string.
rem This subroutine works for Windows XP and all later versions of Windows.

:GetDefaultRegValue
set "TypeToken=2"

:Reg3Run
for /F "skip=1 tokens=%TypeToken%*" %%A in ('%SystemRoot%\System32\reg.exe QUERY "%~1" /ve 2^>nul') do (
if "%%A" == "REG_SZ" (
if not "%%~B" == "" (
set "RegValue=%%B"
goto :EOF
)
) else if "%%A" == "NAME>" (
set "TypeToken=3"
goto Reg3Run
)
)
set "RegValue="
goto :EOF

这个批处理代码只是一个演示。真正找到它时,它不会启动excel。相反,它只输出命令行,该命令行将启动excel,因为 start的echo left…在label RunInstall下面的块中。
此外,此批处理代码包含4行, SET RegValue。这4行只输出从windows注册表成功查询并存储在环境变量 RegValue中的字符串值。这4个命令有助于理解执行批处理文件时发生的情况。最后从批处理文件中删除这四条命令行,并删除大写的单个echo。
注意:如果预期的注册表项不存在或其默认值为空字符串,则很容易测试会发生什么情况。只需在以 #开头的行的最后一个双引号前插入一个像 call :GetDefaultRegValue这样的单个字符,就不会再找到修改后的注册表项。
为了理解使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读为每个命令显示的所有帮助页。
call /?
echo /?
endlocal /?
for /?
goto /?
if /?
pause /?
reg /?
reg query /?
rem /?
setlocal /?
start /?
同时阅读微软关于 Using Command Redirection Operators的文章,了解 2>nul的解释。重定向运算符 >必须用插入符号 ^转义,以便在windows命令解释器在执行命令之前处理此命令行时,将命令行解释为文本字符,而该命令行在单独的命令进程中执行嵌入的 reg.exe命令行。从后台开始。

关于excel - 从Windows批处理脚本以安全模式启动Excel文件,使用默认文件关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45509710/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com