• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Paste from Excel to open Word file or closed

Status
Not open for further replies.

Visor

Member
Greetings friends of the forum, paste this code range MyWord file excel in this closed, then opens it and displays it.

Code:
Private Sub CommandButton1_Click()
Hoja8.Range("A1:H32").Select
    Selection.CopyPicture xlScreen, xlPicture
    Archivo = ThisWorkbook.Path & "\Microbiologia I.docx"
    With CreateObject("word.application")
        Selection.CopyPicture xlScreen, xlPicture
        .Documents.Open Archivo
        .Selection.Paste
        .ActiveDocument.SaveAs (Archivo)
        .Visible = True
        .Activate
    End With
End Sub

But what if I want to paste into MyWord file that is already open ???
Error generates the code shown here:
File .Documents.Open
Because attempts to open a file that is already open.

I tried with the code that the master Jindon, kindly helped me but also it does not work, especially if the file is already open.

Code:
Private Sub CommandButton1_Click()
  Dim fn As String, myWord As Object
    fn = ThisWorkbook.Path & "\Microbiologia I.docx"
    With GetObject(, "word.application")
        If IsFileOpen(fn) Then 'File is open
        Hoja8.Range("A1:H32").Select
    Selection.CopyPicture xlScreen, xlPicture
  
            fn = Split(fn, "\")(UBound(Split(fn, "\")))
            'Set myWord =
            .Documents (fn)
                .Selection.Paste
                .ActiveDocument.SaveAs (Archivo)

        Else    'If file es closed
    Hoja8.Range("A1:H32").Select
    Selection.CopyPicture xlScreen, xlPicture
            'Set myWord =
            .Documents.Open (fn)
            .Selection.Paste
          ' CreateObject("word.application")
          .ActiveDocument.SaveAs (Archivo)
            .Visible = True
            .Activate

        End If
    End With
    MsgBox myWord.Name 'Ready!!
End Sub

How do I get my file already open,... and copy paste my rang?
I anticipate my thanks
 

Thread closed as a duplicate : continue on your original thread

where you wrote Fantastic to jindon for his very well working code !
 
Status
Not open for further replies.
Back
Top