VBS代码中的With语句,在PowerShell中是如何表达的 [英] The With statement in the VBS code, how expressed in PowerShell
问题描述
有没有什么软件或者工具可以快速将VBS代码转换成PowerShell代码?
Is there a software or tool that can quickly convert VBS code into PowerShell code?
另外,我想知道,VBS代码中下面的With
语句,在PowerShell中如何表达?
In addition, I want to know, the following With
statement in the VBS code, how to express in PowerShell?
With
语句很棒,可以缩短代码,我可以在 PowerShell 中实现类似的功能吗?
The With
statement is great, it can shorten the code, can I implement similar functions in PowerShell?
发现PowerShell代码很简洁,很好奇上面的代码,如何用最短的PowerShell代码实现同样的功能.
I found the PowerShell code very succinct, I am very curious to know that the above code, how to use the shortest PowerShell code to achieve the same functionality.
'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 没有与 VBScript 的 With
语句等效的语句.而不是
PowerShell doesn't have an equivalent for VBScript's With
statement. Instead of
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()
...
}
此外,据我所知,没有 VBScript 或 VBA 到 PowerShell 编译器.不过,我整理了一些关于如何将 VBA 代码转换为 PowerShell 的笔记.
Also, to my knowledge there is no VBScript or VBA to PowerShell compiler. I put together some notes on how to translate VBA code to PowerShell, though.
这篇关于VBS代码中的With语句,在PowerShell中是如何表达的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!