VBA宏运行PowerShell命令 [英] VBA Macro to run PowerShell Command

查看:126
本文介绍了VBA宏运行PowerShell命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个宏,该宏将能够重命名文件夹中所有扩展名为".docx"的文件.

I am trying to create a macro, that would be able to rename all files with ".docx" extension in a folder.

PowerShell命令在直接使用时可以工作,但是在通过VBA代码使用时,则不会执行.

The PowerShell command works when used directly, but when used through the VBA code, it's not executing.

Sub test()
    Dim retval
    Dim pscmd

    pscmd = "PowerShell -ExecutionPolicy Bypass -Command ""Get-ChildItem -Path *.docx -Recurse | Rename-Item -NewName {$_.Name.Substring(0,7) + $_.Extension }"""

    retval = Shell(pscmd, vbNormalFocus)

    Debug.Print pscmd
End Sub

脚本正在执行,没有编译错误,但是文件未重命名.

The script is executing without compilation errors, but the files are not being renamed.

我需要更改什么?

推荐答案

您必须提供文件所在的路径尝试一下并更改适合您的路径

You have to provide the path where your files exist Try this and change the path to suit you

Sub Test()
Dim retval, pscmd

pscmd = "PowerShell -ExecutionPolicy Bypass -Command ""Get-ChildItem -Path " & ThisWorkbook.Path & "\Files\" & " *.docx -Recurse | Rename-Item -NewName {$_.Name.Substring(0,7) + $_.Extension }"""
retval = Shell(pscmd, vbNormalFocus)

Debug.Print pscmd
End Sub

这篇关于VBA宏运行PowerShell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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