Finding Formatting Using C# and automation

C

CharlieB

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.
 
C

Cindy M.

Hi =?Utf-8?B?Q2hhcmxpZUI=?=,

My gut feeling is that it may be because you're setting a few of the options
that should be better left unset. Define a variable of type object and assign to
it Type.Missing. Use this for all the argument of Find.Execute that aren't in
the macro code your recorded (such as for the ReplaceText).

In addition, set the Wrap object to STOP (not continue, that could throw you
into a continuous loop under certain circumstances).

And you should be able to delete the part about the Paragraph.Border. Looks like
the macro recorder may have hiccupped, or it could be something special about
the document you were searching when you recorded it.
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.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top