View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.misc
JMB JMB is offline
external usenet poster
 
Posts: 2,062
Default Generating Word docs from Excel

One example. You have to set a reference to Word as the following code uses
early binding (instead of late binding). In VBA, click Tools/References -
check the Microsoft Word Library. In the word template, I usually set up
placeholders, such as %NAME%, then use a search/replace operation to replace
these placeholders with data from Excel. Below, instead of hardcoding the
replacement text as I have done, you could refer to a particular value in one
of your Excel worksheets, such as

Worksheets("Sheet1").Range("A1").Value


Sub Test()
Dim wdApp As Word.Application
Dim wdDoc As Word.Document

Set wdApp = New Word.Application
Set wdDoc = wdApp.Documents.Open("I:\Excel\Test.doc")

With wdDoc
With .Content.Find
.Text = "%NAME%"
.Replacement.Text = "JEFF"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
.Close savechanges:=True
End With

Set wdDoc = Nothing
Set wdApp = Nothing

End Sub

"Jadedshade" wrote:

In excel how do I send data from fields to fill in spaces in a word document
template? I would also like to create a button in excel so that this can be
done easily.
I want to make buttons that can be clicked in excel that will enter a large
amount of specified text into the word document.
I cannot figure out how to do any of this.
Could someone give me examples of how to do these things?
It would be much appreciated.
Many thanks.