Avatar of siacom
Flag for Australia

asked on 

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

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

Open in new window

Visual Basic.NET

Avatar of undefined
Last Comment

8/22/2022 - Mon