a little help in word automation

D

David Krmpotic

Regards,


I need a little explanation in converting word VB macros to charp code!

first, I recorded simple macro which replaces every occurence of one word in
whole document (is it?) , here:

Sub replaceall()
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "original"
.Replacement.Text = "replacement"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

Now..

my code in csharp looks like that:

Word.ApplicationClass WordApp = new Word.ApplicationClass();
object fileName = System.Environment.CurrentDirectory + \\proc.doc;
object missing = System.Reflection.Missing.Value;
WordApp.Visible = true;
// Open the document that was chosen by the dialog
Word.Document aDoc = WordApp.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);
// Activate the document so it shows up in front
aDoc.Activate();
WordApp.ActiveWindow.Selection.Find.ClearFormatting();
WordApp.ActiveWindow.Selection.Find.Replacement.ClearFormatting();
WordApp.ActiveWindow.Selection.Find.Text = "original";
WordApp.ActiveWindow.Selection.Find.Replacement.Text = "replacement";
WordApp.ActiveWindow.Selection.Find.Forward = true;
WordApp.ActiveWindow.Selection.Find.Wrap = Word.WdFindWrap.wdFindContinue;
WordApp.ActiveWindow.Selection.Find.Format = false;
WordApp.ActiveWindow.Selection.Find.MatchCase = false;
WordApp.ActiveWindow.Selection.Find.MatchWholeWord = false;
WordApp.ActiveWindow.Selection.Find.MatchWildcards = false;
WordApp.ActiveWindow.Selection.Find.MatchSoundsLike = false;
WordApp.ActiveWindow.Selection.Find.MatchAllWordForms = false;
WordApp.ActiveWindow.Selection.Find.Execute(ref missing, ref missing, ref
missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, Word.WdReplace.wdReplaceAll, ref missing, ref
missing,ref missing,ref missing);

I get this error:

c:\documents and settings\administrator\my documents\visual studio
projects\borisexcel\form1.cs(388,5): error CS1502: The best overloaded
method match for 'Word.Find.Execute(ref object, ref object, ref object, ref
object, ref object, ref object, ref object, ref object, ref object, ref
object, ref object, ref object, ref object, ref object, ref object)' has
some invalid arguments


So what would be the best structure of code in CSharp (and it has to
compile, too :))

Searching on the net I get a lot of results but mostly in VB.NET (and I
don't know how to translate them, like you can see) and some in C# (here
always something doesn't work)..

THANK YOU!!

David
 

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