使用 VSTO 在 Word 中动态创建的表格后插入文本

发布于 2024-07-27 04:09:25 字数 569 浏览 1 评论 0原文

如何使用 VSTO 和 Word 2003 在创建的表格后插入文本? 我有这样的代码,

bookmarkDescriptions = (object)"bookmarkDescriptions";
Word.Range rangeDescriptions = aDoc.Bookmarks.get_Item(ref bookmarkDescriptions).Range;

foreach (var item in items)
{
    //Add a paragraph with some text
    Table descTable = aDoc.Tables.Add(oSelection.Range, 1, 2, ref missing, ref missing);
    //Insert some text into the cells
    //Add a another paragraph with some text
}

当我添加另一段文本时,它会添加到表格中,但我希望它位于表格之后。 由于我需要循环所有项目并为每个项目创建一些文本 - 段落 - 更多文本,因此我不知道如何利用书签来获取表格之后和之外的范围。

How using VSTO and Word 2003 can I insert text after a table I've created? I have code like

bookmarkDescriptions = (object)"bookmarkDescriptions";
Word.Range rangeDescriptions = aDoc.Bookmarks.get_Item(ref bookmarkDescriptions).Range;

foreach (var item in items)
{
    //Add a paragraph with some text
    Table descTable = aDoc.Tables.Add(oSelection.Range, 1, 2, ref missing, ref missing);
    //Insert some text into the cells
    //Add a another paragraph with some text
}

when I add another paragraph of text it's added within the table but I want it after the table. Since I need to loop over all items and create some text - paragraph - some more text for each of them I don't see how I could make use of a bookmark to get a range after and outside the table.

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

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

发布评论

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

评论(1

自此以后,行同陌路 2024-08-03 04:09:25

我刚刚解决了这个问题。 我正在使用以下代码。

object oLineUnit = (object) Word.WdUnits.wdLine;
object oCountOne = (object) 1;
object oCellUnit = (object) Word.WdUnits.wdCell;

oSelection.MoveRight(ref oCellUnit, ref missing, ref missing);
oSelection.MoveDown(ref oLineUnit, ref oCountTwo, ref missing);

理解 Word 对象模型的最佳方法似乎是在 Word 中记录宏,然后查看源代码,了解正在执行哪些 API 调用,然后在您选择的环境中复制该调用。

I just solved the problem. I am using the following code.

object oLineUnit = (object) Word.WdUnits.wdLine;
object oCountOne = (object) 1;
object oCellUnit = (object) Word.WdUnits.wdCell;

oSelection.MoveRight(ref oCellUnit, ref missing, ref missing);
oSelection.MoveDown(ref oLineUnit, ref oCountTwo, ref missing);

The best way to make sense of the Word object model seems to be to record a macro in Word and to then look at the source code so see what API calls are being made and to then replicate that in your enviroment of choice, hth.

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