.NET General

Moderators: None (Apply to moderate this forum)
Number of threads: 797
Number of posts: 1359

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
C# Word Styling Automation Posted by rangeshram on 13 Apr 2009 at 6:22 AM
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:
<H1>This is Heading1
<Para>This is Paragraph sample...

Our task is to find the <H1> 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);
                objWordDoc.Activate();
                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.
Report
Re: C# Word Styling Automation Posted by Psightoplazm on 24 Apr 2009 at 2:41 PM
if you are just trying to parse the <H1> tags specifically you might just try using a regular expression to find them all...

            var mytext = "this is <H1>my text hare</H1> over hizzah!";
            var pattern = "<H1>.*?</H1>";
            var matches = Regex.Matches(mytext, pattern, RegexOptions.Singleline);

            var result = "";
            foreach (var match in matches.Cast<Match>())
            {
                result += "Heading Item: Start=" + match.Index + " Length=" + match.Length + "\r\n";
                result += "Heading Text: ";
                result += match.Value.Replace("<H1>", "").Replace("</H1>", "") + "\r\n";
                result += "_____________________________________________\r\n";
            }

            MessageBox.Show(result);


></\/~Psightoplasm`~
Report
Re: C# Word Styling Automation Posted by harrypeter86 on 20 Jul 2010 at 2:42 AM
You can do this using Spire.Doc, very easy to use.

http://www.e-iceblue.com/Introduce/word-for-net-introduce.html

Maybe help to you.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.