Inserting Excel Data into Word Columns

M

MarkPowell

Hi,

I am fairly new to VBA and Excel/Word and have been trying to automat
a sheet we have, I have created the Excel Sheet and unfortunately th
Word doc was created sometime ago and cannot be changed.

I have searched this forum and found this code:


Code
-------------------

Sub CreateNewWordDoc()
' to test this code, paste it into an Excel module
' add a reference to the Word-library
' create a new folder named C:\Foldername or edit the filnames in the code
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim i As Integer
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open("P:\InstallShield Metrics\GSS_Install_Jan04.Doc") ' open a document
' or
'Set wrdDoc = wrdApp.Documents.Open("P:\InstallShield Metrics\GSS_Install_Jan04.Doc")
' open an existing document
' example word operations
With wrdDoc
For i = 1 To 100
.Content.InsertAfter "Here is a example test line #" & i
.Content.InsertParagraphAfter
Next i
If Dir("P:\InstallShield Metrics\GSS_Install_Jan04.Doc") <> "" Then
Kill "P:\InstallShield Metrics\GSS_Install_Jan04.Doc"
End If
.SaveAs ("P:\InstallShield Metrics\GSS_Install_Jan04.Doc")
.Close ' close the document
End With
wrdApp.Quit ' close the Word application
Set wrdDoc = Nothing
Set wrdApp = Nothing
End Sub

-------------------


Which does the first part of opening the Word doc I want (trie
recording a macro to do this but that doesn't work)

The next step is to copy certain cell data into columns/rows in Word.


Firstly, can this be done?

Secondly, if so how would I go about getting, data in excel cell J2
to copy to Row 19, Column 2 in Word?

Any help much appreciated.
TIA,
Mark
 
M

MarkPowell

On this topic still (got no feedback yet :( )

I have this code for getting the data from the cells and next is
getting it to the Word doc. But I am getting a 1004: run-time error.

Please help.


Code:
--------------------

Sub Paste_word()
' Cell ranges to select
Range("A21:D21").Select
Selection.Copy
Set wrd = GetObject(, "Word.Application"):


With wrd:
With .Selection

Set MyRange = .Goto(wdGoToField, wdGoToPrevious)
.MoveDown Count:=19
.MoveRight Unit:=2 'previously wdCell
.PasteExcelTable False, False, False
End With
End With

End Sub

' Fill in the data. Rough example
' FillRow wrdDataDoc, 2, "data", "data", "data", "data"

--------------------
 
D

Dave Peterson

You code looks like it expects MSWord to be running and your document to be the
activedocument.

And since you're using GetObject, it looks like you may not have a reference set
to "Microsoft Word x.x Object Library" (in tools|references inside the VBE).

And if that last is true, then excel won't know what wdGoToField, wdGoToPrevious
are.

This version of your code did something--I'm not sure if it's enough to help you
though:

Option Explicit
Sub Paste_word()
Dim wrd As Object
Dim myRange As Object

' Cell ranges to select
Range("A21:D21").Select
Selection.Copy
Set wrd = GetObject(, "Word.Application")

With wrd
With .Selection
Set myRange = .Goto(7, 3) 'wdGoToField, wdGoToPrevious)
.MoveDown Count:=19
.MoveRight Unit:=2 'previously wdCell
.PasteExcelTable False, False, False
End With
End With

End Sub

If it didn't help, you may want to say what line caused the error.
 

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