Consulting

Results 1 to 8 of 8

Thread: Mac Excel VBA - copy paste a picture from Excel to Word document

  1. #1

    Mac Excel VBA - copy paste a picture from Excel to Word document

    It's mac OS.
    I'm trying to copy paste a picture from Excel to Word document.
    The picture should go to the specified bookmark location but unfortunately, it won't.
    The code works just fine on windows but not in Mac.
    I can't find a flaw on it.
    Perhaps somebody could help me to solve this.
    Thanks in advance.

    Here is the current code:

    Sub FindMeReplaceMe()
    
    Dim wdApp As Object
    Dim wdDoc As Object
    Dim wdRng As Object
    Dim BmkRng As Object 'Word.Range
    Dim myWkb As Workbook
    Dim myWks As Worksheet
    Dim myShp As Shape
    
    Set myWkb = ThisWorkbook
    Set myWks = myWkb.Sheets("Sheet1")
    
    
    On Error Resume Next
    Set wdApp = GetObject(, "Word.application")    'gives error 429 if Word is not open
    If Err = 429 Then
        Set wdApp = CreateObject("Word.application")    'creates a Word application
        Err.Clear
    End If
    
    wdApp.Visible = True
    Set wdDoc = wdApp.Documents.Open(strOutputFile)
    
    
    Set myShp = Nothing
    On Error Resume Next
        Set myShp = myWks.Shapes("HeaderImage")
    On Error GoTo 0
    If Not myShp Is Nothing Then
        With wdDoc
        If .Bookmarks.Exists("theHeaderImage") Then
            Set BmkRng = .Bookmarks("theHeaderImage").Range
                myWks.Shapes("HeaderImage").Copy
                BmkRng.PasteAndFormat (wdPasteDefault)
             DoEvents
            Else
            MsgBox "Bookmark of ""theHeaderImage"" is not found", vbExclamation
         End If
        End With
    Else
        MsgBox "Header image is not exist", vbExclamation
    End If
    
    Set wdApp = Nothing: Set wdDoc = Nothing: Set wdRng = Nothing
    
    End Sub

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    In what sense doesn't it work? Did you try using just:
    BmkRng.Paste
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    The bookmark is located in center of the line,
    but the image is shifted to the left side of the page.

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Try:
    BmkRng.PasteSpecial Link:=False, Placement:=wdInLine
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    Hi Macropod, it's still not working.

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Since the image is being pasted 'inline', that suggests either:
    • your bookmark isn't "located in center of the line";
    • the image is too large to fit on the same line as the bookmark; or
    • the paragraph has right-to-left formatting.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    No, the paragraph alignment is center.
    The image size is rather very small.

    Once the code paste the picture, the bookmark is missing, is that normal?

  8. #8
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    The bookmark wouldn't disappear on a PC; Macs might be different, but shouldn't be. To preserve the bookmark, you might use:
    BmkRng.Collapse 0 '0 = wdCollapseEnd
    BmkRng.Paste

    or:
    BmkRng.Collapse 0 'wdCollapseEnd
    BmkRng.PasteSpecial , False, 0 '0 = wdInLine

    Note: Your code seems to be using a mix of early binding and late binding. For example, 'Dim wdApp As Object' implies late binding, but 'BmkRng.PasteAndFormat (wdPasteDefault)' implies early binding. You should use one or the other, not a mix of both.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Tags for this Thread

Posting Permissions

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