Results 1 to 2 of 2
  1. #1
    Goodge12 is offline Novice
    Windows XP Access 97
    Join Date
    Sep 2008
    Posts
    25

    VBA to find/replace text in word document

    Hi All-



    I'm trying to use a VBA in access to open up an existing Word template, then find/replace several bits of text through the document (with data in a field on my form) before re-saving as a pdf with a new naming convention. So far, I've been able to get the Word Document to open and then re-save properly, but I can't get the find/replace functionality to work. Could someone please take a look at my VBA and let me know where I've gone wrong? Here's what I'm using:

    Code:
     
    Private Sub btnEmailSupplier_Click()
    
    'opens template
        Dim db As DAO.Database
        Dim objWord As Word.Application
        Set objWord = New Word.Application
    With objWord
        .Documents.Open "mypath to word.docx"
        .Visible = True
        .Activate
    End With
        
    'Replaces date on first page
    With ActiveDocument.Content.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = "replace first date "
      .Replacement.Text = Me.CP_Return_By
      .Forward = True
      .Wrap = wdFindContinue
      .Format = False
      .MatchCase = False
      .MatchWholeWord = False
      .MatchWildcards = False
      .MatchSoundsLike = False
      .MatchAllWordForms = False
      .Execute Replace:=wdReplaceAll
    End With
    Thank you!

  2. #2
    Goodge12 is offline Novice
    Windows XP Access 97
    Join Date
    Sep 2008
    Posts
    25
    Answered my own question - revised code for anyone else who is trying to do this:

    Code:
    Private Sub btnEmailSupplier_Click()
    
        'opens  template
        Dim db As DAO.Database
        Dim objWord As Word.Application
        Set objWord = New Word.Application
    With objWord
        .Documents.Open "mypath to word.docx"
        .Documents("\\us.grainger.com\files\Office\LF\DB\PM\MerchandisingDBs\Template\Credit Package_SWS_template.docx").Activate
        .Visible = True
    End With
        
    'Replaces date on first page
    With objWord
        .Activate
    End With
    
    With objWord.Selection.Find
                .ClearFormatting
                .Replacement.ClearFormatting
                .Text = "replace first date"
                .Replacement.Text = Me.CP_Return_By
                .Forward = True
                .Wrap = wdFindContinue
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
                .Execute Replace:=2 'wdReplaceAll
    End With

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 05-31-2013, 10:10 AM
  2. Extract text from each word document in a file
    By mercapto in forum Programming
    Replies: 11
    Last Post: 03-12-2013, 10:29 PM
  3. Replies: 3
    Last Post: 02-22-2013, 06:41 AM
  4. Create new Word document.
    By Bill H in forum Programming
    Replies: 3
    Last Post: 06-12-2012, 06:40 AM
  5. Import Word document using VBA
    By degras in forum Import/Export Data
    Replies: 4
    Last Post: 04-12-2011, 02:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums