powershell - VBS代码中的With语句,在PowerShell中如何表达
问题描述
有没有可以快速将VBS代码转换为PowerShell代码的软件或工具?
另外,我想知道,With
VBS代码中的以下语句,在PowerShell中怎么表达?
语句很棒,With
可以缩短代码,我可以在PowerShell中实现类似的功能吗?
我发现 PowerShell 代码非常简洁,我很想知道上面的代码,如何使用最短的 PowerShell 代码来实现相同的功能。
'declare and instaciate wrdApp
Dim wrdApp: Set wrdApp = WScript.CreateObject("Word.Application")
'declare wrdDoc
Dim wrdDoc
Dim wdReplaceAll
'Open the document
Set wrdDoc = wrdApp.Documents.Open("c:\test.docx")
'set the value for the replace "constant"
wdReplaceAll = 2
wrdDoc.Select
With wrdApp.Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "WordToReplace"
.Replacement.Text = "ReplaceWith"
.Forward = True
.Wrap = 1
.Format = False
.MatchCase = False
.MatchWholeWord = False
'the Replace argument is the 11'th argument
.Execute , , , , , , , , , , wdReplaceAll
End With
'save the document and close Word
wrdDoc.SaveAs2 "c:\test-ok.docx"
wrdApp.Quit
'clean up
Set wrdApp = Nothing
Set wrdDoc = Nothing
解决方案
PowerShell 没有 VBScriptWith
语句的等效项。代替
With wrdApp.Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
...
End With
你可能会做这样的事情:
$find = $wrdApp.Selection.Find
$find.ClearFormatting()
$find.Replacement.ClearFormatting()
...
或像这样:
$wrdApp.Selection.Find | ForEach-Object {
$_.ClearFormatting()
$_.Replacement.ClearFormatting()
...
}
此外,据我所知,PowerShell 编译器没有 VBScript 或 VBA。不过,我整理了一些关于如何将 VBA 代码转换为 PowerShell的注释。
推荐阅读
- macos - 尽管没有容器运行,但 Docker 正在最大限度地利用 CPU,如何减少 docker 的 CPU 开销?
- python-3.x - Error message when appending data to pandas dataframe
- php - PhpStorm 代码样式“空格 - 内 - 括号”不起作用
- spring - Spring Data R2DBC - 在反应式存储库中构建自定义 postgresql 查询
- python-3.x - 单选选项不可点击的元素
- php - 如何在 show 方法中丢弃 GET 值(laravel)
- android - 我可以在IOS中使用为android创建的sqlite数据库吗
- hive - 哪个部门的项目总数大于总项目的40%
- javascript - 单击锚点时如何阻止html页面跳转,但仍然转到href中的url?
- testng - 如何在 TestNG 中从同一类运行多个测试用例