question

VishalGundeti-3511 avatar image
0 Votes"
VishalGundeti-3511 asked VishalGundeti-3511 commented

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

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?

office-vba-dev
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RichardMichaels-5985 avatar image
0 Votes"
RichardMichaels-5985 answered VishalGundeti-3511 commented

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


· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks, Richard

It is working as expected.

One more question I want to convert Doc to HTML with headers. I have tried but the headers are not coming in the HTML content.

Could you please help me with this?

Thanks for the Advance.

0 Votes 0 ·

Headers and Footers do not display while you are in Web (html) view. Headers and Footers only display while you are in a Print Layout view. This has nothing to do with the Headers and Footers being converted into an HTML encoding. When you convert a DOCX file format to an HTML format, the Header and Footer data is converted, it just does not display in Web view.

What are you trying to accomplish?

0 Votes 0 ·

I am trying to edit the word Docx and save it again in the same format. I have tried with interop and Aspose doc editor. By using Aspose I am getting a header and footer to edit but in the interop, I am not getting a header and footer. Please check the86043-aspose-imge.png86044-interrop-image.pngattached screenshot.



After saving the Aspose content it is saving as expected with headers and footer but in the interop, it not happened.

0 Votes 0 ·
aspose-imge.png (64.8 KiB)
interrop-image.png (40.7 KiB)