ichi

Hi,

I've upgraded to Office 2007 from Office 2003 and am having some trouble getting some software I made that uses the Word Interop to do search and replace working. I changed the reference to the new Interop assembly I got from the
Primary Interop Assemblies.

I tried converting my template documents to Office 2007 format, but that didn't seem to help. Everything else seems to be OK but I get an error at sel.Find.Text...

Dim sel As Selection = app.Selection
For Each objRep As ReplaceWord In DirectCast(arrReplaceWords.ToArray(GetType(ReplaceWord)), ReplaceWord())
sel.Find.ClearFormatting()
sel.Find.Text = objRep.OldValue
sel.Find.Replacement.ClearFormatting()

The error I get is...
System.Runtime.InteropServices.SEHException: External component has thrown an exception.
at Microsoft.Office.Interop.Word.Find.set_Text(String prop)
at ContractCreator.ContractGen.btnPrint_Click(Object sender, EventArgs e)


Any one have any ideas on how to fix it
Thanks in advance...


Re: Visual Studio Tools for Office Word 2007 Interop problem

ichi

I've just installed Word 2003 and it works again. Wouldn't mind knowing how to fix it, but I think I might just redo it, so instead of using the interop, it just unzips the new format docx file and then does search and replace in the XML files and zips them up again.




Re: Visual Studio Tools for Office Word 2007 Interop problem

Cindy Meister

Hi ichi

I thought I answered this a couple of days ago...

The problem appears to be the situation outlined in this KB article

http://support.microsoft.com/default.aspx scid=kb;en-us;313104

Using Late binding works. For example:

Dim rng As Object 'Word.Range = wdDoc.Content
rng = CType(wdDoc.Content, Word.Range)
Try
With rng.Find
.ClearFormatting()
.Text = "abc"
.Replacement.ClearFormatting()
.Replacement.Text = "123"
.Execute(Replace:=Word.WdReplace.wdReplaceAll)
End With
Catch
Dim ex As Exception
MessageBox.Show(ex.Message)
End Try

If you need to use Selection instead of Range, then declare sel as Object, not as Selection.






Re: Visual Studio Tools for Office Word 2007 Interop problem

ichi

Many thanks for the help. I'll try it tonight...