使用脚本组合文档

发布于 2025-02-03 07:41:58 字数 127 浏览 1 评论 0原文

我想知道我现在是否可以手动执行一项任务。我有文件夹,每个文件夹都有两个Word文档。我将这两个文档与评论色带中的组合收割机结合在一起,并保存文档供其他人查看更改。

我可以自动创建这些文件,例如使用PowerShell或VBA?

I was wondering if I could automate a task I'm doing manually now. I have folders, each with two word documents. I combine both documents with Combine from the Review ribbon and save the document for someone else to review the changes.

Can I automate creating those files, for example with powershell or VBA?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋叶绚丽 2025-02-10 07:41:58

要使用PowerShell组合两个Word文档,您可以使用以下内容:

$document1      = 'D:\Test\firstDoc.docx'
$document2      = 'D:\Test\secondDoc.docx'
$resultDocument = 'D:\Test\combined.docx'  # the output file

$word = New-Object -ComObject Word.Application
$word.Visible = $false

$doc   = $word.Documents.Open($document1)
$range = $doc.Range()
# move to the end of the first document
$range.Collapse(0)     # wdCollapseEnd see https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.word.wdcollapsedirection
# insert a page break so the second document starts on a new page
$range.InsertBreak(7)  # wdPageBreak   see https://learn.microsoft.com/en-us/office/vba/api/word.wdbreaktype
$range.InsertFile($document2)

$doc.SaveAs($resultDocument)
# quit Word and cleanup the used COM objects
$word.Quit()

$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($range)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($doc)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($word)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()

ps使用COM OBOCTS 时,您需要使用完整的绝对文件路径

To combine two word documents using PowerShell, you can use this:

$document1      = 'D:\Test\firstDoc.docx'
$document2      = 'D:\Test\secondDoc.docx'
$resultDocument = 'D:\Test\combined.docx'  # the output file

$word = New-Object -ComObject Word.Application
$word.Visible = $false

$doc   = $word.Documents.Open($document1)
$range = $doc.Range()
# move to the end of the first document
$range.Collapse(0)     # wdCollapseEnd see https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.word.wdcollapsedirection
# insert a page break so the second document starts on a new page
$range.InsertBreak(7)  # wdPageBreak   see https://learn.microsoft.com/en-us/office/vba/api/word.wdbreaktype
$range.InsertFile($document2)

$doc.SaveAs($resultDocument)
# quit Word and cleanup the used COM objects
$word.Quit()

$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($range)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($doc)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($word)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()

P.S. You need to use full, absolute file paths when using COM objects

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文