如何在插入符号位置的Word应用程序中插入文本. [英] How to Insert text in word application at caret position.

查看:85
本文介绍了如何在插入符号位置的Word应用程序中插入文本.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我想使用C#在Word文档中的插入位置插入一些文本.
但是我的程序正在开始位置插入.是否可以在打开的Word文档中的所需位置插入文本?
任何人都可以帮忙....
这是我的代码段.

Hello,

I want to insert some text in word document at caret position using c#.
But my program is inserting at starting position. Will it possible to insert text at desired location in opened word document?
Can anyone help on this....
Here is my code snippet.

Object oTrue = true;
Object oFalse = false;
Object readOnly = false;
Object OpenAndRepair = true;
Object oMissing = System.Reflection.Missing.Value;
Object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */
Word.Application wordApp = new Word.Application();
Word.Document wordDoc = new Word.Document();
wordApp.Visible = true; ;

object fileName = @"D:\Sample.doc";
//Opening Word Document

wordApp.Documents.Open(ref fileName,
                        ref oMissing, ref readOnly, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

Word.Selection currentSelection = wordApp.Selection;

if (currentSelection.Type == Word.WdSelectionType.wdSelectionIP)
{
    currentSelection.TypeText("Inserting at insertion point. ");
    currentSelection.TypeParagraph();
}

//Insert a paragraph at the end of the document.
Word.Paragraph oPara2;
object oRng = wordDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
oPara2 = wordDoc.Content.Paragraphs.Add(ref oRng);
oPara2.Range.Text = "Heading 2";
oPara2.Format.SpaceAfter = 6;
oPara2.Range.InsertParagraphAfter();
currentSelection.t("Inserting at insertion point$. ");
currentSelection.TypeParagraph();



问候,
Mahesh



Regards,
Mahesh

推荐答案

. "); currentSelection.TypeParagraph();
. "); currentSelection.TypeParagraph();



问候,
Mahesh



Regards,
Mahesh


您的代码完全可以正常工作.为了让您在给定位置插入文本,您实际上必须告诉Word该位置在哪里.

您的代码无法执行此操作...这就是您正在执行的操作.

您打开Word文档. (默认情况下,插入记号将位于第一个位置,或您所说的开始"位置.).

然后,获取当前选择. (但同样,您没有告诉Word更改插入符号的位置,因此,当前选择的长度为0,文本中的位置为0).

然后,在当前选择的位置插入文本(我们刚刚说过是在起点).

当然可以在文本中的给定位置插入文本.您只需要告诉它从哪里开始.

看一下: Word对象模型概述 [ ^ ]

您可以通过Selection.SetRange方法
Your code is working exactly as it should. In order for you to insert text at a given position, you have to actually tell Word where that position is.

Your code doesn''t do that...here''s what you''re doing.

You open the Word document. (By default, the caret will be at the first position...or "starting" position as you said).

Then, you grab the current selection. (But again, you''ve not told Word to change the caret position, therefore, the current selection has a length of 0 and a position in the text of 0).

Then, you insert text at that current selection (which we''ve just said was at the starting point).

Of course it''s possible to insert text at a given position in the text. You just have to tell it where to start.

Have a look at this: Word Object Model Overview[^]

You can do that through Selection.SetRange method


这篇关于如何在插入符号位置的Word应用程序中插入文本.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆