Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default 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.Vi sible = 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default 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" wrote in message
oups.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.Vi sible = 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   Report Post  
Posted to microsoft.public.excel.programming
JMB JMB is offline
external usenet poster
 
Posts: 2,062
Default 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.Vi sible = 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default 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.Vi sible = 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   Report Post  
Posted to microsoft.public.excel.programming
JMB JMB is offline
external usenet poster
 
Posts: 2,062
Default 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.Vi sible = 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
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
find and replace \n wildcards like word julesgf Excel Discussion (Misc queries) 4 February 12th 09 03:11 AM
Find/Replace macro from excel into Word Matt[_40_] Excel Programming 0 July 28th 06 04:37 PM
How can I use find and replace to delete a word in Excel? callpaultwt Excel Discussion (Misc queries) 2 December 1st 05 09:11 PM
Ability for Excel to find and replace graphic objects (like Word) Marjan S Excel Worksheet Functions 4 August 26th 05 03:44 AM
macro to Find Replace in Excel Nurddin Excel Discussion (Misc queries) 7 January 3rd 05 04:29 AM


All times are GMT +1. The time now is 10:49 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"