Pass variables from Excel to Word

S

Stuart Duncan

Hi,

I have an Excel application that copies several cell values to variables.

I have a Word template that I want to put the variables into then save and
print the the template as a Word document.

I am struggling to open the template from within my Excel code and write the
variables to the document.

I would appreciate any help.

Thanks

Stuart Duncan
 
G

Guest

The following will get you started with opening Word via
Excel. Beyond this the issue isn’t likely to be passing
the variables from one to the other if the entirety of
your code exists in Excel. - Pikus

Dim WordObj As Word.Application
Dim WordDoc As Word.Document
Dim stringVar As String

On Error Resume Next

Set WordObj = GetObject("Word.Application")

If WordObj Is Nothing Then
Set WordObj = CreateObject("Word.Application")
End If

WordObj.Visible = True

Set WordDoc = WordObj.Documents.Add

stringVar = “Hello World!”

WordObj.Selection.TypeText stringVar
 
P

Peter Hewett

Hi Stuart Duncan

You don't need to pass values between Excel and Word. From Excel you can directly
manipulate the entire Word object model. Just make sure that your Excel project has a
reference to Word.

You can then:
Dim appWord As Word.Application
Dim docNew An Word.Document
Dim rngBody as Word.Range

Set AppWord = New Word.Application

Set docNew = AppWord.Documents.Add
Set rngBody = docNew.Content
rngBody.InsertAfter "Here's some text. "
rngBody.InsertAfter "Here's some more text"

docNew.SaveAs "c:\Temp\new.doc"
docNew.PrintOut
docNew.Close wdDoNotSaveChanges

appWord.Quit
Set appWord = Nothing

BTW you say Word template but I assume you really mean a Word document.

HTH + Cheers - Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top