PDA

View Full Version : [SOLVED] Find and replace in word doc from excel vba.



sadiablo
10-31-2013, 09:08 AM
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.

snb
10-31-2013, 09:31 AM
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

macropod
11-01-2013, 01:11 AM
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.

sadiablo
11-01-2013, 06:05 AM
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.

TheKirkwoods
09-28-2015, 09:20 AM
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. :banghead: Any ideas?

liveperson
04-12-2016, 09:57 AM
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. :banghead: 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