Results 1 to 25 of 25

Thread: Image files not converted to html

Threaded View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2009
    Posts
    235

    Image files not converted to html

    Hi all,

    I'm trying to convert a Outlook .msg file to pdf. For this, I 1st convert the .msg file to .html file and then export the .html file as .pdf file. It works perfect except for the images in the .msg file. All the images are shown with "x" mark in html.

    Here is my code: I get the folder with .msg files, write all the file names in a text file. Each line of the text file is read, checked for attachments with in the .msg file, save only the .msg atachments in .html and then to .pdf and also save the main .msg file as .html and .pdf.

    code:
    Code:
    Option Strict On
    Imports Microsoft.Office.Interop
    Imports System.IO
    Imports Microsoft.Office.Interop.Word
    Imports System.Windows.Forms.Application
    Imports System.Text
    Imports System.Drawing.Imaging
    
    
    Public Class Form1
       
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    
            Dim path As String = ""
            Dim folderBrowserDialog1 As FolderBrowserDialog
            folderBrowserDialog1 = New System.Windows.Forms.FolderBrowserDialog()
            Dim resultOK As DialogResult = folderBrowserDialog1.ShowDialog
            ' get the selected path value
            If resultOK = System.Windows.Forms.DialogResult.OK Then
                path = folderBrowserDialog1.SelectedPath
                path = path & "\"
            End If
    
    
            Dim di As New IO.DirectoryInfo(path)
            Dim diar1 As IO.FileInfo() = di.GetFiles("*.msg", IO.SearchOption.AllDirectories)
            Dim dra As IO.FileInfo
    
            'list the names of all files in the specified directory
            For Each dra In diar1
                ListBox1.Items.Add(dra.FullName)
            Next
    
            Dim FileNumber As Integer = FreeFile()
            FileOpen(FileNumber, path & "ListofExcelFiles.txt", OpenMode.Output)
            For Each Item As Object In ListBox1.Items
                PrintLine(FileNumber, Item.ToString)
            Next
            FileClose(FileNumber)
    
            Dim FILE_NAME As String = path & "ListofExcelFiles.txt"
    
            If System.IO.File.Exists(FILE_NAME) = True Then
                Dim objReader As New System.IO.StreamReader(FILE_NAME, System.Text.Encoding.UTF7)
                Dim TextLine As String = ""
                Do While objReader.Peek() <> -1
                    TextLine = objReader.ReadLine()
    
                    '***************************************************
                    Dim position As Integer = TextLine.LastIndexOf("\"c)
                    Dim getname As String
                    getname = TextLine.Substring(position + 1)
                    'MessageBox.Show(getname)
    
                    Dim extension = System.IO.Path.GetExtension(getname)
                    Dim result = getname.Substring(0, getname.Length - extension.Length)
                    'MessageBox.Show(result)
                    Dim newFolder As String = path & result & "\"
    
                    If IO.Directory.Exists(newFolder) Then
                        IO.Directory.Delete(newFolder, True)
                        IO.Directory.CreateDirectory(newFolder)
                    ElseIf Not IO.Directory.Exists(newFolder) Then
                        IO.Directory.CreateDirectory(newFolder)
                    End If
    
    
                    'Dim MSWordPageSetup As WdOrientation = WdOrientation.wdOrientLandscape
    
                    Dim MasterFileName As String = path & result
    
                    Dim MSWordExportFilePath As String = newFolder & result & ".pdf"
                    Dim MSWordExportFormat As WdExportFormat = WdExportFormat.wdExportFormatPDF
                    Dim MSWordOpenAfterExport As Boolean = False
                    Dim MSWordExportOptimizeFor As WdExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint
                    Dim MSWordExportRange As WdExportRange = WdExportRange.wdExportAllDocument
                    Dim MSWordStartPage As Int32 = 0
                    Dim MSWordEndPage As Int32 = 0
                    Dim MSWordExportItem As WdExportItem = WdExportItem.wdExportDocumentContent
                    Dim MSWordIncludeDocProps As Boolean = True
                    Dim MSWordKeepIRM As Boolean = True
                    Dim MSWordCreateBookmarks As WdExportCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks
                    Dim MSWordDocStructureTags As Boolean = True
                    Dim MSWordBitmapMissingFonts As Boolean = True
                    Dim MSWordUseISO19005_1 As Boolean = False
                    TextBox1.Clear()
                    Dim oApp As New Outlook.Application
                    Dim OMItem As Outlook.MailItem
                    TextBox1.AppendText("Reading the .msg file" & vbNewLine)
                    OMItem = CType(oApp.CreateItemFromTemplate(MasterFileName & ".msg"), Outlook.MailItem)
    
                 
                    Dim mailAttachments As Outlook.Attachments = OMItem.Attachments
                    Dim attachmentInfo As StringBuilder = New StringBuilder()
    
                    If Not IsNothing(mailAttachments) Then
                        For i As Integer = 1 To mailAttachments.Count
                            Dim currentAttachment As Outlook.Attachment = mailAttachments.Item(i)
                           
    
                            If Not IsNothing(currentAttachment) Then
                                attachmentInfo.AppendFormat("#{0}", i)
                                attachmentInfo.AppendLine()
                                attachmentInfo.AppendFormat("File Name: {0}", currentAttachment.FileName)
                                attachmentInfo.AppendLine()
                                attachmentInfo.AppendFormat("Diplay Name: {0}", currentAttachment.DisplayName)
                                attachmentInfo.AppendLine()
                                attachmentInfo.AppendFormat("Type: {0}", currentAttachment.Type)
                                attachmentInfo.AppendLine()
                                attachmentInfo.AppendLine()
                                Dim attachmentName As String = currentAttachment.FileName
    
    
                                Dim counter As Integer = 0
    
                                Dim newFileName As String = attachmentName
    
                                While File.Exists(newFolder & newFileName)
                                    counter = counter + 1
                                    Dim positionAtt As Integer = newFileName.LastIndexOf("\"c)
                                    Dim getAttname As String
                                    getAttname = newFileName.Substring(positionAtt + 1)
                                    Dim extensionAtt = System.IO.Path.GetExtension(getAttname)
    
                                    Dim newFileNwmeWithoutExt = getAttname.Substring(0, getAttname.Length - extensionAtt.Length)
                                    newFileName = String.Format("{0}({1})", newFileNwmeWithoutExt, counter.ToString())
                                    newFileName = newFileName & extensionAtt
                                End While
                                
                                If newFileName.Contains(".msg") Then
                                    currentAttachment.SaveAsFile(newFolder & newFileName)
                                    Dim oApp1 As New Outlook.Application
                                    Dim OMAItem As Outlook.MailItem
                                    '//TextBox1.AppendText("Reading the .msg file" & vbNewLine)
                                    Dim positionAtt As Integer = newFileName.LastIndexOf("\"c)
                                    Dim getAttname As String
                                    getAttname = newFileName.Substring(positionAtt + 1)
                                    Dim extensionAtt = System.IO.Path.GetExtension(getAttname)
    
                                    Dim currentAttachmentName = getAttname.Substring(0, getAttname.Length - extensionAtt.Length)
                                    OMAItem = CType(oApp.CreateItemFromTemplate(newFolder & newFileName), Outlook.MailItem)
                                    Dim name As String = newFolder & currentAttachmentName
                                    Dim SW1 As StreamWriter = New StreamWriter(name & ".html")
                                    SW1.Write(OMAItem.HTMLBody)
                                    SW1.Close()
                                    oApp1.Quit()
                                    OMAItem = Nothing
                                    oApp1 = Nothing
    
                                    Dim wAppAtt As New Word.Application
                                    Dim wdocAtt As New Word.Document
                                    Dim MSWordExportFilePathAtt As String = name & ".pdf"
                                    '//TextBox1.AppendText("Reading the HTML file" & vbNewLine)
                                    wdocAtt = wAppAtt.Documents.Open(name & ".html")
                                    '//TextBox1.AppendText("Saving the PDF file" & vbNewLine)
                                    wdocAtt.ExportAsFixedFormat(MSWordExportFilePathAtt, _
                                    MSWordExportFormat, MSWordOpenAfterExport, _
                                    MSWordExportOptimizeFor, MSWordExportRange, MSWordStartPage, _
                                    MSWordEndPage, MSWordExportItem, MSWordIncludeDocProps, _
                                    MSWordKeepIRM, MSWordCreateBookmarks, _
                                     MSWordDocStructureTags, MSWordBitmapMissingFonts, _
                                    MSWordUseISO19005_1)
                                    wdocAtt.Close()
                                    wAppAtt.Quit()
                                    wdocAtt = Nothing
                                    wAppAtt = Nothing
                                End If
                            End If
                        Next
                    End If
    
                    TextBox1.AppendText("Writing as HTML file" & vbNewLine)
                    Dim SW As StreamWriter = New StreamWriter(newFolder & result & ".html")
                    SW.Write(OMItem.HTMLBody)
                    SW.Close()
                    oApp.Quit()
                    OMItem = Nothing
                    oApp = Nothing
    
    
                    Dim wApp As New Word.Application
                    Dim wdoc As New Word.Document
                    TextBox1.AppendText("Reading the HTML file" & vbNewLine)
                    wdoc = wApp.Documents.Open(newFolder & result & ".html")
                    TextBox1.AppendText("Saving the PDF file" & vbNewLine)
                    wdoc.ExportAsFixedFormat(MSWordExportFilePath, _
                        MSWordExportFormat, MSWordOpenAfterExport, _
                     MSWordExportOptimizeFor, MSWordExportRange, MSWordStartPage, _
                     MSWordEndPage, MSWordExportItem, MSWordIncludeDocProps, _
                     MSWordKeepIRM, MSWordCreateBookmarks, _
                     MSWordDocStructureTags, MSWordBitmapMissingFonts, _
                     MSWordUseISO19005_1)
                    wdoc.Close()
                    wApp.Quit()
                    wdoc = Nothing
                    wApp = Nothing
    
                    If System.IO.File.Exists(TextLine) = True Then
                        Dim mail As String = newFolder & getname
                        System.IO.File.Copy(TextLine, mail)
                    End If
                Loop
               
            End If
         
    
            TextBox1.AppendText("All done" & vbNewLine)
            MessageBox.Show("Converted")
            Button1.Enabled = False
            Me.Close()
        End Sub
    
       End Class
    Thanks in advance.
    Last edited by vijay2482; Oct 1st, 2015 at 10:33 AM. Reason: (solved -Convertion of outlook email to pdf files)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width