ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Find and replace in Word from Excel (https://www.excelbanter.com/excel-programming/434540-find-replace-word-excel.html)

gab1972

Find and replace in Word from Excel
 
From Excel, I need to open a Word document, find "aaaa" in the Word
document, and then replace "aaaa" with the contents in A1 in Excel.
The code I have doesn't like .Selection.Find

Please help. Thanks in advance.

Sub FindReplace()
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open("Z:\COMMON FILES\Encroachment
Permits\Permit.Tracker\Testing\testdoc.doc")
With wrdDoc

With .Selection.Find
.Text = "aaaa"
.Replacement.Text = Cells("A1")
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With

If Dir("Z:\COMMON FILES\Encroachment Permits\Permit.Tracker
\Testing\MyNewWordDoc.doc") < "" Then
Kill "Z:\COMMON FILES\Encroachment Permits\Permit.Tracker
\Testing\MyNewWordDoc.doc"
End If
End With
End Sub

gab1972

Find and replace in Word from Excel
 
On Oct 5, 4:05*pm, gab1972 wrote:
From Excel, I need to open a Word document, find "aaaa" in the Word
document, and then replace "aaaa" with the contents in A1 in Excel.
The code I have doesn't like .Selection.Find

Please help. *Thanks in advance.

Sub FindReplace()
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
* * Set wrdApp = CreateObject("Word.Application")
* * wrdApp.Visible = True
* * Set wrdDoc = wrdApp.Documents.Open("Z:\COMMON FILES\Encroachment
Permits\Permit.Tracker\Testing\testdoc.doc")
* * With wrdDoc

* * * * With .Selection.Find
* * * * * * * * * * .Text = "aaaa"
* * * * * * * * * * .Replacement.Text = Cells("A1")
* * * * * * * * * * .Wrap = wdFindContinue
* * * * * * * * * * .Execute Replace:=wdReplaceAll
* * * * * * * * End With

* * * *If Dir("Z:\COMMON FILES\Encroachment Permits\Permit.Tracker
\Testing\MyNewWordDoc.doc") < "" Then
* * * * * * Kill "Z:\COMMON FILES\Encroachment Permits\Permit..Tracker
\Testing\MyNewWordDoc.doc"
* * * * End If
* * End With
End Sub


I got it...

Sub CreateNewWordDoc()
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Set wrdApp = GetObject(, "Word.Application")
wrdApp.Visible = True
wrdApp.Activate
Set wrdDoc = wrdApp.Documents.Open("Z:\COMMON FILES\Encroachment
Permits\Permit.Tracker\Testing\testdoc.doc")
With wrdDoc.Range.Find
.Text = "aaaa"
.Replacement.Text = "Insufficient plans."
.Execute Replace:=wdReplaceAll
End With
'wrdApp.Quit ' close the Word application
'Set wrdDoc = Nothing
'Set wrdApp = Nothing
End Sub

....in case anyone was wondering.

JLGWhiz[_2_]

Find and replace in Word from Excel
 
Thanks for the courtesy of posting back with your solution. It could help
someone else.


"gab1972" wrote in message
...
On Oct 5, 4:05 pm, gab1972 wrote:
From Excel, I need to open a Word document, find "aaaa" in the Word
document, and then replace "aaaa" with the contents in A1 in Excel.
The code I have doesn't like .Selection.Find

Please help. Thanks in advance.

Sub FindReplace()
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open("Z:\COMMON FILES\Encroachment
Permits\Permit.Tracker\Testing\testdoc.doc")
With wrdDoc

With .Selection.Find
.Text = "aaaa"
.Replacement.Text = Cells("A1")
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With

If Dir("Z:\COMMON FILES\Encroachment Permits\Permit.Tracker
\Testing\MyNewWordDoc.doc") < "" Then
Kill "Z:\COMMON FILES\Encroachment Permits\Permit.Tracker
\Testing\MyNewWordDoc.doc"
End If
End With
End Sub


I got it...

Sub CreateNewWordDoc()
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Set wrdApp = GetObject(, "Word.Application")
wrdApp.Visible = True
wrdApp.Activate
Set wrdDoc = wrdApp.Documents.Open("Z:\COMMON FILES\Encroachment
Permits\Permit.Tracker\Testing\testdoc.doc")
With wrdDoc.Range.Find
.Text = "aaaa"
.Replacement.Text = "Insufficient plans."
.Execute Replace:=wdReplaceAll
End With
'wrdApp.Quit ' close the Word application
'Set wrdDoc = Nothing
'Set wrdApp = Nothing
End Sub

....in case anyone was wondering.




All times are GMT +1. The time now is 12:24 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com