-
Sep 24th, 2015, 03:55 AM
#1
Thread Starter
Addicted Member
converting .msg files with french characters into .pdf, not converting
Hi,I'm working on an application that converts .msg files to .pdf.
It works perfect when the .msg file name is of English alphabets.
When I try to use the .msg files with french character naming, i have some issue as follows.
The .msg file is well read and written in txt file. then a folder is created with the .msg file name.
then i'm creating a html file with the same name of the .msg file and opening it with word doc, to convert to pdf.
I get an error at this place, saying "not able to find the file". but i see the .html file created and i could open it locally.
Not able to open from word to save as pdf in vb.net.
here is my code:
Code:
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.GetEncoding("iso-8859-1"))
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)
Dim extension = System.IO.Path.GetExtension(getname)
Dim result = getname.Substring(0, getname.Length - extension.Length)
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 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
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"
wdocAtt = wAppAtt.Documents.Open(name & ".html")
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)
'**************************eror at this code******************************
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)
' MessageBox.Show("File Moved")
End If
Loop
End If
TextBox1.AppendText("All done" & vbNewLine)
MessageBox.Show("Converted")
Button1.Enabled = False
Me.Close()
End Sub
Any help will be helpful.
Thanks in advance
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|