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

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

Forum statistics

Threads
1,224,813
Messages
6,181,117
Members
453,021
Latest member
Justyna P

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
Back
Top