当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 从 PowerShell ISE 发送到 Microsoft Word 重访

作者:精品下载站 日期:2024-12-14 07:45:20 浏览:14 分类:玩电脑

从 PowerShell ISE 发送到 Microsoft Word 重访


你们中的许多人似乎喜欢我的小 PowerShell ISE 插件,用于将文本从脚本窗格发送到 Word 文档。我早该知道有人会问一种让它着色的方法。您可以手动选择脚本中的行,当您将它们粘贴到 Word 中时,它们会自动继承彩色标记。不幸的是,开发一个等效的 PowerShell 要复杂得多。

如果您四处搜索,您会发现大量用于从 ISE 生成 HTML 和彩色输出的工具和脚本。我尝试将其中一些合并到我的脚本中,但它们比我想要处理的要复杂得多。我真正需要的只是一个简单的 Ctrl+C 命令。所以我作弊了。我决定使用 VBScript 中的 SendKeys() 方法。

if ($Colorized) {
    #copy the selection to the clipboard and paste
    #This is a shortcut hack that may not always work the first time
    $wshell = New-Object -ComObject Wscript.shell
    #must be lower-case c otherwise you will end up sending
    #ctrl+shift+c
    $wshell.SendKeys("^c") 
    #timing is everything with SendKeys. This could be a lower value
    start-sleep -Milliseconds 500
    $global:selection.Paste()
}

我向名为 Colorized 的函数添加了一个新的开关参数。这意味着我还需要一个额外的菜单快捷方式。

$psise.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Send to Word Colorized",{Send-ToWord -Colorized},"") | Out-Null

您会注意到没有键盘快捷键。至少对我来说,使用键盘快捷键得到的结果不一致,而且通常什么也得不到。但如果我选择了菜单项,它似乎总是有效。

这是完整更新的功能。

#requires -version 3.0

#this is an ISE only function
Function Send-ToWord {
[cmdletbinding()]
Param(
[ValidatePattern("\S+")]
[string[]]$Text = $psise.CurrentFile.Editor.SelectedText,
[switch]$Colorized
)

If (($global:word.Application -eq $Null) -OR -NOT (Get-Process WinWord)) {
    #remove any variables that might be left over just to be safe
    Remove-Variable -Name doc,selection -Force -ErrorAction SilentlyContinue

    #create a Word instance if the object doesn't already exist
    $global:word = New-Object -ComObject word.application

    #create a new document
    $global:doc = $global:word.Documents.add()

    #create a selection
    $global:selection = $global:word.Selection

    #set font and paragraph for fixed width content
    $global:selection.Font.Name = "Consolas"
    $global:selection.font.Size = 10
    $global:selection.paragraphFormat.SpaceBefore = 0
    $global:selection.paragraphFormat.SpaceAfter = 0

    #show the Word document
    $global:word.Visible = $True    
}

if ($Colorized) {
    #copy the selection to the clipboard and paste
    #This is a shortcut hack that may not always work the first time
    $wshell = New-Object -ComObject Wscript.shell
    #must be lower-case c otherwise you will end up sending
    #ctrl+shift+c
    $wshell.SendKeys("^c") 
    #timing is everything with SendKeys. This could be a lower value
    start-sleep -Milliseconds 500
    $global:selection.Paste()
}
else {
#insert the text
$global:selection.TypeText($text) 
}

#insert a new paragraph (ENTER)  
$global:selection.TypeParagraph()

} #end Function

#add to menu
$psise.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Send to Word",{Send-ToWord},"Ctrl+Alt+W") | Out-Null                

#keyboard shortcut doesn't always work
$psise.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Send to Word Colorized",{Send-ToWord -Colorized},"") | Out-Null

我不能保证彩色复制和粘贴 100% 有效。否则,您始终可以使用传统的键盘快捷键:Ctrl+C、Alt+Tab(对于 Word)、Ctrl+V。

享受。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯