View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Debra Dalgleish Debra Dalgleish is offline
external usenet poster
 
Posts: 2,979
Default Filling a Word form from an Excel spreadsheet

If you have bookmarks in the Word document, you could use a macro
similar to the following:

'===========================
Sub CopyToWord()
'set a reference to Word
' -- in VBE, choose ToolsReferences
Dim ws As Worksheet
Dim r As Long
Dim WdApp As Object
Dim strPath As String
Dim strFile As String
Dim strFileNew As String
Dim doc As Object
Set ws = Sheets("Sheet1")

strPath = "C:\Data\"
strFile = "Test.doc"
strFileNew = "TestNew.doc"

On Error Resume Next
Set WdApp = GetObject(, "Word.Application")
If Err.Number < 0 Then
Err.Clear
Set WdApp = CreateObject("Word.Application")
End If

On Error GoTo 0
WdApp.Documents.Open Filename:=strPath & strFile, _
ConfirmConversions:=False, ReadOnly:=False
Set doc = WdApp.activedocument
WdApp.Visible = True

With WdApp
.Selection.Goto What:=wdGoToBookmark, Name:="bkmk1"
.Selection.TypeText Text:=CStr(ws.Range("Field01").Value)

.Selection.Goto What:=wdGoToBookmark, Name:="bkmk2"
.Selection.TypeText Text:=CStr(ws.Range("Field02").Value)
End With

doc.SaveAs Filename:=strPath & strFileNew
doc.Close SaveChanges:=wdSaveChanges
Set WdApp = Nothing

End Sub
'======================================

Roel wrote:
This must be quite a routine job but I haven't found a
good example anywhere.
I have an Excel worksheet containing data that will be
exported to a Word document which in fact is a form. My
VBA-program should open the Word document, export the
data to the correct locations in the form, save the form
under a new filename and close the Word documents.
Any examples coming close to solving this problem?
Thanks,
Roel



--
Debra Dalgleish
Excel FAQ, Tips & Book List
http://www.contextures.com/tiptech.html