+ Reply to Thread
Results 1 to 5 of 5

Find/Replace macro from excel to word

  1. #1
    Matt
    Guest

    Find/Replace macro from excel to word

    Hi all:

    I am trying to run a macro from excel to open up a word document called
    doc1.doc on C:\doc1.doc and do a find and replace in the word document.
    I have the code written below and it does the search but it does not
    do the replace. Please help as I have been pulling my hair out.
    Thanks in advance.


    Sub Find_and_Replace()
    Dim appWD As Object
    Set appWD = CreateObject("Word.Application")

    appWD.documents.Open("C:\Doc1.doc").Application.Visible = True

    With appWD.Visible = True
    With appWD.Application.Selection.Find
    .Text = "Date"
    .Replacement.Text = "Time"
    .Forward = True
    .Wrap = wdFindContinue
    End With

    Do While appWD.Application.Selection.Find.Execute
    appWD.Application.Selection.Find.Execute Replace:=wdReplaceAll
    Loop

    End With

    End Sub


  2. #2
    Tom Ogilvy
    Guest

    Re: Find/Replace macro from excel to word

    Since you are using selection, do you have anything selected. I would guess
    not.

    --
    Regards,
    Tom Ogilvy


    "Matt" <MattSonnier@gmail.com> wrote in message
    news:1154281962.167956.248660@p79g2000cwp.googlegroups.com...
    > Hi all:
    >
    > I am trying to run a macro from excel to open up a word document called
    > doc1.doc on C:\doc1.doc and do a find and replace in the word document.
    > I have the code written below and it does the search but it does not
    > do the replace. Please help as I have been pulling my hair out.
    > Thanks in advance.
    >
    >
    > Sub Find_and_Replace()
    > Dim appWD As Object
    > Set appWD = CreateObject("Word.Application")
    >
    > appWD.documents.Open("C:\Doc1.doc").Application.Visible = True
    >
    > With appWD.Visible = True
    > With appWD.Application.Selection.Find
    > .Text = "Date"
    > .Replacement.Text = "Time"
    > .Forward = True
    > .Wrap = wdFindContinue
    > End With
    >
    > Do While appWD.Application.Selection.Find.Execute
    > appWD.Application.Selection.Find.Execute Replace:=wdReplaceAll
    > Loop
    >
    > End With
    >
    > End Sub
    >




  3. #3
    JMB
    Guest

    RE: Find/Replace macro from excel to word

    This seems to work fine on my box. In addition to Tom's point, I think you
    will have problems using word constants (wdfindcontinue and wdreplaceall)
    since you are using late binding.

    Sub Find_and_Replace()
    Dim appWD As Object
    Dim docWD As Object
    Set appWD = CreateObject("Word.Application")

    Set docWD = appWD.documents.Open("C:\Doc1.doc")
    appWD.Visible = True

    With docWD.Content.Find
    .Text = "Date"
    .Replacement.Text = "Time"
    .Forward = True
    .Wrap = 1
    .Execute Replace:=2
    End With

    End Sub


    "Matt" wrote:

    > Hi all:
    >
    > I am trying to run a macro from excel to open up a word document called
    > doc1.doc on C:\doc1.doc and do a find and replace in the word document.
    > I have the code written below and it does the search but it does not
    > do the replace. Please help as I have been pulling my hair out.
    > Thanks in advance.
    >
    >
    > Sub Find_and_Replace()
    > Dim appWD As Object
    > Set appWD = CreateObject("Word.Application")
    >
    > appWD.documents.Open("C:\Doc1.doc").Application.Visible = True
    >
    > With appWD.Visible = True
    > With appWD.Application.Selection.Find
    > .Text = "Date"
    > .Replacement.Text = "Time"
    > .Forward = True
    > .Wrap = wdFindContinue
    > End With
    >
    > Do While appWD.Application.Selection.Find.Execute
    > appWD.Application.Selection.Find.Execute Replace:=wdReplaceAll
    > Loop
    >
    > End With
    >
    > End Sub
    >
    >


  4. #4
    Matt
    Guest

    Re: Find/Replace macro from excel to word

    Thanks. I appreciate the help. It finally works!
    ~Matt


    JMB wrote:
    > This seems to work fine on my box. In addition to Tom's point, I think you
    > will have problems using word constants (wdfindcontinue and wdreplaceall)
    > since you are using late binding.
    >
    > Sub Find_and_Replace()
    > Dim appWD As Object
    > Dim docWD As Object
    > Set appWD = CreateObject("Word.Application")
    >
    > Set docWD = appWD.documents.Open("C:\Doc1.doc")
    > appWD.Visible = True
    >
    > With docWD.Content.Find
    > .Text = "Date"
    > .Replacement.Text = "Time"
    > .Forward = True
    > .Wrap = 1
    > .Execute Replace:=2
    > End With
    >
    > End Sub
    >
    >
    > "Matt" wrote:
    >
    > > Hi all:
    > >
    > > I am trying to run a macro from excel to open up a word document called
    > > doc1.doc on C:\doc1.doc and do a find and replace in the word document.
    > > I have the code written below and it does the search but it does not
    > > do the replace. Please help as I have been pulling my hair out.
    > > Thanks in advance.
    > >
    > >
    > > Sub Find_and_Replace()
    > > Dim appWD As Object
    > > Set appWD = CreateObject("Word.Application")
    > >
    > > appWD.documents.Open("C:\Doc1.doc").Application.Visible = True
    > >
    > > With appWD.Visible = True
    > > With appWD.Application.Selection.Find
    > > .Text = "Date"
    > > .Replacement.Text = "Time"
    > > .Forward = True
    > > .Wrap = wdFindContinue
    > > End With
    > >
    > > Do While appWD.Application.Selection.Find.Execute
    > > appWD.Application.Selection.Find.Execute Replace:=wdReplaceAll
    > > Loop
    > >
    > > End With
    > >
    > > End Sub
    > >
    > >



  5. #5
    JMB
    Guest

    Re: Find/Replace macro from excel to word

    Glad to hear it.

    "Matt" wrote:

    > Thanks. I appreciate the help. It finally works!
    > ~Matt
    >
    >
    > JMB wrote:
    > > This seems to work fine on my box. In addition to Tom's point, I think you
    > > will have problems using word constants (wdfindcontinue and wdreplaceall)
    > > since you are using late binding.
    > >
    > > Sub Find_and_Replace()
    > > Dim appWD As Object
    > > Dim docWD As Object
    > > Set appWD = CreateObject("Word.Application")
    > >
    > > Set docWD = appWD.documents.Open("C:\Doc1.doc")
    > > appWD.Visible = True
    > >
    > > With docWD.Content.Find
    > > .Text = "Date"
    > > .Replacement.Text = "Time"
    > > .Forward = True
    > > .Wrap = 1
    > > .Execute Replace:=2
    > > End With
    > >
    > > End Sub
    > >
    > >
    > > "Matt" wrote:
    > >
    > > > Hi all:
    > > >
    > > > I am trying to run a macro from excel to open up a word document called
    > > > doc1.doc on C:\doc1.doc and do a find and replace in the word document.
    > > > I have the code written below and it does the search but it does not
    > > > do the replace. Please help as I have been pulling my hair out.
    > > > Thanks in advance.
    > > >
    > > >
    > > > Sub Find_and_Replace()
    > > > Dim appWD As Object
    > > > Set appWD = CreateObject("Word.Application")
    > > >
    > > > appWD.documents.Open("C:\Doc1.doc").Application.Visible = True
    > > >
    > > > With appWD.Visible = True
    > > > With appWD.Application.Selection.Find
    > > > .Text = "Date"
    > > > .Replacement.Text = "Time"
    > > > .Forward = True
    > > > .Wrap = wdFindContinue
    > > > End With
    > > >
    > > > Do While appWD.Application.Selection.Find.Execute
    > > > appWD.Application.Selection.Find.Execute Replace:=wdReplaceAll
    > > > Loop
    > > >
    > > > End With
    > > >
    > > > End Sub
    > > >
    > > >

    >
    >


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1