replace - C# Word Document - How to clean formatting? -



replace - C# Word Document - How to clean formatting? -

the dilemma rather simple. need create little app clear font background colors (leave table cell background colours unchanged), , remove text strikethrough in word document, , save document folder. otherwise document's formatting should remain untouched.

below large-ish illustration scraped random examples available in google showing how apply specific kinds of formatting random strings found using find.execute(). have no clue however, on how described above.

public static string searchdoc(string filenameref) { microsoft.office.interop.word._application word = new microsoft.office.interop.word.application(); ; microsoft.office.interop.word._document doc = new microsoft.office.interop.word.document(); object missing = system.type.missing; seek { system.io.fileinfo executablefileinfo = new system.io.fileinfo(system.reflection.assembly.getentryassembly().location); object filename = system.io.path.combine(executablefileinfo.directoryname, filenameref); doc = word.documents.open(ref filename, ref missing, ref missing, ref missing , ref missing, ref missing, ref missing, ref missing, ref missing, ref missing , ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); doc.activate(); //object findstr = "hello"; //sonething find // part fail, can't find of way find.execute on formatting // opposed mere strings. //while (word.selection.find.execute(ref findstr)) //found... //{ // //change font , format of matched words // word.selection.font.name = "tahoma"; //change font tahoma // word.selection.font.colorindex = microsoft.office.interop.word.wdcolorindex.wdred; //change color reddish //} object savefilename = executablefileinfo.directoryname + "\\new\\" + filenameref; doc.saveas(ref savefilename, ref missing, ref missing, ref missing, ref missing , ref missing, ref missing, ref missing, ref missing, ref missing, ref missing , ref missing, ref missing, ref missing, ref missing, ref missing); } grab (exception) { } { doc.close(ref missing, ref missing, ref missing); word.application.quit(ref missing, ref missing, ref missing); } homecoming filenameref; }

thanks help! , mean any, getting started on how spot formatting help great deal, imagine. :)

this not c#-specific question; it's word object model question (i refer here , here).

as specific question, suggest turn on macro recorder in word, perform actions, , see generated vba code. can apply in c#.

try this:

using system; using microsoft.office.interop.word; using system.io; using system.reflection; namespace wordformattingfindreplace { class programme { static void main(string[] args) { } public static string searchdoc(string filename) { _application word = new application(); ; _document doc; string foldername = path.getdirectoryname(assembly.getentryassembly().location); string filepath = path.combine(foldername,filename); doc = word.documents.open(filepath); var find=doc.range().find; find.text="hello"; find.format=true; find.replacement.font.name="tahoma"; find.replacement.font.colorindex=wdcolorindex.wdred; find.execute(replace:wdreplace.wdreplaceall); doc.saveas2(path.combine(foldername,"new",filename)); doc.close(); //we need cast _application resolve quit method beingness called ((_application)word.application).quit(); homecoming filename; } } }

some notes:

use using statements clarity. instead of microsoft.office.interop.word._application word, add together using microsoft.office.interop.word @ top of file, , can write _application word if need folder name, utilize static path.getdirectoryname method , save string variable, instead of creating fileinfo object as of .net 4, can skip optional arguments when calling documents.open, document.saveas , document.close. means don't need object missing. there's nil here user needs see, calling document.activate unnecessary it's improve reuse word.application instance, instead of recreating each call.

c# replace formatting ms-word

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -