PC Review


Reply
Thread Tools Rate Thread

Copy/Paste Multiple cells from Excel to Word

 
 
lo_rah via OfficeKB.com
Guest
Posts: n/a
 
      14th Feb 2008
Hello,
I'm new at VBA and have been polking around looking for some answers but
haven't found them yet. The code I'm using is listed below. Basically what
I want to do is copy cell B2 into word as a heading 1, cell C2 as a heading 2,
D2 as heading 3, and G2 as body text. I figured I would need to have two
macros, one to bring the text into word from excel and one to change the
style of the text to the correct heading/body text arrangement.

The problem I'm having with the code from excel that I'm using is that it is
pasting over what was previously brought it so i'm only left with the the
text for D2 and nothing else. I tried to make it add a paragraph at the end
of each so that it wouldn't overwrite it, but what's happening is the text is
being selected and pasted over each time. I don't know really anything about
VBA so anything I try is like a stab in the darkness. In order to get the
code to copy and past all 4 cells in a row i repeated the following code
which is opening the word doc and i think that's where the problem is. I
just don't know how to fix it:

On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set wdApp = CreateObject("Word.Application")
End If
Err.Clear
On Error GoTo 0

Set wdDoc = wdApp.Documents.Open(fNameAndPath)
wdApp.Visible = True

I don't know if i'm going to need to use bookmarks to keep this from
happening, or if there is another way. Also, I need a solution that would
work with a loop, because i will need to loop this code until the frist blank
row.

Laura

The code:

Dim wdApp As Object
Dim wdDoc As Object
Dim fNameAndPath As String


fNameAndPath = "C:\data\Test.doc"

ActiveSheet.Range("B2:B2").Copy

On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set wdApp = CreateObject("Word.Application")
End If
Err.Clear
On Error GoTo 0

Set wdDoc = wdApp.Documents.Open(fNameAndPath)
wdApp.Visible = True

With wdDoc.Content
.Font.Name = "Tahoma"
.Font.Size = 20
.PasteSpecial DataType:=wdPasteText
End With

ActiveDocument.Content.InsertParagraphAfter

Workbooks("test.xls").Activate
ActiveSheet.Range("C2:C2").Copy

AppActivate "Microsoft Word"

With wdDoc.Content
.Font.Name = "Tahoma"
.Font.Size = 18
.PasteSpecial DataType:=wdPasteText
End With

ActiveDocument.Content.InsertParagraphAfter

Workbooks("test.xls").Activate
ActiveSheet.Range("D22").Copy

AppActivate "Microsoft Word"

With wdDoc.Content
.Font.Name = "Tahoma"
.Font.Size = 16
.PasteSpecial DataType:=wdPasteText
End With

ActiveDocument.Content.InsertParagraphAfter

Workbooks("test.xls").Activate
ActiveSheet.Range("G2:G2").Copy

AppActivate "Microsoft Word"

With wdDoc.Content
.Font.Name = "Tahoma"
.Font.Size = 14
.PasteSpecial DataType:=wdPasteText
End With

ActiveDocument.Content.InsertParagraphAfter

End Sub

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...mming/200802/1

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I copy data in Excel cells and paste in Word w/o cell borde =?Utf-8?B?TnNpbW0xMw==?= Microsoft Excel Misc 1 1st Sep 2006 03:35 PM
How to copy cells excel data and paste them under MS Word bookmark Bon Microsoft Excel Programming 0 17th Jan 2006 03:36 PM
How do I copy selected cells in excel and paste into word table =?Utf-8?B?UkVWQQ==?= Microsoft Excel Programming 2 4th Jan 2006 09:43 PM
copy multiple worksheets of a workbook, and paste onto a Word document ( either create new doc file or paste onto an existing file.) I need this done by VBA, Excel Macro Steven Microsoft Excel Programming 1 17th Oct 2005 08:56 AM
Excel splits Word table into multiple cells on paste Phil Microsoft Excel Misc 4 14th Mar 2004 05:25 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:40 AM.