Saturday, 12 September 2015

Convert Word File To PDF and XPS format

Convert Word File To PDF and XPS format

Add the Following Code In the Code Window

Imports Microsoft.Office.Interop.Word

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim wordApp As Application = New Application()
        Dim wordDoc As Document = Nothing
        Dim strSource As String = TextBox1.Text

        Dim d As String = TextBox2.Text & "\" & TextBox3.Text & ".pdf"
        Try
            wordDoc = wordApp.Documents.Open(strSource)
            wordDoc.ExportAsFixedFormat(d, WdExportFormat.wdExportFormatPDF, False, WdExportOptimizeFor.wdExportOptimizeForOnScreen, WdExportRange.wdExportAllDocument)

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        GroupBox1.Visible = True
        Label4.Text = "Word Document " & TextBox3.Text & " is changed From docx to pdf format"
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim d As String
        Dim f As String
        OpenFileDialog1.Title = "Please select a MicroSoft Word file"
        OpenFileDialog1.InitialDirectory = "C:"
        OpenFileDialog1.Filter = "Word files (*.doc)|*.docx"
        If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            Dim wordFileName As String = OpenFileDialog1.FileName
            TextBox1.Text = wordFileName
        End If
        d = System.IO.Path.GetFileName(OpenFileDialog1.FileName)
        f = ".docx"
        TextBox3.Text = Replace(d, f, "")
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim wordApp As Application = New Application()
        Dim wordDoc As Document = Nothing
        Dim strSource As String = TextBox1.Text
        Dim d As String = TextBox2.Text & "\" & TextBox3.Text & ".xps"
        Try
            wordDoc = wordApp.Documents.Open(strSource)
            wordDoc.ExportAsFixedFormat(d, WdExportFormat.wdExportFormatXPS, False, WdExportOptimizeFor.wdExportOptimizeForOnScreen, WdExportRange.wdExportAllDocument)

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        GroupBox1.Visible = True
        Label4.Text = "Word Document " & TextBox3.Text & " is changed From docx to xps format"
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        FolderBrowserDialog1.ShowDialog()
        TextBox2.Text = FolderBrowserDialog1.SelectedPath
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        TextBox1.Clear()
        TextBox2.Clear()
        TextBox3.Clear()
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        GroupBox1.Visible = False
    End Sub
End Class

No comments:

Post a Comment