PDA

View Full Version : [SOLVED] How to Paste info from an Excel Macro into a new word document



PNJ
12-03-2004, 02:24 PM
OK, I have written an Excel macro that creates a String. I want to take that String and copy it into a new word document. I can open a new word document using

Application.ActivateMicrosoftApp (xlMicrosoftWord)

but I don't seem to be able to access the document from the excel macro to copy the information.

Is there a simple way to do this? Does some one have some sample code to show me?

Thanks in advance.

PNJ

Zack Barresse
12-03-2004, 02:27 PM
Hi PNJ, welcome to the board!


Here is some sample code ...



Option Explicit
Sub openWordPlease()
Dim wordApp As Object, wordFile As Object, myFile As String
myFile = InputBox("Please enter the full path and filename of the Word " & _
"document to open:", "Path & Name")
If myFile = "" Then
MsgBox "Sorry, I don't know where that's at!", vbExclamation, "Sorry"
Exit Sub
End If
Set wordApp = CreateObject("Word.Application")
wordApp.Visible = True
Set wordFile = wordApp.Documents.Open(myFile)
On Error GoTo 0
If wordFile Is Nothing Then
MsgBox "File is not found!", vbInformation, "ERROR"
GoTo quickEnd
End If
With wordFile
.Range.Text = Range("A1").Value
.Save
End With
wordApp.Quit
quickEnd:
Set wordFile = Nothing
Set wordApp = Nothing
End Sub


As you can see, there are certain variables you can 'hardcode' if you like. It also closes the Word file when done. The information put in there is a range value you can substitute for your variable.


HTH

PNJ
12-03-2004, 03:14 PM
Thanks

PNJ