Export data from Excel to Word template and save output as *.doc usiung VBA

Peter Ren

New Member
Joined
May 1, 2018
Messages
3
Hi, I have a spreadsheet that is populated with data.
Each column has a header and there can be many rows.
I want to copy/export a selected row of data to a Word Template,
Save the Word file as Filename.Doc
Close trhe Word Application
and then return to Excel (Command Button that calls the code).

I found this example and thought it would/might work:
Option Explicit

Sub ReplaceText()
Dim wApp As Word.Application
Dim wDoc As Word.Document
Set wApp = CreateObject("Word.Application")
wApp.Visible = True

Set wDoc = wApp.Documents.Add(Template:="D:SEARS SOLAR Template - CSR", NewTemplate:=False, DocumentType:=0)
With wDoc
.Application.Selection.Find.text = "<<Customer Name>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("A5")
.Application.Selection.EndOf

.Application.Selection.Find.text = "<<dob>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("A6")

.ActiveDocument.SaveAs Filename:="C:\Documents\NewWordDocumentFromTemplate.doc"
End With
End Sub

This code fails at the first DIM Statement so I can't do any testing/experimenting to try and make it work.
Am I on the right/wrong track?
Your help will be greatly appreciated
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

Watch MrExcel Video

Forum statistics

Threads
1,129,367
Messages
5,635,853
Members
416,886
Latest member
coreyalaurence37

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top