VBA code to import data from excel to word

danboi1010

New Member
Joined
Nov 23, 2017
Messages
13
Hi, I am trying to get a macro to insert the data held in cell a1 into a word letter.

Code:
Private Sub CommandButton1_Click()

'Open Word


Dim WrdApp As Word.Application
Dim WrdDoc As Word.Document
Dim MyText As String
Dim Selection As String








Set WrdApp = CreateObject("Word.Application")
WrdApp.Visible = True
WrdApp.Activate
Set WrdDoc = WrdApp.Documents.Add






WrdApp.Selection.TypeText Text:="Hello"
WrdApp.Selection.TypeParagraph
WrdApp.Selection.TypeParagraph
WrdApp.Selection.TypeText Text:="Bye"
WrdApp.Selection.TypeParagraph
WrdApp.Selection.TypeParagraph






WrdApp.ActiveDocument.SaveAs Filename:="C:\Users\Dan.docx"


WrdApp.Quit




End Sub

This code does exactly what i want it to do but i cannot work out how to import the data held in a1 into the word letter.

I feel like it should be
Code:
WrdApp.Selection.cell = ("A1")
but of course it's not!

Any help would be great. Thanks.Dan.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
You really shouldn't be working with Selections; Use range objects instead. For example:
WrdDoc.Tables(1).Cell(1,1).Range.Text = "Hello World!"
 
Upvote 0
HI Macropod,

Thanks for that. TBH i haven't a clue when it comes to macros! I just recorded one and then followed the text. The reason i'm using selection is because i want to write a full letter within the VB code rather than use mail merge, and at certain points I would like to be able to import data from a cell rather than type in "Hello Word" eg. select cell a1 and for the data held in that cell to appear in the word letter. I hope that makes a bit of sense!
 
Upvote 0
i want to write a full letter within the VB code rather than use mail merge, and at certain points I would like to be able to import data from a cell rather than type in "Hello Word" eg. select cell a1 and for the data held in that cell to appear in the word letter.
You might save yourself a lot of work using a mailmerge. That said, you could replace "Hello World!" with the appropriate worksheet/cell reference (e.g. ActiveSheet.Range("A1").Text)
 
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,423
Members
448,961
Latest member
nzskater

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