none
Alter Word text without losing images

    Question

  • I am trying to customize word attachments with VBA. I have a template word document that I open, alter the text and save. That works fine except the images at the top of the document are removed when I change the text. 

    I didn't originate the template document, it appears the images are just InlineShapes pasted at the top of the document.  

    This is essentially the first time I've worked with VBA with Word so the answer could be very simple. I don't understand why the images are disappearing. I thought maybe I could copy the images and then paste them back in after the text changes but  doing  variable = doc.InlineShapes(1) causes an error as does variable = doc.InlineShapes(1).copy(). 

    Any suggestions are appreciated. 


    Sub test_word_attach()
       filedump = "C:\out files\" 
    'where to put the word attachments that are generated.'
    
       
        fp = "C:\locationofwordtemplate\documentname.docx"
        Set myWord = CreateObject("Word.Application")
        Set doc = myWord.Documents.Open(fp, ReadOnly = False)
        
        bodytxt = doc.Content.Text
        
        new_body_text = Replace(bodytxt, "Sir or Madam", "MY NAME")
        doc.Content.Text = new_body_text 'this is where the InlineShapes disappear
    
        doc.SaveAs2 (filedump & "newfile" & " docx")
        doc.SaveAs2 filedump & "newfile" & ".pdf", 17  
    
    End Sub
         


    Friday, February 09, 2018 8:48 PM

All replies

  • Try something along the lines of:

    Sub test_word_attach()
    Dim filedump As String, fp As String
    Dim myWord As Object, doc As Object
    filedump = "C:\out files\"
    'where to put the word attachments that are generated.'
    fp = "C:\locationofwordtemplate\documentname.docx"
    Set myWord = CreateObject("Word.Application")
    Set doc = myWord.Documents.Open(fp, , False, False)
    With doc
      With .Range.Find
        .Text = "Sir or Madam"
        .Replacement.Text = "MY NAME"
        .Execute Replace:=wdReplaceAll
      End With
      .SaveAs2 filedump & "newfile" & " docx", 12, , , False
      .SaveAs2 filedump & "newfile" & ".pdf", 17, , , False
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, February 09, 2018 9:31 PM
  • Thanks Paul. I want to run this from VBA in Outlook. This works from Word but not Outlook. I didn't think it would make a difference but it does. Any idea how to make it work from Outlook or why it won't from Outlook?
    Friday, February 09, 2018 10:04 PM
  • The code works fine for me when run from Outlook - though I did have to change the file paths to work with my system.

    What the code still lacks, though, is closing the document and quitting Word.


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Saturday, February 10, 2018 1:00 AM
    Saturday, February 10, 2018 12:18 AM
  • It's probably because wdReplaceAll is undefined in Outlook and will be treated as 0 (i.e. as wdReplaceNone). If so, you can change it to 2 or use Tools->References in the Outlook VBE to make a reference to the Microsoft Word library (whichever version you have).

    Peter Jamieson

    Saturday, February 10, 2018 9:36 AM
  • Referencing the Word library made it work. Many thanks to both of you. 

    Wednesday, February 14, 2018 7:32 PM