PDA

View Full Version : Solved: Excel to word at position



peterwmartin
12-16-2006, 09:18 PM
Hi all this is my first post I hope it works.
Yes I am new to this stuff.
I have borrowed,loan,stole some code I need help with. I would like to select from excel and paste special to a open word document at the cursor position(if possible) this bit of code does work however i get an error.
Sub CopyToWord()

'This code requires a referece to the Word object model
Dim Appword As Object 'Word.Application
Set Appword = GetObject(, "Word.Application")
'Appword.Documents.Add
Selection.Copy
'Range("A15:B36").Copy
Appword.Selection.PasteSpecial(link:=True).Select
' Appword.Visible = True
Set Appword = Nothing

End Sub
On the bold line can someone please help. thank you

mdmackillop
12-17-2006, 08:53 AM
Hi Peter
Welcome to VBAX
Regards
MD

Sub CopyToWord()
'This code requires a referece to the Word object model
Dim Appword As Object
Set Appword = GetObject(, "Word.Application")
ActiveCell.Copy
Appword.Selection.PasteSpecial Link:=True
Set Appword = Nothing
End Sub

Bob Phillips
12-17-2006, 10:40 AM
Nice try Malcolm, but it still throws the same error for me.

Did you actually get it working? I Googled for that problem, and saw a few hits, but no solutions.

mdmackillop
12-17-2006, 10:51 AM
Hi Bob,
With an open Word document the code works for me.
Regards
Malcolm

Bob Phillips
12-17-2006, 11:07 AM
Hi Bob,
With an open Word document the code works for me.
Regards
Malcolm

How odd, for me it doesn't. What I found suggests that the prolem was not universally reproducible.

Did you get a problem with the oroginal code?

mdmackillop
12-17-2006, 12:22 PM
I got Error 424 Object Required on the AppWord.Selection line, but the text had copied. I'm guessing it was the Select statement that failed.

fixo
12-17-2006, 12:56 PM
Not sure about but you could be to use this one:


ApWord.Selection.PasteExcelTable Link:=True


~'J'~

peterwmartin
12-17-2006, 05:21 PM
Thank you everyone for your help yes I did get it to work. Have also changed the code to open a new document. Can some one tell me why I can paste to an open document. I can open a document and paste into it.
However if I open one with the code I can't Then use the copy part to paste into it.

mdmackillop
12-18-2006, 12:44 AM
However if I open one with the code I can't Then use the copy part to paste into it.
Haven't tried this, but how do you determine where the text is to be pasted. Is it to be inserted at a bookmark, in a formfield or what?

peterwmartin
12-18-2006, 04:24 AM
not sure for the moment only wanted to add it to the same document, however the question you asked is tempting can you insert it into a textbox.

mdmackillop
12-18-2006, 05:15 AM
Have you looked at this?
http://vbaexpress.com/kb/getarticle.php?kb_id=184

peterwmartin
12-18-2006, 06:28 AM
Thanks MD but now I am totally confused. Please give me some time to look through the code. You make reference to bookmarks which I haven't come across before so I will start by looking at what they are and how to use them. I am sure I will have more questions.