John Mark
John Mark

Reputation: 73

Generate pdf file from dotx file in VB.NET

I can create a docx file from dotx file with vb.net. However, I need a pdf file from this dotx file template. I tried to create a pdf file from the MS Office Word by saving itself as pdf, I hope that it can be done also by coding in vb.net

Imports Word = Microsoft.Office.Interop.Word

Public Class frmWordTemplate
    Private wdApp As Word.Application
    Private wdDocs As Word.Documents
    Const sPath As String = "D:\"
    Private sFileName As String

    Private Sub btnCreate_Click(sender As Object, e As EventArgs) Handles btnCreate.Click

        sFileName = "export-file"

        wdApp = New Word.Application

        wdDocs = wdApp.Documents

        Dim wdDoc As Word.Document = wdDocs.Add(sPath & "template_sample.dotx")
        Dim wdBooks As Word.Bookmarks = wdDoc.Bookmarks

        wdBooks("bkClient_name").Range.Text = txtClient.Text.ToString
        wdBooks("bkDate").Range.Text = dtpDate.Text.ToString


        wdDoc.SaveAs2(sPath & sFileName & ".docx")

        ReleaseObject(wdBooks)
        wdDoc.Close(False)
        ReleaseObject(wdDoc)
        ReleaseObject(wdDocs)
        wdApp.Quit()

Upvotes: 0

Views: 304

Answers (1)

John Mark
John Mark

Reputation: 73

Adding a fileformat in saving

wdDoc.SaveAs2(sPath & sFileName & ".pdf", Word.WdSaveFormat.wdFormatPDF)

Jobs done!

Upvotes: 1

Related Questions