The project illustrates how to insert picture into Picture Content control using Open XML


Some customers want to insert picture into Picture Content Control using openxml way and ask this question in MSDN forum, but there is no existing sample about use openxml way. So this sample is helpful for those customers.

Customer Evidence:

Building the Project

Open the project (OpenXmlInsertPictureInWord.vbproj) in the Visual Studio 2010 and build it.


VB Script
Module Module1 
    Sub Main(args AsString()) 
        TryDim appPath AsString = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) 
            Dim containingDocumentPath AsString = appPath & Convert.ToString("\Test1.docx") 
            Dim imagePath AsString = appPath & Convert.ToString("\Image1.jpg") 
            Dim doc As WordprocessingDocument = WordprocessingDocument.Open(containingDocumentPath, True) 
            Dim mainDocumentPart As MainDocumentPart = doc.MainDocumentPart 
            Dim cc As SdtBlock = mainDocumentPart.Document.Body.Descendants(Of SdtBlock)().FirstOrDefault( 
                    Dim p As SdtProperties = c.Elements(Of SdtProperties)().FirstOrDefault() 
                    If p IsNotNothingThenDim pict As SdtContentPicture = p.Elements(Of SdtContentPicture)().FirstOrDefault() 
                        Dim a As SdtAlias = p.Elements(Of SdtAlias)().FirstOrDefault() 
                        If pict IsNotNothingAndAlso a.Val = "MyPicture"ThenReturnTrueEndIfEndIfReturnFalseEndFunction) 
            Dim embedRef AsString = NothingIf cc IsNotNothingThenDim dr As Drawing = cc.Descendants(Of Drawing)().FirstOrDefault() 
                If dr IsNotNothingThenDim blip As Blip = dr.Descendants(Of Blip)().FirstOrDefault() 
                    If blip IsNotNothingThen 
                        embedRef = blip.Embed 
                    EndIfEndIfEndIfIf embedRef IsNotNothingThenDim idpp As IdPartPair = doc.MainDocumentPart.Parts.Where(Function(pa) pa.RelationshipId = embedRef).FirstOrDefault() 
                If idpp IsNotNothingThenDim ip As ImagePart = DirectCast(idpp.OpenXmlPart, ImagePart) 
                    Dim fileStream As FileStream = File.Open(imagePath, FileMode.Open) 
                    Console.WriteLine("Picture inserted into picture content control successfully") 
                EndIfEndIfCatch ex As Exception 


Running the Sample

  1. This sample inserts/replaces a picture into a picture content control in word document using OpenXML SDK.
  2. Copy “Test1.docx” and “Image1.jpg” from the project directory to the executable file location.
  3. Run the executable CSOpenXmlInsertPictureInWord.exe and this will insert the picture “Image1.jpg” into the picture content control.
  4. Open the document “Test1.docx” and verify the contents of picture content control.


Microsoft All-In-One Code Framework is a free, centralized code sample library driven by developers' real-world pains and needs. The goal is to provide customer-driven code samples for all Microsoft development technologies, and reduce developers' efforts in solving typical programming tasks. Our team listens to developers’ pains in the MSDN forums, social media and various DEV communities. We write code samples based on developers’ frequently asked programming tasks, and allow developers to download them with a short sample publishing cycle. Additionally, we offer a free code sample request service. It is a proactive way for our developer community to obtain code samples directly from Microsoft.