Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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();
            }
        }
    }
}
Posted
Updated 20-Jun-12 12:52pm
v2

 
Share this answer
 
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..

C#
InsertTextAtSelection("Hellow 0\n");
MoveDownNLine(1);
InsertTextAtSelection("Hellow 1\n");
MoveDownNLine(2);
InsertTextAtSelection("Hellow 2\n");
 
Share this answer
 
i have done it in my way
counting the characters seems to me very important
C#
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);
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900