1

I need your help with inserting text in a particular area in a word document.

I have a following code that inserts text from a .txt file to the Word document, but I need it to be placed in an exact area not just anywhere.

My Code:

        string[] readText = File.ReadAllLines(@"p:\CARTAP1.txt");

        foreach (string s in readText)
        {
            Console.WriteLine(s);
        }


        Application application = new Application();
        Document document = application.Documents.Open(@"P:\PreciboCancelado.doc");
        application.Visible = true;
        application.Selection.TypeText(readText[2]);
meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
Hans
  • 387
  • 1
  • 7
  • 20
  • 3
    Possible duplicate of [How to insert characters to a file using C#](http://stackoverflow.com/questions/98484/how-to-insert-characters-to-a-file-using-c-sharp) – meJustAndrew Jul 07 '16 at 18:21
  • 1
    You will have to read the hole document and write it again, with the new text added. Somebody asked this [here](http://stackoverflow.com/questions/98484/how-to-insert-characters-to-a-file-using-c-sharp) and got a similar answer. – meJustAndrew Jul 07 '16 at 18:26
  • 1
    I'm not super familiar with word documents, but If It were me, I'd take advantage of Intellisence, and/or [the debugger](http://www.codeproject.com/Articles/79508/Mastering-Debugging-in-Visual-Studio-A-Beginn) and look at what properties and methods are found in a `Document` You're probably going to have edit your `Document` object, and save it, overwriting your existing file. – Sam I am says Reinstate Monica Jul 07 '16 at 18:27
  • *... inserts _the third full line of_ text from a .txt file..." since we're explicitly referencing `readText[2]` - correct? – gravity Jul 07 '16 at 18:27
  • @meJustAndrew: Nonsense. Word has an Automation object that allows you to work with the content of the document directly. You can easily position and work with the document without having to read and write it all. See http://stackoverflow.com/q/32693480/62576, for instance, which finds the end of the document and appends text to it. You just need to move the Selection to the location first, and then use `TypeText` to insert the new content. – Ken White Jul 07 '16 at 18:34
  • meJustAndrew has it right. The hardest part is probably understanding precisely where you need to set your "cursor". And yes, I have done a bunch of this sort of thing, but primarily using OpenOffice (AOO) and LibreOffice (LO). – Andrew Jul 07 '16 at 18:40

2 Answers2

4

I found a way to do it using bookmarks just as Manuel stated:

    string[] readText = File.ReadAllLines(@"p:\CARTAP1.txt");

    // declare objects and variables
    object fileName = @"P:\PreciboCancelado.doc";
    object readOnly = false;
    object isVisible = true;
    object missing = System.Reflection.Missing.Value;

    // create instance of Word
    Microsoft.Office.Interop.Word.ApplicationClass oWordApp = new Microsoft.Office.Interop.Word.ApplicationClass();


    // Create instance of Word document
    Microsoft.Office.Interop.Word.Document oWordDoc = new Document();


    // Open word document.
    oWordDoc = oWordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref readOnly,
    ref missing, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing,
    ref missing, ref missing, ref missing, ref missing, ref missing);

    oWordDoc.Activate();

    // Debug to see that I can write to the document.
    oWordApp.Selection.TypeText("This is the text that was written from the C# program! ");
    oWordApp.Selection.TypeParagraph();


    // Example of writing to bookmarks each bookmark will have exists around it to avoid null.
    if (oWordDoc.Bookmarks.Exists("Fecha"))
        {
            // set value for bookmarks           
            object oBookMark = "Fecha";
            oWordDoc.Bookmarks.get_Item(ref oBookMark).Range.Text = readText[2] ;

            oBookMark = "Nombre";
            oWordDoc.Bookmarks.get_Item(ref oBookMark).Range.Text = readText[3];

    }

    // Save the document.
    oWordApp.Documents.Save(ref missing, ref missing);


    // Close the application.
    oWordApp.Application.Quit(ref missing, ref missing, ref missing);
Pontus Magnusson
  • 632
  • 1
  • 10
  • 25
Hans
  • 387
  • 1
  • 7
  • 20
0

You can try it with a bookmark in Word. Perhaps this link helps you http://gregmaxey.mvps.org/word_tip_pages/insert_text_at_or_in_bookmark.html

  • From the [Help Center](http://stackoverflow.com/help/how-to-answer): Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. – Adam Jul 08 '16 at 00:31