Bob Eaton
11/30/2007 3:35:00 AM
"Akshit Shah" <akshit.shah@rediffmail.com> wrote in message
news:06e677e35607477380a4449b9bdea98d@ureader.com...
> I want to convert word document (with all formatings & images) to a PDF
> file.
If you have Office/Word 2007, you can install the PDF plug-in and save
directly from within Word.
Below is a snippet of VB.Net code that will do the conversion using the Word
(2007) PIAs.
Bob
+------------------cut here------------------+
Imports System.Windows.Forms
Imports Word = Microsoft.Office.Interop.Word
Imports System.Runtime.InteropServices ' for Marshal
Imports System.IO ' Path
Module ConvertDocToPdf
Sub Main()
Dim openFileDialog As New OpenFileDialog
openFileDialog.Multiselect = True
openFileDialog.Filter = "All Word Documents
(*.docx;*.docm;*.dotx;*.dotm;*.doc;*.dot;*.htm;*.html;*.rtf;*.mht;*.mhtml;*.xml)|*.docx;
*.docm; *.dotx; *.dotm; *.doc; *.dot; *.htm; *.html; *.rtf; *.mht; *.mhtml;
*.xml|All files (*.*)|*.*"
If (openFileDialog.ShowDialog() = DialogResult.OK) Then
Dim wdSaveFormat As Word.WdSaveFormat =
Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF
Dim wrdApp As New Word.Application
Try
Dim oDocFilename As Object
For Each oDocFilename In openFileDialog.FileNames
Dim wrdDoc As Word.Document = wrdApp.Documents.Open(oDocFilename)
Dim oToFilename As Object = String.Format("{0}\{1}",
Path.GetDirectoryName(oDocFilename), _
Path.GetFileNameWithoutExtension(oDocFilename) + ".pdf")
wrdDoc.SaveAs(oToFilename, wdSaveFormat)
wrdDoc.Close()
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
wrdApp.Quit()
Marshal.ReleaseComObject(wrdApp)
End Try
End If
End Sub
End Module