VBA Copy and paste image. Image getting lost


  • 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?



    '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"
            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
            End With

            oNewDoc.SaveAs2 filename:=DocName, AddToRecentFiles:=False
            Counter = Counter + 1
        oDoc.Close wdDoNotSaveChanges

    Friday, November 30, 2012 4:01 PM


All replies

  • Try changing:
    oNewDoc.Range.PasteAndFormat wdPasteDefault

    Also, 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)'.

    Paul Edstein
    [MS MVP - Word]

    Saturday, December 01, 2012 7:43 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 10:20 AM
  • How are the images being inserted into the mailmerge files?

    Paul Edstein
    [MS MVP - Word]

    Monday, December 03, 2012 11:33 AM
  • Via 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.
    Monday, December 03, 2012 1:48 PM
  • Sorry, 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.

    Paul Edstein
    [MS MVP - Word]

    • Edited by macropodMVP Monday, December 03, 2012 9:51 PM
    Monday, December 03, 2012 9:47 PM
  • The images are static in the mail merge file. They are not inserted as part of the merge.
    Wednesday, December 05, 2012 11:08 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.

    Paul Edstein
    [MS MVP - Word]

    Thursday, December 06, 2012 2:01 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.

    Monday, December 10, 2012 11:47 AM