Consulting

Results 1 to 6 of 6

Thread: Find and replace in word doc from excel vba.

  1. #1

    Lightbulb Find and replace in word doc from excel vba.

    I am trying to write code that will find the word "Date:" in a word document and replace it with todays date. I've managed to get this far but all it seems to do is find and select it, not replace it.

    Sub docsearch()

    Set wd = CreateObject("word.application")
    wd.Documents.Open ("C:\Documents and Settings\Owner\My Documents\downloads\work\M-F-380.1.doc")
    wd.Visible = True

    With wd.Application.Selection.Find

    .Text = "Date:"
    .Replacement.Text = "Datetest"
    .Wrap = wdFindContinue
    .Execute Replace:=wdReplaceAll
    End With




    End Sub


    Any ideas?

    Thanks.

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    First idea: please use code tags !
    Second idea:

    Sub M_snb()
       with getobject("C:\Documents and Settings\Owner\My Documents\downloads\work\M-F-380.1.doc")
          .content.Find.execute "Date:", , , , , , , , , format(date,"dd-mm-yyyy"),2
          .close -1
       end with
    End Sub

  3. #3
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Try:
    Sub DocSearch()
    Dim wdApp As Object, wdDoc As Object
    Set wdApp = CreateObject("word.application")
    wdApp.Visible = True
    Set wdDoc = wdApp.Documents.Open("C:\Documents and Settings\Owner\My Documents\downloads\work\M-F-380.1.doc")
    With wdDoc.Content.Find
      .Text = "Date:"
      .Replacement.Text = "Datetest"
      .Wrap = wdFindContinue
      .Execute Replace:=wdReplaceAll
    End With
    Set wdApp = Nothing: Set wdDoc = Nothing
    End Sub
    You might also want to add some code to save the changes, close the file and quit Word, before the 'Set wdApp = Nothing: Set wdDoc = Nothing' line. Note that you don't really need 'wdApp.Visible = True' unless you want to leave the document active after the code has run; if you don't want to do that, setting it to false would reduce screen flicker.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  4. #4
    Thanks guys, ended up getting it working today anyway. Your right about the .visible part, I only had it in there while I was stuffing around getting it working, and as far as saving it and quitting goes, I didn't want it to save just print then close without saving. I have now got everything I had set out to do working, which was basically click a single icon on my desktop which would print out all the paperwork I need for my day at work, already pre-filled with the date, and shift info.

    So thanks heaps guys.

  5. #5

    Angry

    Okay...so, yes, this code (run from Excel VBA) works when replacing words in a Word document, but it doesn't search and replace in the headers. Any ideas?

  6. #6
    Quote Originally Posted by TheKirkwoods View Post
    Okay...so, yes, this code (run from Excel VBA) works when replacing words in a Word document, but it doesn't search and replace in the headers. Any ideas?
    You can solve the problems by adding one more variable, wdRng.
    The key is that you need to tell your code that you are working on the whole range in the word document, not only the body part.


    Sub xyz()

    Dim wdApp As Word.Application
    Dim wdDoc As Word.Document
    Dim wdRng As Word.Range

    Set wdApp = CreateObject("word.application")
    wdApp.Visible = True
    Set wdDoc = wdApp.Documents.Open("C:\Testing_VBA.doc")
    For Each wdRng In wdDoc.StoryRanges

    With wdRng.Find
    .Text = "Date: "
    .Replacement.Text = "Datetest"
    .Wrap = wdFindContinue
    .Execute Replace:=wdReplaceAll
    End With

    Set wdApp = Nothing: Set wdDoc = Nothing: Set wdRng = Nothing

    Next wdRng
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •