【问题标题】:Content getting truncated when TypeTexting to word documentTypeTexting 到 word 文档时内容被截断
【发布时间】:2020-10-17 05:51:22
【问题描述】:

当我尝试将长文本文件键入到 word 文档时,内容会被 3200 个字符截断,总共 27 页,即使当我手动粘贴时完整内容大约为 6000 个字符和 60 页长。这是我的代码:

$Source1 = Get-Content -Path FileSystem::C:\Users\My_user\Desktop\file.txt -Raw;
$Word = New-Object -ComObject Word.Application;
$Word.Visible = $True;
$Document = $Word.Documents.Add();
$Selection = $Word.Selection;
$Selection.TypeText($source1);

【问题讨论】:

  • $Selection.Text = $source1 有类似的限制吗? (未测试)
  • 嘿@slightlysnarky,它也有效。目前没有限制。谢谢。
  • FWIW 我怀疑 .TypeText 的问题在于它可能会执行“为每个字符生成 Windows 键盘事件”之类的操作,而 Word 很可能会丢失其中一些事件,即使它不应该.

标签: powershell ms-word


【解决方案1】:

official documentation 没有提到长度限制,尽管this forum post 建议它是 64KB。

虽然这并不能解释您仅使用 6,000 字符的问题,但请考虑改用 .InsertFile method(未经测试):

$Selection.InsertFile('C:\Users\My_user\Desktop\file.txt')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-23
    • 1970-01-01
    • 2014-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-31
    • 1970-01-01
    相关资源
    最近更新 更多