Finding Formatting Using C# and automation

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



I have been unable to figure out how to find formatting (a style) using
Microsoft.Word.Interopt

First issue was getting an instance of a style
In the following m_WordDoc is an instance of a document in m_WordApp an
instance of word
This I discovered after much trying a way to get the "Heading 2" style as
follows:

object sStyle = "Heading 2";
object Heading2obj =
((Microsoft.Office.Interop.Word.Styles)m_WordDoc.Styles).get_Item(ref sStyle);

I made a macro in Word to find "Heading 2" style and attempted to copy it.
My code then looks like

Microsoft.Office.Interop.Word.Selection Sel1 =
m_WordApp.Selection;
Sel1.Find.ClearFormatting();
Sel1.Find.set_Style(ref Heading2obj);
Sel1.Find.ParagraphFormat.Borders.Shadow = false;
object FindText = "";
object ReplaceText = "";
object Wrap =
Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
object True=true;
object False=false;
Sel1.Find.Execute(ref FindText, //
/*in*/System.Object FindText,
ref False, //
/*in*/System.Object MatchCase,
ref False, //
/*in*/System.Object MatchWholeWord,
ref False, //
/*in*/System.Object MatchWildcards,
ref False, //
/*in*/System.Object MatchSoundsLike,
ref False, //
/*in*/System.Object MatchAllWordForms,
ref True, //
/*in*/System.Object Forward,
ref Wrap, //
/*in*/System.Object Wrap,
ref True, //
/*in*/System.Object Format,
ref ReplaceText, //
/*in*/System.Object ReplaceWith,
ref False, //
/*in*/System.Object Replace,
ref False, //
/*in*/System.Object MatchKashida,
ref False, //
/*in*/System.Object MatchDiacritics,
ref False, //
/*in*/System.Object MatchAlefHamza,
ref False) //
/*in*/System.Object MatchControl

The Find.Execute always returns false. I have tried various changes of True
and False for parameters with no luck -- Yes the document contains instances
of Heading 2 style.

The macro (recorded in Word) which works looks like
// Selection.Find.ClearFormatting
// Selection.Find.Style = ActiveDocument.Styles("Heading
2")
// Selection.Find.ParagraphFormat.Borders.Shadow = False
// With Selection.Find
// .Text = ""
// .Replacement.Text = ""
// .Forward = True
// .Wrap = wdFindContinue
// .Format = True
// .MatchCase = False
// .MatchWholeWord = False
// .MatchWildcards = False
// .MatchSoundsLike = False
// .MatchAllWordForms = False
// End With
// Selection.Find.Execute

Please Help I am going crazy.
--
Charlie
.



Relevant Pages

  • Re: Advice for managing / formatting long document
    ... >> Hi, I have a similar problem, formatting documents that have ... under the heading 'Example — creating and inserting a ... This macro ensures that a table cell that is too large to fit in the ...
    (microsoft.public.mac.office.word)
  • Re: LISTNUM & Wildcards
    ... "Jay Freedman" wrote: ... That's what the macro looks for. ... are the heading numbers just ordinary typed ... Listnum), try to translate those three characters to a number (which ...
    (microsoft.public.word.docmanagement)
  • Re: LISTNUM & Wildcards
    ... That's what the macro looks for. ... are the heading numbers just ordinary typed ... Listnum), try to translate those three characters to a number (which ... "Jay Freedman" wrote: ...
    (microsoft.public.word.docmanagement)
  • Re: LISTNUM & Wildcards
    ... I loaded the macro and ran it; ... Is the reason it didn't work is you say < 4 but my heading is 4.0? ... "Jay Freedman" wrote: ... For each Listnum \l 2 it finds, it needs to examine the value ...
    (microsoft.public.word.docmanagement)
  • Re: Styles help
    ... Heading 0 style. ... This revision to the macro will avoid that error by changing the number from ... 'Apply List Format To Selection ... I other procedures have the same pattern. ...
    (microsoft.public.word.vba.general)