C# Word Styling Automation - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

C# Word Styling Automation

rangeshramrangeshram Posts: 2Member
Dear All,

Using C# and word object model, i am automate the process, where based on the tag name present in the document its

corresponding style has to be applied for the paragraph.
For instance:

This is Heading1
This is Paragraph sample...

Our task is to find the tag and replace it with "H1" style for the whole document.

Below is the code where I can open the document and add the template styles into the current file.


ApplicationClass objWordApp = new ApplicationClass();
Document objWordDoc = new Document();

foreach (string eachfile in lstfile.Items)
Object nothing = System.Reflection.Missing.Value;
Object fileName = txtFilename.Text.ToString();
Object notTrue = false;
Object missing = System.Reflection.Missing.Value;
objWordDoc = null;

Object readOnly = false;
Object isVisible = false;

objWordApp.Visible = false;

objWordDoc = objWordApp.Documents.Open(ref fileName, ref missing,
ref readOnly, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref isVisible, ref missing, ref missing,
ref missing, ref missing);
objWordApp.Visible = false;

string InDoc = OPagPath + "\" + eachfile;
objWordApp.Selection.InsertFile(PagFpath, ref missing, ref missing, ref missing, ref missing);

//Here I need help to find and replace with particular style

objWordApp.Quit(ref notTrue, ref missing, ref missing);

I need your help/suggestion regarding the find text and replace with corresponding style.


  • PsightoplazmPsightoplazm Posts: 332Member ✭✭
    if you are just trying to parse the

    tags specifically you might just try using a regular expression to find them all...

    var mytext = "this is my text hare

    over hizzah!";
    var pattern = "


    var matches = Regex.Matches(mytext, pattern, RegexOptions.Singleline);

    var result = "";
    foreach (var match in matches.Cast())
    result += "Heading Item: Start=" + match.Index + " Length=" + match.Length + "
    result += "Heading Text: ";
    result += match.Value.Replace("

    ", "").Replace("

    ", "") + "
    result += "_____________________________________________


  • harrypeter86harrypeter86 Posts: 2Member
    You can do this using Spire.Doc, very easy to use.


    Maybe help to you.
Sign In or Register to comment.