3
votes

I am writing a plugin of MS Word. Still in the very early stages of learning VSTO. So basically so far what i have in a Word document is some user written text (a complete Word document) in somewhat the following form:

Hello, my name is <%NAME%>. I am <%AGE%> years old and i live in <%COUNTRY%>.

Those <%%> are the variables which user drag and drop from a custom task pane.

What I'll be doing is replacing those variables with some actual data values read from some data file. But for now all i am looking for is a way to read the content of current Active Document in a String, then replace those variables with dummy data values for now and then replace the old document with variable values with this new one with actual values replaced in place of NAME, AGE etc. Preferably in onSaveAs event but as that's not available so i'll have to do it in BeforeSave event.

In short all i am looking for is a way to:

  • Read the contents of current document.
  • Modify those contents.
  • Write them back.

I've been searching the web and MSDN for hours now but can't really find anything useful or maybe can't implement it because i'm a newbie on this.

Some of the articles i read to achieve this are:

add-in in VSTO - How to get text from Word document using Ribbon with button

How To Read & Write Text From Word Document Using VSTO In C#

Globals.ThisAddIn.Application.Selection.Text;

This only gives the selected text but i need all the text of the document selected and non selected both.

Current code in my ThisAddin.cs is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Word;


namespace dragdrop
{
    public partial class ThisAddIn
    {

        private void ThisAddIn_Startup(object sender, System.EventArgs e){Globals.ThisAddIn.CustomTaskPanes.Add(new OrdersListUserControl(), "Drag and Drop List Items on Word Doc").Visible = true;}       
        private void ThisAddIn_Shutdown(object sender, System.EventArgs e){}
        public void OnDropOccurred(object data) //adding dropped text on current cursor position.
        {
            Word.Selection currentSelection = Application.Selection;
            currentSelection.TypeText(data.ToString());
        }
        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);

        }


        #endregion
    }
}

Any sort of help/directions is more than welcome. Thanks.

EDIT: I managed to get the contents using:

Globals.ThisAddIn.Application.ActiveDocument.Range(0, 5).Text

but the issue with this is that if the end range exceeds the total length of document it will crash! So how to get the length of the document in order to provide it as second argument, OR is there a better way to do it? Also i was wondering just reading the text and writing it back will make the text lose it's formatting and styling so is there a way to preserve that for what i want to achieve? Thanks a lot for clearing out all these confusions.

1

1 Answers

5
votes

Use Content property of the document to get first and last index of your document's range

Microsoft.Office.Interop.Word._Document oDoc = 
     Globals.ThisAddIn.Application.ActiveDocument;
Object start = oDoc.Content.Start;
Object end = oDoc.Content.End;

// select entire document 
oDoc.Range(ref start,ref end).Select();