0

Hi all i´m very new to VBA i´m struggling with a macro i´m trying to create. I want to copy paste as figures to a word file multiples ranges from Excel.

This is the code i´ve come up with:

Sub imagem1()
    Dim objWord, objDoc As Object
    ActiveWindow.View = xlNormalView
    Worksheets(2).Range("A1:O47").Select
    Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture
    Set objWord = CreateObject("Word.Application")
    Set objDoc = objWord.Documents.Add
    objWord.Visible = True
    objWord.Selection.Paste
    objWord.Selection.TypeParagraph
    ActiveWindow.View = xlPageBreakPreview

End Sub

Sub imagem2()
    Dim objWord, objDoc As Object
    ActiveWindow.View = xlNormalView
    Worksheets(2).Range("U1:AI47").Select
    Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture
    Set objWord = CreateObject("Word.Application")
    Set objDoc = objWord.Documents.Add
    objWord.Visible = True
    objWord.Selection.Paste
    objWord.Selection.TypeParagraph
    ActiveWindow.View = xlPageBreakPreview
End Sub

Sub imagem3()
    Dim objWord, objDoc As Object
    Worksheets(4).Activate
    ActiveWindow.View = xlNormalView
    Worksheets(4).Range("A1:Q47").Select
    Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture
    Set objWord = CreateObject("Word.Application")
    Set objDoc = objWord.Documents.Add
    objWord.Visible = True
    objWord.Selection.Paste
    objWord.Selection.TypeParagraph
    ActiveWindow.View = xlPageBreakPreview
End Sub

The problem i´m having is that it will create three different word files with one image each. I would like to know how can i code it so it will paste the 3 images to the same word file.

I would also like to make it not creating a new word file every time i used the macro but instead copy the 3 images to an already opened word file where the cursor is.

Thanks a lot for the help.

1 Answer 1

1

Tested (changed the ranges for my testing):

Sub imagem1()

    Dim objWord As Object, objDoc As Object, Rng As Object
    Dim wb As Workbook

    Set wb = ActiveWorkbook

    'see if Word is already open
    On Error Resume Next
    Set objWord = GetObject(, "Word.Application")
    On Error GoTo 0

    'not open - create a new instance and add a document
    If objWord Is Nothing Then
        Set objWord = CreateObject("Word.Application")
        objWord.Visible = True
        objWord.documents.Add
    End If

    Set objDoc = objWord.activedocument
    Set Rng = objWord.Selection

    wb.Windows(1).View = xlNormalView

    wb.Worksheets(1).Range("A1:C5").CopyPicture Appearance:=xlScreen, Format:=xlPicture
    Rng.Paste
    Rng.typeparagraph

    wb.Worksheets(1).Range("A7:C12").CopyPicture Appearance:=xlScreen, Format:=xlPicture
    Rng.Paste
    Rng.typeparagraph

    wb.Worksheets(1).Range("A14:C19").CopyPicture Appearance:=xlScreen, Format:=xlPicture
    Rng.Paste
    Rng.typeparagraph

End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.