Click here to Skip to main content
15,868,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to generate a word document with some syntax inside it. It find and replace for the whole document except for the footer. Please help me. Thanks in advanced!
VB
Public Sub WordDocumentReminder4(fileName As Object, NewFileName As Object)
        Dim missing As Object = System.Reflection.Missing.Value
        Dim wordApp As Microsoft.Office.Interop.Word.Application = New Microsoft.Office.Interop.Word.Application
        Dim aDoc As Microsoft.Office.Interop.Word.Document = Nothing

        Try
            aDoc = wordApp.Documents.Open(fileName, missing, missing, missing, missing, missing, _
             missing, missing, missing, missing, missing, missing, _
             missing, missing, missing, missing)

            Dim Dtable4 As New DataTable
            Dtable4 = ObjDBConn.GetDataTable("Exec Aduan.ReminderLetter3DataGET " & _
                                      "'" & txtcomplaintid.Text.ToString & "','" & txtexplanationid.Text.ToString & "'")
            aDoc.Activate()

            If Dtable4.Rows.Count > 0 Then
                Me.FindAndReplace(wordApp, "<<OurRefNo>>", Dtable4.Rows(0).Item("OurRefNo").ToString)
                Me.FindAndReplace(wordApp, "<<LetterIssueDate>>", Dtable4.Rows(0).Item("LetterIssueDate").ToString)
                Me.FindAndReplace(wordApp, "<<TO>>", Dtable4.Rows(0).Item("TO").ToString)
                Me.FindAndReplace(wordApp, "<<AlamatTO>>", Dtable4.Rows(0).Item("AlamatTO").ToString)
                Me.FindAndReplace(wordApp, "<<StateTO>>", Dtable4.Rows(0).Item("StateTO").ToString)
                Me.FindAndReplace(wordApp, "<<ComplaintNo>>", Dtable4.Rows(0).Item("ComplaintNo").ToString)
                Me.FindAndReplace(wordApp, "<<ReminderSubj3>>", Dtable4.Rows(0).Item("ReminderSubject3").ToString)
                Me.FindAndReplace(wordApp, "<<ReminderRefNo3>>", Dtable4.Rows(0).Item("ReminderRefNo3").ToString)
                Me.FindAndReplace(wordApp, "<<ReminderDate3>>", Dtable4.Rows(0).Item("ReminderDate3").ToString)
                Me.FindAndReplace(wordApp, "<<CmpnrName>>", Dtable4.Rows(0).Item("ComplainantName").ToString)
                'It did not find this syntax which is inside a footer.
                Me.FindAndReplace(wordApp, "<<FooterDate>>", Dtable4.Rows(0).Item("ReminderDate3").ToString)

                aDoc.SaveAs(NewFileName, missing, missing, missing, missing, missing, _
                 missing, missing, missing, missing, missing, missing, _
                 missing, missing, missing, missing)
            End If

            aDoc.Close(missing, missing, missing)
            wordApp.Quit(Type.Missing, Type.Missing, Type.Missing)
        Catch ex As Exception

            aDoc.Close(missing, missing, missing)
            wordApp.Quit(Type.Missing, Type.Missing, Type.Missing)
        End Try
    End Sub

Private Sub FindAndReplace(WordApp As Microsoft.Office.Interop.Word.Application, findText As Object, replaceWithText As Object)
        Dim matchCase As Object = True
        Dim matchWholeWord As Object = True
        Dim matchWildCards As Object = False
        Dim matchSoundsLike As Object = False
        Dim nmatchAllWordForms As Object = False
        Dim forward As Object = True
        Dim format As Object = False
        Dim matchKashida As Object = False
        Dim matchDiacritics As Object = False
        Dim matchAlefHamza As Object = False
        Dim matchControl As Object = False
        Dim read_only As Object = False
        Dim visible As Object = True
        Dim replace As Object = 2
        Dim wrap As Object = 1

        WordApp.Selection.Find.Execute(findText, matchCase, matchWholeWord, matchWildCards, matchSoundsLike, nmatchAllWordForms, _
         forward, wrap, format, replaceWithText, replace, matchKashida, _
         matchDiacritics, matchAlefHamza, matchControl)
    End Sub
Posted
Updated 17-Jul-13 23:08pm
v3
Comments
Maciej Los 18-Jul-13 4:32am    
And the issue is...
sern89 18-Jul-13 4:35am    
It did not find the syntax inside the footer.
sern89 22-Jul-13 3:52am    
hai @Maciej Los, thank you for the solution that you gave. and sorry to bother you again but after i declare the FindAndReplace procedure like what you said, it gives me a warning that it couldn't find the word document. and could you explain more about the range declaration? thank you.
Maciej Los 22-Jul-13 3:58am    
What to explain? Follow the links. See my updated answer ;)

1 solution

@sern89, you'll never get expected result because you're searching and replacing inside Selection.Range. If you would like to search and replace in all document, you need to define proper range:
How to: Programmatically Define and Select Ranges in Documents[^]
How to: Programmatically Set Search Options in Word[^]
How to: Programmatically Search for and Replace Text in Documents[^]

[EDIT #1]
How to automate Word to set and retrieve section header and footer information[^]
How to: Programmatically Add Headers and Footers to Documents[^]
[/EDIT]

Declaration and definition of FindAndReplace procedure is wrong.
VB
Private Sub FindAndReplace(WordApp As Microsoft.Office.Interop.Word.Application, findText As Object, replaceWithText As Object)

It should be:
VB
Private Sub FindAndReplace(Worddoc As Microsoft.Office.Interop.Word.Document, findText As String, replaceWithText As String)

because of the type of input parameters:
VB
.Text = "find me"
.Replacement.Text = "Found"
 
Share this answer
 
v2

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