PDA

View Full Version : Excel to word



photon_ed
09-01-2006, 12:45 AM
Hello All,

Can anyone advise on copying and pasting from excel to word.
Many thanks

yours,
Ed

matthewspatrick
09-01-2006, 07:57 AM
What do you need, specifically? I can normally copy from Excel and paste to Word quite easily...

photon_ed
09-01-2006, 08:10 AM
Thanks for you reply.
I apologies for not being clear.
I would like to know the VBA code to copy and paste infomation from excel to word.
Many thanks

yours,
Ed

matthewspatrick
09-01-2006, 09:18 AM
OK, a true copy-paste between apps using code is tricky.

Can you describe what you need to do in some detail?

JonPeltier
09-02-2006, 11:57 AM
I would like to know the VBA code to...
Office applications come with a very handy tool called the Macro Recorder. Turn it on, then do manually what you want to do in code. The recorded code isn't perfect, because it captures a lot of mouse actions, such as activating and selecting objects, which are not strictly required. But it will get you started, and it will help with syntax.

Go to Excel, turn on the macro recorder, and copy what you need to copy. Go to Word, turn on the macro recorder, and paste what you need pasted. You'll have to get some code to get Excel to work with the Word application; this might be a good place to start:

http://peltiertech.com/Excel/XL_PPT.html

Use the Excel code you recorded, and the Word code, keeping in mind that the Word code will need to be prefixed with a reference to the Word application, document, or other Word object.

lucas
09-02-2006, 09:06 PM
for a range:

Sub CopyToWord()

'This code requires a referece to the Word object model
Dim Appword As New Word.Application
Dim wdDoc As Word.Document

Set Appword = CreateObject("Word.Application")
Appword.Documents.Add

Range("A15:B36").Copy

Appword.Selection.Paste


Appword.Visible = True

End Sub

photon_ed
09-04-2006, 12:31 AM
Many thanks for all the responds.

Lucas: Visual Basic doesnt seem to like the line " Dim Appword As New Word.Application " and a popup message saying "Compile error: User-defined type not defined".
Pelase advise.

yours,
Ed

mdmackillop
09-04-2006, 12:33 AM
Hi Ed
You need to add a reference to Word in the VBE as the Comment line in the code states.
Tools/References Microsoft Word xx Object Library.

photon_ed
09-04-2006, 12:47 AM
That is perfect!
Many thanks to ALL.

yours,
Ed

matthewspatrick
09-04-2006, 05:21 AM
Lucas: Visual Basic doesnt seem to like the line " Dim Appword As New Word.Application " and a popup message saying "Compile error: User-defined type not defined".
Pelase advise.

Ed,

As Malcolm indicated, you needed to set a reference. You could have also used late binding, and eliminated the need to set a reference. (Late binding is especially useful if you think people with different versions of Word will use the code, as this can cause version conflicts if you use early binding.)


Sub CopyToWord()

'This code requires a referece to the Word object model
Dim Appword As Object 'Word.Application

Set Appword = CreateObject("Word.Application")
Appword.Documents.Add

Range("A15:B36").Copy

Appword.Selection.Paste


Appword.Visible = True

Set Appword = Nothing

End Sub