我无法在文本右侧添加书签 [英] i could not add a bookmark to the right of a text

查看:112
本文介绍了我无法在文本右侧添加书签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试插入
第1行末尾的书签1

第2行末尾的书签2
我希望我的程序像这样

您好0
[] Hellow1
Hellow2 []


我不能那样做
我使用的文档有4个空行

i am trying to insert
a bookmark1 at the end of line1
and
a bookmark2 at the end of line2
i want my program to do like this

Hello0
[]Hellow1
Hellow2[]


i could not do that
i use a document that has 4 empty lines

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Word = Microsoft.Office.Interop.Word;//2007

namespace EstemaraA4Shmrani
{
    public partial class Form1 : Form
    {
        Word.Application wrdApp;
        Word._Document wrdDoc;
        Word.Range rng;
        //
        Object oMissing = System.Reflection.Missing.Value;
        Word.Bookmark bookmark1;
        Word.Bookmark bookmark2;
        Object name;
        //
        Object unit, count, extend;
        Object start, end;
        Word.Selection wrdSelection;
        //-------------------------
        public Form1()
        {
            InitializeComponent();
        }
        //---------------------Word-----------------
        private void CallWord_Click(object sender, EventArgs e)
        {   
            wrdApp = new Word.Application();
            wrdApp.Visible = true;
            Object range = rng;

            name = "D:\\EstemaraA4Sabtan.docx";
            wrdDoc = wrdApp.Documents.Open(ref name, 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,
                    ref oMissing, ref oMissing);
            //----------------------
            InsertTextAtSelection("Hellow 0");
            MoveDownNLine(1); InsertTextAtSelection("Hellow 1");
            MoveDownNLine(2); InsertTextAtSelection("Hellow 2");          
            // Display Bookmarks.
            wrdDoc.ActiveWindow.View.ShowBookmarks = true;
            
            SelectSentencesId(1);
            rng.Select();
            range = rng;
            bookmark1 = wrdDoc.Bookmarks.Add("bookmark1", ref range);
            rng.InsertBefore(" Before ");

            SelectSentencesId(2);
            rng.Select();
            range = rng;
            bookmark2 = wrdDoc.Bookmarks.Add("bookmark2", ref range);
            rng.InsertAfter(" After ");
            
            //Release References.
            wrdSelection = null;
            wrdDoc = null;
            wrdApp = null;
        }//
        //------------------------
        private void MoveDownNLine(Object id)
        {
            unit = Word.WdUnits.wdLine;
            extend = Word.WdMovementType.wdMove;
            wrdApp.Selection.MoveDown(ref unit, ref id, ref extend);
        }
        //--------------------------------------------------
        private void InsertTextAtSelection(string Mystring)
        {
            Word.Selection sln = wrdApp.Selection;
            // Make sure overtype is turned off.
            wrdApp.Options.Overtype = false;
            //if (sln.Type == Word.WdSelectionType.wdSelectionNormal)
            //{
            sln.TypeText(Mystring);
            //sln.TypeParagraph();
            //}
        }
        private void SelectSentencesId(int id)
        {
            if (wrdDoc.Sentences.Count >= id)
            { //Supply a Start and end value for the Range.
                start = wrdDoc.Sentences[id+1].Start;
                end = wrdDoc.Sentences[id+1].End;
                rng = wrdDoc.Range(ref start, ref end);
                rng.Select();
            }
        }
    }
}

推荐答案

查看我的链接:
MSDN库添加书签控件 [类似的讨论 [ ^ ]
添加书签的程序指南 [ ^ ]
See my links:
MSDN library Add bookmark control[^]
A similar discussion [^]
A program guide for adding Bookmark[^]


您的程序运行正常..当您在selectsentences函数中对行进行计数时,在插入空白文档时需要换行.像这样插入即可..

Your program is working fine.. as you are counting lines in the selectsentences function you need newlines when you are inserting in to a blank document.. so insert like this it is working..

InsertTextAtSelection("Hellow 0\n");
MoveDownNLine(1);
InsertTextAtSelection("Hellow 1\n");
MoveDownNLine(2);
InsertTextAtSelection("Hellow 2\n");


我已经按照自己的方式做到了
在我看来,对字符进行计数非常重要
i have done it in my way
counting the characters seems to me very important
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Word = Microsoft.Office.Interop.Word;
//http://msdn.microsoft.com/en-us/library/2a9dt54a.aspx

namespace EstemaraA4Shmrani
{
    public partial class Form1 : Form
    {
        Word.Application wrdApp;
        Word._Document wrdDoc;
        Word.Range rng;
        //
        Object oMissing = System.Reflection.Missing.Value;
        Word.Bookmark bookmarkA;
        Object name;
        //
        Object unit, count, extend;
        Object start, end;
        Word.Selection wrdSelection;
        //-------------------------
        public Form1()
        {
            InitializeComponent();
        }
        //---------------------Word-----------------
        private void CallWord_Click(object sender, EventArgs e)
        {   
            wrdApp = new Word.Application();
            wrdApp.Visible = true;
            Object range = rng;

            name = "D:\\EstemaraA4Sabtan.docx";
            wrdDoc = wrdApp.Documents.Open(ref name, 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,
                    ref oMissing, ref oMissing);
            //----------------------
            InsertTextAtSelection("Hellow 0\n");
            InsertTextAtSelection("Hellow 1\n");
            InsertTextAtSelection("Hellow 2\n");          
            // Display Bookmarks.
            wrdDoc.ActiveWindow.View.ShowBookmarks = true;
            MoveUpNLine(2); InsertTextAtSelection("Hellow A bookmarkA\n");
            MoveNcharacterLeft(9);
            //SelectN_Words(1);
            //Supply a Start and end value for the Range.
            start = 9 * 2 ;
            end =   9 * 3 ;
            rng = wrdDoc.Range(ref start, ref end);
            rng.Select();
            //----------
            range = rng;
            bookmarkA = wrdDoc.Bookmarks.Add("This_is_the_name_of_bookmark_in_ms_word", ref range);
            rng.InsertAfter(" after ");
            //
            //Release References.
            wrdSelection = null;
            wrdDoc = null;
            wrdApp = null;
        }//
        //------------------------
        private void MoveUpNLine(Object id)
        {
            unit = Word.WdUnits.wdLine;
            extend = Word.WdMovementType.wdMove;
            wrdApp.Selection.MoveUp(ref unit, ref id, ref extend);
        }
        //------------------------
        private void MoveDownNLine(Object id)
        {
            unit = Word.WdUnits.wdLine;
            extend = Word.WdMovementType.wdMove;
            wrdApp.Selection.MoveDown(ref unit, ref id, ref extend);
        }
        //--------------------------------------------------
        private void MoveNcharacterLeft(Object n)
        {// Move the insertion point left n characters.
            unit = Word.WdUnits.wdCharacter;
            count = n;
            extend = Word.WdMovementType.wdMove;
            wrdApp.Selection.MoveLeft(ref unit, ref count,
                ref extend);
        }
        //-------------
        private void MoveNcharacterRight(Object n)
        {// Move the insertion point left n characters.
            unit = Word.WdUnits.wdCharacter;
            count = n;
            extend = Word.WdMovementType.wdMove;
            wrdApp.Selection.MoveRight(ref unit, ref count,
                ref extend);
        }
        //-------------
        private void InsertTextAtSelection(string Mystring)
        {
            Word.Selection sln = wrdApp.Selection;
            // Make sure overtype is turned off.
            wrdApp.Options.Overtype = false;
            //if (sln.Type == Word.WdSelectionType.wdSelectionNormal)
            //{
            sln.TypeText(Mystring);
            //sln.TypeParagraph();
            //}
        }
        private void SelectSentencesId(int id)
        {
            if (wrdDoc.Sentences.Count >= id)
            { //Supply a Start and end value for the Range.
                start = wrdDoc.Sentences[id+1].Start;
                end = wrdDoc.Sentences[id+1].End;
                rng = wrdDoc.Range(ref start, ref end);
                rng.Select();
            }
        }
        private void SelectN_Words(Object n)
        {// Select the 3 words to the right of the insertion point.
        unit = Word.WdUnits.wdWord;
        count = n;
        extend = Word.WdMovementType.wdExtend;
        wrdApp.Selection.MoveRight(ref unit,ref count,ref extend);
        }
    }
}


这篇关于我无法在文本右侧添加书签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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