使用Range.PasteAndFormat c#时粘贴项目的顺序

问题描述 投票:0回答:1

我正在尝试使用以下代码使用Word Automation在循环中粘贴文本。

Clipboard.SetData(System.Windows.DataFormats.Rtf, text);
if (!Clipboard.ContainsText())
    Thread.Sleep(500);

initialRange.PasteAndFormat(WdRecoveryType.wdFormatOriginalFormatting);
initialRange.Collapse(WdCollapseDirection.wdCollapseEnd);

我有一个试图写的字符串集合:

这是第1行这是第二行这是第3行

并且我希望输出以相同的顺序打印,但是当使用上面的代码时,我以相反的顺序得到输出,即

这是第3行这是第二行这是第1行

知道要固定输出顺序需要做什么吗?

c# ms-word office-interop
1个回答
0
投票

最后,我找出了解决办法。我必须添加

“ initialRange.MoveEnd();”在PasteAndFormat调用之前的行。

因此,最终代码看起来像

Clipboard.SetData(System.Windows.DataFormats.Rtf, text);
if (!Clipboard.ContainsData(DataFormats.Rtf))
    Thread.Sleep(500);

initialRange.MoveEnd();
initialRange.PasteAndFormat(WdRecoveryType.wdFormatOriginalFormatting);
initialRange.Collapse(WdCollapseDirection.wdCollapseEnd);
© www.soinside.com 2019 - 2024. All rights reserved.