VBA-Excel: Add/Insert a Image/Picture in Word Document

VBA-Excel: Add/Insert a Image/Picture in Word Document

To Add or Insert Images or Pic­tures in Microsoft Word Doc­u­ment using Microsoft Excel, you need to fol­low the steps below:

  • Cre­ate the object of Microsoft Word
  • Using MS word object, Open the exist­ing word doc­u­ment by pro­vid­ing the com­plete path
  • Make the MS Word visible
  • Cre­ate a Selec­tion object with the help of WordObject.
  • Set  Shapes object using Inli­ne­Shapes
  • Insert image in the word doc­u­ments using AddPic­ture()

 

Cre­ate the object of Microsoft Word

Set obj­Word = CreateObject(“Word.Application”)

Using MS word object, Open the exist­ing word doc­u­ment by pro­vid­ing the com­plete path

Set obj­Doc = objWord.Documents.Open(“D:\OpenMe.docx”)

Make the MS Word Visible

objWord.Visible = True

Cre­ate a Selec­tion object with the help of WordObject.

Set objS­e­lec­tion = objWord.Selection

Set  Shapes object using InlineShapes

Set objShapes = objDoc.InlineShapes

Insert image in the word doc­u­ments using AddPicture()

objShapes.AddPicture (str­Com­pleteIm­agePath)

Com­plete Code:

Function FnImageInsert(strCompleteImagePath)

   Dim objWord

   Dim objDoc

   Dim objSelection

 Dim objShapes    

   Set objWord = CreateObject("Word.Application")

   Set objDoc = objWord.Documents.Open("D:\OpenMe.docx")

   objWord.Visible = True    

   Set objSelection = objWord.Selection

   objSelection.TypeText (vbCrLf & "One Picture will be inserted here....")

   Set objShapes = objDoc.InlineShapes

 objShapes.AddPicture (strCompleteImagePath)        

End Function

 Call FnImageInsert(“D:\After_Rain.jpg”)

Insert image in Word Document

Insert image in Word Document

 

Also Read About

Happy Macro­ing :)

Sumit Jain

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

%d bloggers like this: