简体   繁体   English

在 excel 365 vba 中打开 cmd.exe

[英]Open cmd.exe in excel 365 vba

I want to open cmd.exe in excel 365 vba.我想在 excel 365 vba 中打开 cmd.exe。 I used to open shell in excel 2013 but it seems not to work in excel 365.我曾经在 excel 2013 中打开 shell 但在 excel 365 中似乎不起作用。

I tried this code and works for calc.exe, ... but not for cmd.exe.我尝试了这段代码并适用于 calc.exe,......但不适用于 cmd.exe。 Does it need a special parameter/permission to run from vba.从 vba 运行是否需要特殊参数/权限。

Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As LongPtr, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As LongPtr

Sub ShellExec()
Dim strFile As String
Dim strAction As String
Dim lngErr As LongPtr

' Edit this:
strFile = "notepad.exe"  ' the file you want to open/etc.
strAction = "OPEN"  ' action might be OPEN, NEW or other, depending on what you need to do

lngErr = ShellExecute(0, strAction, strFile, "", "", 0)

' optionally, add code to test lngErr

End Sub

Kind regards, w.亲切的问候, w。

use SW_SHOW = 5 as last parameter使用 SW_SHOW = 5 作为最后一个参数

Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As LongPtr, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As LongPtr

Sub ShellExec()
Dim strFile As String
Dim strAction As String
Dim lngErr As LongPtr

' Edit this:
strFile = "cmd.exe"  ' the file you want to open/etc.
strAction = "OPEN"  ' action might be OPEN, NEW or other, depending on what you need to do

lngErr = ShellExecute(0, strAction, strFile, "", "", 5)

' optionally, add code to test lngErr

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM