Export data to from Excel to Word

J

jswalsh33

I have an Excel worksheet with columns of data that was filled in by a Macro.

I would like to extract one column of that data and paste it into a Word
document with a Macro.

I have tried using answers to other questions but they don't quite make it
work.

Help?

Jim
 
K

ker_01

Jim- I'll provide sample code here (and hopefully it will help); If you can't
get it working, I'd suggest re-posting and indicating what version of Excel
and Word you are using, and where you are getting stuck- is Word not opening?
Is Word open, but your desired contents aren't getting pasted in properly?
etc.

This code should give you a basic starting point; you will still need to use
VBA to cycle through your target column's values to add them to Word instead
of using the WER array.

My apologies to whomever originally posted this code- I didn't capture that
way back (mid 90's?) when I first acquired this snippet.

Best,
Keith

Sub createDoc()

Dim wordApp As Word.Application
Dim wordDoc As Word.Document
Dim wordRng As Word.Range
Dim wordPara As Word.Paragraph
Dim WER(1 To 3) As Variant


Set wordApp = CreateObject("Word.Application")

With wordApp

.WindowState = wdWindowStateMaximize
.Documents.Add
Set wordDoc = wordApp.ActiveDocument
Set wordRng = wordDoc.Range

With wordRng

WER(1) = "tree"
WER(2) = "bed"
WER(3) = "car"

.InsertAfter WER(1)
.InsertParagraphAfter
.InsertAfter WER(2)
.InsertParagraphAfter
.InsertAfter WER(3)
.InsertParagraphAfter

' insert a blank paragraph between the two paragraphs
.InsertParagraphAfter

End With

Set wordPara = wordRng.Paragraphs(3)

With wordPara.Range

.Bold = True
.Italic = False
.Font.Size = 12
.InsertAfter "Report Created: "
.Collapse Direction:=wdCollapseEnd
.InsertDateTime DateTimeFormat:="MM-DD-YY HH:MM:SS"

End With

.ActiveDocument.saveas "c:\Test_Create_Doc.Doc"
.Quit

End With

Set wordPara = Nothing
Set wordRng = Nothing
Set wordDoc = Nothing
Set wordApp = Nothing

End Sub
 

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