VBA Copy and paste image. Image getting lost
-
Friday, November 30, 2012 4:01 PM
Hi folks,
I'm using the following to split a mail merged document into multiple documents.
The problem is that there are two images per document in the combined document and they aren't always getting pasted into the new document. I just get a blank square saying image can't be displayed at this time. It's not a display problem as the file size is distinctly larger in the ones where the image has pasted correctly and the images are definitely there in the combined document.
Same problem with Word 2007 and 2010.
This code is being run from Excel as it contains the source data for the merge and has to merge into several different word documents depending upon the data filter applied.
Any ideas?
Thanks
Gary
'open the combined doc
Set oDoc = oWord.Documents.Open(indoc, Visible:=False)
Letters = oDoc.Sections.Count
Counter = 1
While Counter < Letters
DocName = outputpath & accountnumber(Counter - 1) & "-" & doctype & " - " & filename & ".docx"
oDoc.Sections.First.Range.Cut
Set oNewDoc = oWord.Documents.Add
oNewDoc.Range.PasteAndFormat wdPasteDefault
' remove section break from end of doc
Set oRng = oNewDoc.Range
With oRng.Find
.Text = "^b"
While .Execute
oRng.Delete
Wend
End With
oNewDoc.SaveAs2 filename:=DocName, AddToRecentFiles:=False
oWord.ActiveWindow.Close
Counter = Counter + 1
Wend
oDoc.Close wdDoNotSaveChanges
All Replies
-
Saturday, December 01, 2012 7:43 AM
Try changing:
oNewDoc.Range.PasteAndFormat wdPasteDefault
to:
oNewDoc.Range.PasteAlso, if the images have been inserted with links to the files they came from, you may want to break those links, via oDoc.Fields.Unlink, immediately after 'Set oDoc = oWord.Documents.Open(indoc, Visible:=False)'.
Cheers
Paul Edstein
[MS MVP - Word]- Proposed As Answer by Quist ZhangMicrosoft Contingent Staff, Moderator Monday, December 03, 2012 5:49 AM
- Marked As Answer by Quist ZhangMicrosoft Contingent Staff, Moderator Friday, December 07, 2012 5:54 AM
-
Monday, December 03, 2012 10:20 AM
Thanks for the response.
Using Paste didn't make any difference.
The images aren't links.
It's weird because it's not consistent. One split document gets the image then the next one doesn't. It's like the image is getting lost from the clipboard.
-
Monday, December 03, 2012 11:33 AMHow are the images being inserted into the mailmerge files?
Cheers
Paul Edstein
[MS MVP - Word] -
Monday, December 03, 2012 1:48 PMVia the code (oDoc.Sections.First.Range.Cut then oNewDoc.Range.PasteAndFormat wdPasteDefault). It's literally copying the entire contents of a document section into the clipboard and pasting the whole thing into a new document.
- Edited by OffColour1972 Monday, December 03, 2012 1:50 PM
-
Monday, December 03, 2012 9:47 PMSorry, but that's not telling me how the images got into the mailmerge file - only how you're trying to transfer it from there to the final file. Ordinarily, if they're inserted by MAILMERGE fields (which need to be embedded in INCLUDEPICTURE fields), the resultant images remain linked to their source files.
Cheers
Paul Edstein
[MS MVP - Word]- Edited by macropodMVP Monday, December 03, 2012 9:51 PM
-
Wednesday, December 05, 2012 11:08 AMThe images are static in the mail merge file. They are not inserted as part of the merge.
-
Thursday, December 06, 2012 2:01 AM
In that case, it seems there's some instability in your system. You mentioned using both Word 2007 and Word 2010. If they're on the same system, I'd suggest repairing both (2007 first), via Programs & Features in the Windows Control Panel.Cheers
Paul Edstein
[MS MVP - Word]- Marked As Answer by Quist ZhangMicrosoft Contingent Staff, Moderator Friday, December 07, 2012 5:54 AM
-
Monday, December 10, 2012 11:47 AM
Thanks, but tried the same thing on three different machines (not with with 2007 and 2010 on the same machine). All have the same problem.
Giving up and have to find another approach.

