Here is the code
Private Sub ConvertToPDFButton_Click(ByVal Ctrl As
Office.CommandBarButton, ByRef CancelDefault As Boolean) Handles
ConvertToPDFButton.Click
Dim RetValInteger
Dim szFilePath As String
Dim szPrinterName As String
Dim iWksheet As Integer
Dim iBlnkWksheetsCnt As Integer
Dim IsConvertionDoneBoolean As Boolean = False
IsButtonClickedBoolean = True
Try
If IsApplicationRegistredBoolean() = True Then
RegistryKeyClass =
Registry.LocalMachine.OpenSubKey(REG_HKLM_KEY)
ApplicationPathString =
RegistryKeyClass.GetValue(REG_APPLICATION_PATH).ToString
'PDFprinter.SetDefaultPrinter()
PDFprinter.DriverInit(PRINTER_DRIVER_NAME)
If ApplicationNameString.ToLower.Contains("word") Then
WordApp = CType(applicationObject,
Word.Application)
WordDoc = WordApp.ActiveDocument
'documentName is assigned the value of active
document name
SelectOfficeFileString = WordDoc.Name.ToString
If SelectOfficeFileString.Contains(".doc") = False
Then
SelectOfficeFileString = SelectOfficeFileString
& ".doc"
Else
SelectOfficeFileString = SelectOfficeFileString
End If
ConvertOfficetoPDF.ConvertWorkbookCheckBox.Visible
= False
End If
If ApplicationNameString.ToLower.Contains("excel") Then
ExcelApp = CType(applicationObject,
Excel.Application)
ExcelWorkBook = ExcelApp.ActiveWorkbook
'documentName is assigned the value of active
document name
SelectOfficeFileString =
ExcelWorkBook.Name.ToString
If SelectOfficeFileString.Contains(".xls") = False
Then
SelectOfficeFileString = SelectOfficeFileString
& ".xls"
Else
SelectOfficeFileString = SelectOfficeFileString
End If
If ExcelApp.ActiveWorkbook.Sheets.Count > 1 Then
ConvertOfficetoPDF.ConvertWorkbookCheckBox.Enabled = True
Else
ConvertOfficetoPDF.ConvertWorkbookCheckBox.Enabled = False
End If
End If
If ApplicationNameString.ToLower.Contains("powerpoint")
Then
PowerPointApplication = CType(applicationObject,
PowerPoint.Application)
'documentName is assigned the value of active
document name
SelectOfficeFileString =
PowerPointApplication.ActivePresentation.Name.ToString
If SelectOfficeFileString.Contains(".ppt") = False
Then
SelectOfficeFileString = SelectOfficeFileString
& ".ppt"
Else
SelectOfficeFileString = SelectOfficeFileString
End If
ConvertOfficetoPDF.ConvertWorkbookCheckBox.Visible
= False
End If
ConvertOfficetoPDF.ShowDialog()
ConvertOfficetoPDF.PDFSaveFileDialog.Title = "Save As
..pdf File"
ConvertOfficetoPDF.PDFSaveFileDialog.Filter = "PDF
file(*.pdf)|*.pdf"
ConvertOfficetoPDF.PDFSaveFileDialog.InitialDirectory =
MY_DOCUMENT_PATH & "\" & My.Resources.PRODUCT_NAME & "\" &
SAVEDDOCUMENTS_FOLDER & "\"
GetFileNameString = SelectOfficeFileString.Substring(0,
SelectOfficeFileString.LastIndexOf(".")).ToString()
If GetFileNameString.LastIndexOf(".") <> -1 Then
GetFileNameString = GetFileNameString.Substring(0,
GetFileNameString.LastIndexOf("."))
End If
ConvertOfficetoPDF.PDFSaveFileDialog.FileName =
GetFileNameString + ".pdf"
ConvertOfficetoPDF.PDFSaveFileDialog.AddExtension =
True
If ConvertOfficetoPDF.PDFSaveFileDialog.FileName.Length
= 0 Then
Exit Sub
End If
Call InitializePrinter()
If ApplicationNameString.ToLower.Contains("excel") Then
' If workbook has charts then dont check
If ConvertOfficetoPDF.CancelBoolean = True Then
Exit Sub
End If
If (ExcelApp.ActiveWorkbook.Charts.Count = 0) Then
iBlnkWksheetsCnt = 0
' If convert entire workbook is enabled
If
(ConvertOfficetoPDF.ConvertWorkbookCheckBox.Checked = True) Then
For iWksheet = 1 To
ExcelApp.ActiveWorkbook.Worksheets.Count
If
ExcelApp.ActiveWorkbook.Sheets(iWksheet).UsedRange.Address = "$A$1"
Then
If
ExcelApp.ActiveWorkbook.Sheets(iWksheet).Range("A1").Value = "" Then
iBlnkWksheetsCnt =
iBlnkWksheetsCnt + 1
End If
End If
Next
If iBlnkWksheetsCnt =
ExcelApp.ActiveWorkbook.Worksheets.Count Then
MessageBox.Show(My.Resources.BLANK_WORKBOOK,
My.Resources.APPLICATION_NAME, MessageBoxButtons.OK,
MessageBoxIcon.Information)
Exit Sub
End If
' If convert only active sheet
Else
If
ExcelApp.ActiveWorkbook.ActiveSheet.UsedRange.Address = "$A$1" Then
If
CStr(ExcelApp.ActiveWorkbook.ActiveSheet.Range("A1").Value) =
String.Empty Then
MessageBox.Show(My.Resources.BLANK_WORKBOOK,
My.Resources.APPLICATION_NAME, MessageBoxButtons.OK,
MessageBoxIcon.Information)
Exit Sub
End If
End If
End If
End If
End If
If ConvertOfficetoPDF.SavePDFRadioButton.Checked = True
Then
Call
SetCompressionLevel(ConvertOfficetoPDF.GetTrackBarValueInteger)
If ConvertOfficetoPDF.PDFSaveFileDialog.ShowDialog
= System.Windows.Forms.DialogResult.OK Then
FilePathString =
ConvertOfficetoPDF.PDFSaveFileDialog.FileName
Else
SetDefaultPrinterBack()
Exit Sub
End If
Call CreatePDFFileForSaving()
ElseIf ConvertOfficetoPDF.EmailPDFRadioButton.Checked =
True Then
PDFprinter.Resolution = 75
PDFprinter.JPegLevel = 1
If ConvertOfficetoPDF.PDFSaveFileDialog.ShowDialog
= System.Windows.Forms.DialogResult.OK Then
FilePathString =
ConvertOfficetoPDF.PDFSaveFileDialog.FileName
Else
Exit Sub
End If
Call CreatePDFFileForSaving()
'Call OutlookCall()
Dim RegistryObject As RegistryKey
Dim LaunchMailExePath As String
RegistryObject =
Registry.LocalMachine.OpenSubKey("SOFTWARE\KMT Software\OfficeReady
4.0\Pro\Options")
LaunchMailExePath =
RegistryObject.GetValue("ApplicationPath") + "\LaunchMail.exe"
Shell(LaunchMailExePath + " " + FilePathString)
End If
Else
MessageBox.Show(My.Resources.REGISTER_APPLICATION,
My.Resources.APPLICATION_NAME, MessageBoxButtons.OK)
End If
Catch ex As Exception
Finally
SetDefaultPrinterBack()
End Try
End Sub ' here exception is thrown.