none
COMEXCEPTION was unhandled by a user RRS feed

  • Question

  • using System;
    using System.Reflection;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Word = Microsoft.Office.Interop.Word;
    using System.IO;

    namespace login.Common
    {
        public class Utilities
        {
            /// <summary>
            /// A static class to convert from HTML document to Word document
            /// </summary>
            public static class HTML2WordConverter
            {

               // private static Application word;
                //private static Document document;

                /// <summary>
                /// converts a HTML file to Word file
                /// </summary>
                /// <param name="htmlSrcFilePath">the path to the source HTML file</param>
                /// <param name="wordDestFilePath">the path of the destination word file</param>
                public static void Convert(string htmlSrcFilePath, string wordDestFilePath, bool embedImages)
                {
                    FileInfo SrcFile = new FileInfo(htmlSrcFilePath);
                    FileInfo DestFile = new FileInfo(wordDestFilePath);
                    if (SrcFile.Exists == false)
                    {
                        throw new Exception(htmlSrcFilePath + " doesn't exist.");
                    }

                    Word._Application word = new Word.Application();
                    Word._Document document = new Word.Document();
                    try
                    {
                        document = word.Documents.Add();
                        word.Visible = false;

                        document = word.Documents.Open(SrcFile.FullName);

                        document.Activate();

                        if (embedImages)
                        {
                            //embed inline images in the document
                            foreach (Word.InlineShape image in document.InlineShapes)
                            {
                                if (image.LinkFormat != null)
                                {
                                    try
                                    {
                                        image.LinkFormat.SavePictureWithDocument = true;
                                        image.LinkFormat.BreakLink();
                                    }
                                    catch (Exception ex) { throw ex; }
                                   // throw;
                                }
                            }
                        }

                        document.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdPrintView;

                        document.SaveAs(DestFile.FullName, Word.WdSaveFormat.wdFormatDocument);

                        document.Close(false);
                       word.Quit();
                        
                    }
                    catch (Exception ex)
                    {
                        
                        try
                        {
                            document.Close(false);
                            word.Quit();
                        }
                        catch (Exception ex2)
                        {
                            /* nothing */
                            throw ex2;
                        }
                        throw ex; //EXCEPTION
                     }
                    
                }
            }
        }
    }
    Tuesday, October 10, 2017 12:57 PM

Answers