PDA

View Full Version : Mac Excel VBA - copy paste a picture from Excel to Word document



chooriang
08-29-2017, 07:57 PM
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

macropod
08-29-2017, 11:22 PM
In what sense doesn't it work? Did you try using just:
BmkRng.Paste

chooriang
08-31-2017, 12:41 AM
The bookmark is located in center of the line,
but the image is shifted to the left side of the page.

macropod
08-31-2017, 02:45 AM
Try:
BmkRng.PasteSpecial Link:=False, Placement:=wdInLine

chooriang
09-01-2017, 05:10 PM
Hi Macropod, it's still not working.

macropod
09-01-2017, 06:24 PM
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.

chooriang
09-01-2017, 08:17 PM
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?

macropod
09-02-2017, 05:06 AM
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.