How to extract images from docx file using Microsoft.Office.Interop.Word

Vishal Gundeti 21 Reputation points
2021-03-24T15:49:35.84+00:00

I am trying to edit Docx in my application by using Microsoft.Office.Interop.Word but while parsing Docx to HTML, It is not extracting images from the header and footer. I want to extract images from the header and footer. Can anyone send me the sample code or reference links?

Developer technologies Visual Basic for Applications
0 comments No comments
{count} votes

Accepted answer
  1. Richard Michaels 126 Reputation points
    2021-03-27T12:43:07.423+00:00

    Headers and Footers are stored by Section within the Word document. And then there are 5 types of Headers and Footers that you should iterate through. Finally, you have to determine if the shape is an image, is embedded or linked, and does it have wrapping text (inline shape or floating shape range). Below is a sample code that will find all images.

    Sub GetHderFooterPics()
        Dim sec As Word.Section, HDFT As Word.HeaderFooter
        Dim i As Long
        For Each sec In ActiveDocument.Sections
            For Each HDFT In sec.Headers
                If HDFT.Range.InlineShapes.Count > 0 Then
                    For i = 1 To HDFT.Range.InlineShapes.Count
                        If HDFT.Range.InlineShapes(i).Type = wdInlineShapeLinkedPicture Or _
                           HDFT.Range.InlineShapes(i).Type = wdInlineShapePicture Then
                            'do something with the picture
                        End If
                    Next
                End If
                If HDFT.Range.ShapeRange.Count > 0 Then
                    For i = 1 To HDFT.Range.ShapeRange.Count
                        If HDFT.Range.ShapeRange(i).Type = msoLinkedPicture Or _
                           HDFT.Range.ShapeRange(i).Type = msoPicture Then
                            'do something with the picture
                        End If
                    Next
                End If
            Next
            For Each HDFT In sec.Footers
                If HDFT.Range.InlineShapes.Count > 0 Then
                    For i = 1 To HDFT.Range.InlineShapes.Count
                        If HDFT.Range.InlineShapes(i).Type = wdInlineShapeLinkedPicture Or _
                           HDFT.Range.InlineShapes(i).Type = wdInlineShapePicture Then
                            'do something with the picture
                        End If
                    Next
                End If
                If HDFT.Range.ShapeRange.Count > 0 Then
                    For i = 1 To HDFT.Range.ShapeRange.Count
                        If HDFT.Range.ShapeRange(i).Type = msoLinkedPicture Or _
                           HDFT.Range.ShapeRange(i).Type = msoPicture Then
                            'do something with the picture
                        End If
                    Next
                End If
            Next
        Next
    End Sub
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.