Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Visual Bsic 2008 with Word 2007 using .Execute(Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)

Avatar of siacom
siacomFlag for Australia asked on
Visual Basic.NET
1 Comment1 Solution881 ViewsLast Modified:
In Visual Basic 2008 SP!
I am trying to open word 2007 with a template and replace a range with a dataset record.

I get an error at .Execute(Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)

error is:
COMExeption was unhandled
This command is not available.

If I use try .. catch or on error the the replacement still happens. But I want to avoid this error from happening at all.

Is there a better way to construct the statement ?
I have tried 2 different methods to prevent this but have come up with the same result.
Private Sub rcPILetters_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rcPILetters.SelectedIndexChanged
        If Not ThisApp.DONOTHING Then
            ' MsgBox(Me.rcPILetters.SelectedItem.Text & " : " & Me.rcPILetters.SelectedItem.Tag)
            '    ThisApp.DONOTHING = False
            '    ThisApp.DONOTHING = False
            Dim word As New Microsoft.Office.Interop.Word.Application
            Dim doc As Microsoft.Office.Interop.Word.Document
            ' doc = word.Documents.Open(Me.rcPILetters.SelectedItem.Tag)
                doc = word.Documents.Add(Me.rcPILetters.SelectedItem.Tag)
            Catch ex As Exception
                MsgBox("Error accessing Word document.", MsgBoxStyle.Critical)
                Exit Sub
            End Try
            Dim WordRange As Microsoft.Office.Interop.Word.Range
            Dim wdReplaceAll As Microsoft.Office.Interop.Word.WdReplace
                For Each WordRange In doc.StoryRanges
                    With WordRange.Find
                        .Text = "<<FirmName>>"
                        With .Replacement
                            .Text = MainDbPiForm.bsCustomerDetails.Current("FirmsName")
                        End With
                        .Execute(Replace:=wdReplaceAll, _
                               Format:=True, MatchCase:=True, _
                    End With
                    With WordRange.Find
                        .Text = "<<Street>>"
                        .Replacement.Text = MainDbPiForm.bsCustomerDetails.Current("Street")
                        .Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue
                    End With
                Next WordRange
            Catch ex As Exception
            End Try
            word.Visible = True
        End If
    End Sub
Avatar of siacom
siacomFlag of Australia image

Our community of experts have been thoroughly vetted for their expertise and industry experience.

This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answers