Problem with Word.Find.Replacement using C#

A

Abongs

hi,

i want to automate a Word document using C#.
I don't understand what's not OK with this code segment:

find = selection.Find;
find.ClearFormatting();
replacement = find.Replacement; <--- Error: System.StackOverflowException
replacement.ClearFormatting();

The exact error is:

"An unhandled exception of type 'System.StackOverflowException' occurred in
mscorlib.dll"

Thanks for your help

Abongs
 
C

Cindy M.

Hi Abongs,
i want to automate a Word document using C#.
I don't understand what's not OK with this code segment:

find = selection.Find;
find.ClearFormatting();
replacement = find.Replacement; <--- Error: System.StackOverflowException
replacement.ClearFormatting();

The exact error is:

"An unhandled exception of type 'System.StackOverflowException' occurred in
mscorlib.dll"
I can't duplicate the problem using Office 2003 as the target version. From
your code in the VSTO forum I take it you're not automating 2003. Which
version of Word are you trying to work with?

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 :)
 
A

Abongs

Hi Cindy,

i work with Office 2003

Cindy M. said:
Hi Abongs,

I can't duplicate the problem using Office 2003 as the target version. From
your code in the VSTO forum I take it you're not automating 2003. Which
version of Word are you trying to work with?

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


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

Cindy M.

Hi =?Utf-8?B?QWJvbmdz?=,
i work with Office 2003
OK... Then back up a few steps: comment out all your "Find" code and build your
project with just the parts that start Word and open the document. Does that work,
or do you get errors?

If you get errors, start by fixing those. If it builds properly, please copy/paste
the code - WITH all the field declarations and instantiations - into your next
reply so that I can see what's happening. As I wrote in my answer to you in the
VSTO forum, your code doesn't compile correctly here: wrong number of parameters
in the Documents.Open method, for starters.

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 :)
 
A

Abongs

Thanks Cindy for your answer,

The project is very long. But hier you have a test project. Also this test
project don't work. Without "Find & Replacement" i don't receive any error.

using System;
using System.Reflection;
using Word = Microsoft.Office.Interop.Word;

namespace Replacement
{
class Program
{
static void Main()
{
object missing = System.Reflection.Missing.Value;
object oFalse = false;
object oTrue = true;
object replaceAll = Word.WdReplace.wdReplaceAll;
object oWrap = Word.WdFindWrap.wdFindContinue;
object filename = @"D:\Sources 2.0\Replacement.doc";

Word.Application wApp = new Word.Application();
Word.Document word = wApp.Documents.Open(ref filename, ref
missing, ref oFalse, 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);

wApp.Visible = true;
Word.Selection selection = wApp.Selection;

try
{
selection.Find.ClearFormatting();
object findText = "VPN-Client";

wApp.Selection.Find.Replacement.ClearFormatting(); // Error:
System.StackOverflowException
wApp.Selection.Find.Replacement.Text = "Client";
selection.Find.Execute(ref findText, ref oFalse, ref oFalse,
ref oFalse, ref oFalse,
ref oFalse, ref oTrue, ref oWrap, ref oFalse, ref missing,
ref replaceAll, ref oFalse, ref oFalse, ref oFalse, ref
oFalse);

}
catch (Exception ex)
{
System.Console.Write(ex.Message);
}
finally
{
word.Close(ref missing, ref missing, ref missing);
wApp.Quit(ref missing, ref missing, ref missing);
}
}
}
}
 
C

Cindy M.

Hi =?Utf-8?B?QWJvbmdz?=,
The project is very long. But hier you have a test project. Also this test
project don't work. Without "Find & Replacement" i don't receive any error.
The code you posted runs here, in a Windows Forms project, with no errors.

Hmmm. Are you referencing the Word 2003 PIAs? In the list of References for
your project, click on the one for Word then check the information. Is the
description Microsoft Word 11.0 Object Library

And you only get the error when referencing Find.Replacement? Not for
anything else having to do with Find?

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 :)
 
A

Abongs

Hi Cindy,
The code you posted runs here, in a Windows Forms project, with no errors.

.... The code also run by other developers but not on my PC.
Hmmm. Are you referencing the Word 2003 PIAs? In the list of References for
your project, click on the one for Word then check the information. Is the
description Microsoft Word 11.0 Object Library

.... Yes, the description is "Microsoft Word 11.0 Object Library".

and i don't have any other error because can't continue debuging.

My question is, do i absolutly need to do "Find.ClearFormatting()" and
"Find.Replacement.ClearFormatting()" before "Find.Execute(...)" ?
When i comment this part (Find.Replacement.ClearFormatting()) i become this
error code: "Exception from HRESULT: 0x800706F7" in Find.Execute().

Thanks again
 
C

Cindy M.

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

BINGO! That's the error I would generally have expected. See

http://support.microsoft.com/kb/313104/en-us
.... The code also run by other developers but not on my PC.


.... Yes, the description is "Microsoft Word 11.0 Object Library".

and i don't have any other error because can't continue debuging.

My question is, do i absolutly need to do "Find.ClearFormatting()" and
"Find.Replacement.ClearFormatting()" before "Find.Execute(...)" ?
When i comment this part (Find.Replacement.ClearFormatting()) i become this
error code: "Exception from HRESULT: 0x800706F7" in Find.Execute().

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 :)
 
A

Abongs

Thank you so much Cindy!!!!

that was my problem! My project work now very well!

Thanks again
Abongs
 

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