ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Filling a Word form from an Excel spreadsheet (https://www.excelbanter.com/excel-programming/305668-filling-word-form-excel-spreadsheet.html)

Roel[_2_]

Filling a Word form from an Excel spreadsheet
 
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

Tom Ogilvy

Filling a Word form from an Excel spreadsheet
 

http://support.microsoft.com/support.../Q123/8/59.asp
ACC: Sample OLE Automation for MS Word and MS Excel

http://support.microsoft.com/support...aqVBOffice.asp
Frequently Asked Questions about Microsoft Office Automation Using Visual
Basic

http://support.microsoft.com/support...fdevinapps.asp
Programming Office from Within Office

http://support.microsoft.com/default...83&Product=xlw
How to Create a Form Letter with a Visual Basic Macro

http://support.microsoft.com/default...16&Product=xlw
XL2000: Macro to Link a Range of Cells in Word

http://support.microsoft.com/default...74&Product=xlw
OFF: How to Use (OLE) Automation with Word

--
Regards,
Tom Ogilvy


"Roel" wrote in message
...
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

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


Roel[_2_]

Filling a Word form from an Excel spreadsheet
 
Thanks, Tom. I'm trying to find my way here. Your help is
much appreciated.

Roel

-----Original Message-----

http://support.microsoft.com/support...icles/Q123/8/5

9.asp
ACC: Sample OLE Automation for MS Word and MS Excel

http://support.microsoft.com/support...Dev/FaqVBOffic

e.asp
Frequently Asked Questions about Microsoft Office

Automation Using Visual
Basic

http://support.microsoft.com/support...Dev/offdevinap

ps.asp
Programming Office from Within Office

http://support.microsoft.com/default.aspx?scid=kb;en-

us;138283&Product=xlw
How to Create a Form Letter with a Visual Basic Macro

http://support.microsoft.com/default.aspx?scid=kb;en-

us;213316&Product=xlw
XL2000: Macro to Link a Range of Cells in Word

http://support.microsoft.com/default.aspx?scid=kb;en-

us;184974&Product=xlw
OFF: How to Use (OLE) Automation with Word

--
Regards,
Tom Ogilvy


"Roel" wrote in

message
...
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



.


Roel[_2_]

Filling a Word form from an Excel spreadsheet
 
Thanks, Debra. This is of great help.
Roel

-----Original Message-----
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

.



All times are GMT +1. The time now is 05:26 AM.

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