-1

I am converting html to docx using Microsoft.Office.Interop.Word. Also html have img tag. Converted docx files shows imges properly in server but in other machines images does't come.

After investigating i found that images in docx is not embedded as it shows image path of server.

Any help on this would be of great use.

Code is as follows :

Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
                Microsoft.Office.Interop.Word.Document wordDoc = new Microsoft.Office.Interop.Word.Document();
                Object oMissing = System.Reflection.Missing.Value;
                //wordDoc = word.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                word.Visible = false;
                //Object encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8;
                Object openType = Microsoft.Office.Interop.Word.WdOpenFormat.wdOpenFormatWebPages;
                Object filepath = documentPath;
                word.Documents.Open(FileName: filepath, ReadOnly: false, Format: openType);
                Object confirmconversion = System.Reflection.Missing.Value;
                Object readOnly = false;
                string htmlFileNameWithExtension = Path.GetFileName(documentPath);
                string htmlFileNameWithoutExtension = Path.GetFileNameWithoutExtension(documentPath);

                Object saveto = documentPath.Replace(htmlFileNameWithExtension, htmlFileNameWithoutExtension);

                Object oallowsubstitution = System.Reflection.Missing.Value;


                wordDoc = word.Documents.Open(ref filepath, ref confirmconversion, ref readOnly, ref oMissing,
                                              ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                              ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                              ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                object fileFormat = WdSaveFormat.wdFormatDocumentDefault;


                wordDoc.SaveAs(ref saveto, ref fileFormat, ref oMissing, ref oMissing, ref oMissing,
                               ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                               ref oMissing, ref oMissing, ref oMissing, ref oallowsubstitution, ref oMissing,
                               ref oMissing);


                wordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                word.Quit(ref oMissing, ref oMissing, ref oMissing);
jai shukla
  • 211
  • 1
  • 2
  • 15

3 Answers3

1

Below is a simplified example of looping through to set images to save with the document (see SavePictureWithDocument).

for (var i = 0; i < wordDoc.InlineShapes.Count; i++) {
    if (wordDoc.InlineShapes[i].LinkFormat == null) {
        continue;
    }

    wordDoc.InlineShapes[i].LinkFormat.SavePictureWithDocument = true;
}
Fenton
  • 241,084
  • 71
  • 387
  • 401
1

So beautiful and logical

for (var i = !!! 1 ; i <= !!! wordDoc.InlineShapes.Count; i++) {
    if (wordDoc.InlineShapes[i].LinkFormat != null) {
        wordDoc.InlineShapes[i].LinkFormat.SavePictureWithDocument = true;
    }    
}

You get a null when you try to access the index i=0, because numbering is from 1

0

For Office 2016 for some reason I've received wordDoc.InlineShapes[i] = null, thus I've extend solution with additional check:

for (var i = 0; i < wordDoc.InlineShapes.Count; i++) {
    if (wordDoc.InlineShapes[i].LinkFormat == null) {
        continue;
    }

    wordDoc.InlineShapes[i].LinkFormat.SavePictureWithDocument = true;
}

or same with Lamdbda

document.InlineShapes.ToList().
                    Where(v => v != null && v.LinkFormat != null).ToList().
                    ForEach(v => v.LinkFormat.SavePictureWithDocument = true);
Aleksey Dr.
  • 532
  • 1
  • 4
  • 11