Word OLE VBNet - SDK sample


Related Articles Add comment Email article Print article

Word OLE VBNet sample is a simple Microsoft Windows console application that converts a Microsoft Word document (an example path for the document would be C:\Test.docx) to a PDF using Word OLE automation and the novaPDF SDK application.

Note: To be able to use the samples you must install novaPDF SDK as samples work only with it. Download it here: nova PDF SDK.

Source Code

Imports System
Imports System.Drawing
Imports System.Drawing.Printing
Imports System.Windows.Forms
' the novapiLib package must be added as a    COM    reference
Imports novapiLib 

Module Module1
'    <summary>
'    The    main entry point for the application.
'    </summary>
Const PRINTER_NAME As String = "novaPDF Pro v6"
Const PROFILE_NAME As String = "Word OLE VBNet"
Const PROFILE_IS_PUBLIC As Integer = 0
Const NOVAPDF_INFO_SUBJECT As String = "Document Subject"
Const NV_PROFILE_EXISTS As Long = -707133434 


Sub Main()
  Try
    ' create the NovaPdfOptions object
    Dim pNova As NovaPdfOptions
    pNova = New NovaPdfOptions
    ' initialize the NovaPdfOptions object
    ' if you have an application license for novaPDF SDK,
    ' pass both the registration name and the license key to the Initialize() function
    ' pNova.Initialize(PRINTER_NAME, "<registration name>", "<license key>");
    pNova.Initialize(PRINTER_NAME, "", "", "")
    ' get the active profile ...
    Dim activeProfile As String
     Dim nActivePublic As Integer
    pNova.GetActiveProfile(activeProfile, nActivePublic)
    Try
      ' and make    a copy of it
      pNova.CopyProfile(activeProfile, PROFILE_NAME, PROFILE_IS_PUBLIC)
    Catch e As System.Runtime.InteropServices.COMException
      ' ignore profile exists error
      If (NV_PROFILE_EXISTS = e.ErrorCode) Then
        System.Console.WriteLine("Profile already exists")
      Else
        ' more    serious    error, propagate it
        Throw e
      End If
    End Try
    ' set the copy    profile    as active profile ...
    pNova.SetActiveProfile(PROFILE_NAME, PROFILE_IS_PUBLIC)
    ' and set some    options
    pNova.SetOptionString(NOVAPDF_INFO_SUBJECT, "VB.Net Hello document", PROFILE_NAME, PROFILE_IS_PUBLIC)
    ' set nova default printer
    pNova.SetDefaultPrinter()
    ' print word document
    Dim objWord As Object
    Dim objDoc As Object
    pNova.InitializeOLEUsage("Word.Application")
    objWord = CreateObject("Word.Application")
    pNova.LicenseOLEServer()
    objDoc = objWord.Documents.Open("C:\Test.doc", False, True)
    objDoc.PrintOut(False)
    objDoc.Close(False)
    objWord.Quit(False)
    ' restore active profile
    pNova.SetActiveProfile(activeProfile, nActivePublic)
    pNova.DeleteProfile(PROFILE_NAME, PROFILE_IS_PUBLIC)
    ' restore default printer
    pNova.RestoreDefaultPrinter()
  Catch e As System.Runtime.InteropServices.COMException
    MessageBox.Show(e.Message)
  Catch e As Exception
    MessageBox.Show(e.Message)
  End Try
End Sub 

End Module

Related articles

Comments