Consulting

Results 1 to 3 of 3

Thread: How to Paste info from an Excel Macro into a new word document

  1. #1
    VBAX Newbie
    Joined
    Dec 2004
    Posts
    4
    Location

    Question How to Paste info from an Excel Macro into a new word document

    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

  2. #2
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    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

  3. #3
    VBAX Newbie
    Joined
    Dec 2004
    Posts
    4
    Location
    Thanks

    PNJ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •