Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When trying to automate a word document for Find And Replace in vb.net I get the following error:
exception SEHException

This is my code:-

VB
Imports Word = Microsoft.Office.Interop.Word
 Dim sFind As String
        Dim sReplace As String
        sFind = "findme"
        sReplace = "findyou"
        Dim oWord As Word.Application
        Dim oDoc As Word.Document
        Try
            oWord = CreateObject("Word.Application")
            oDoc = oWord.Documents.Open("E:\test1.docx")
            oDoc.Activate()
            Dim myStoryRange As Microsoft.Office.Interop.Word.Range
            For Each myStoryRange In oDoc.StoryRanges
                With myStoryRange.Find
                    .Text = sFind
                    .Replacement.Text = sReplace
                    .Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue
                    .Execute(Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
                End With
            Next myStoryRange

            oDoc.SaveAs("E:\test2.docx")
            oDoc.Close()
            oDoc = Nothing
            oWord.Application.Quit()
            oWord = Nothing
        Catch ex As Exception
            MessageBox.Show("Error accessing Word document.")
        End Try
    End Sub
Posted
Updated 7-Nov-11 0:36am
v3

 
Share this answer
 
v2
SEHException generally refers to an exception that has not been thrown by .Net but by a native component. You should put a breakpoint inside your exception handler catch block and check the ExternalException.ErrorCode property and see what it says. Check your stacktrace also, as you might be able to determine which component is throwing the error.

Hope this helps
 
Share this answer
 
Comments
Wayne Gaylard 7-Nov-11 7:06am    
I have just tested your code and it works exactly like it should. I get no exception.
RICKY_Xxx 8-Nov-11 0:38am    
Thanks for help.i think problem is that i created file on my network folder. that why it gives exception
RICKY_Xxx 9-Nov-11 0:01am    
i found solution yesterday for this exception that I re-Installed office 2007.then i ran code it was running.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900