Question

I am having a very strange problem where one of the computers in our company is unable to print an MS Access report and attach it to an email. I have done a lot of research however most of it doesn't apply to my case as this issue is happening only on one PC out of the 20+ we have in the company. This is a print screen of the error we are getting: enter image description here

The code I am using is the following:

If PrintMode = "Email" Then

            Dim mAcc As New Access.Application
            Dim DefaultPrinterName As New String("")
            Dim PDFPrinterName As New String("")

            Maintain__Loading.Setup()

            Try
                mAcc.CloseCurrentDatabase()
                mAcc.DoCmd.Close()
                System.Runtime.InteropServices.Marshal.ReleaseComObject(mAcc)
                mAcc = Nothing
            Catch ex As Exception
            End Try

            Dim startInfo As New ProcessStartInfo("C:\Program Files (x86)\PDFCreator\PDFCreator.exe") 'starts PDF Creator so it can save the report to PDF
            startInfo.WindowStyle = ProcessWindowStyle.Minimized
            Process.Start(startInfo)

            AttachmentName = "C:\PDFs\pdf.pdf" 'PDF has been set up to save all files as TestPrint.pdf
            PDFPrinterName = "PDFCreatorDistribution"
            '------ GETS DEFAULT PRINTER NAME -------
            Dim oPS As New System.Drawing.Printing.PrinterSettings
            Try
                DefaultPrinterName = oPS.PrinterName
            Catch ex As System.Exception
                DefaultPrinterName = ""
            Finally
                oPS = Nothing
            End Try

            'sets PDFCreatorDistribution as default printer
            Shell(String.Format("rundll32 printui.dll,PrintUIEntry /y /n ""{0}""", PDFPrinterName))

            Try
                If Not UCase(Trim(Database)) = "TEST" Then
                    mAcc.OpenCurrentDatabase("R:\Distribution\Access\Distribution-Reports.mde")
                Else
                    mAcc.OpenCurrentDatabase("R:\Distribution\Access\Test-Distribution-Reports.mde")
                End If
            Catch ex As Exception
                MsgBox(Err.Description)
            End Try

            If IO.File.Exists(AttachmentName) Then 'if file exists it deletes it
                IO.File.Delete(AttachmentName)
            End If

            Select Case Trim(Grid.Rows(Grid.CurrentRow.Index).Cells("Passing1Type").Value.ToString)
                Case "String"
                    mAcc.DoCmd.OpenReport(Trim(Grid.Rows(Grid.CurrentRow.Index).Cells("MacroName").Value.ToString), Access.AcView.acViewPreview, , Trim(Grid.Rows(Grid.CurrentRow.Index).Cells("Passing1").Value.ToString) & " = '" & Number & "'", Access.AcWindowMode.acWindowNormal)
                Case "Numeric"
                    mAcc.DoCmd.OpenReport(Trim(Grid.Rows(Grid.CurrentRow.Index).Cells("MacroName").Value.ToString), Access.AcView.acViewPreview, , Trim(Grid.Rows(Grid.CurrentRow.Index).Cells("Passing1").Value.ToString) & " = " & Number & "", Access.AcWindowMode.acWindowNormal)
            End Select

            mAcc.DoCmd.PrintOut()
            mAcc.Visible = True
            mAcc.CloseCurrentDatabase()
            mAcc.DoCmd.Close()
            System.Runtime.InteropServices.Marshal.ReleaseComObject(mAcc)
            mAcc = Nothing

            Do While Not System.IO.File.Exists("C:\PDFs\pdf.pdf")
                Threading.Thread.Sleep(2000) 'if doesn't exist, wait for 2 seconds

                If Not System.IO.File.Exists("C:\PDFs\pdf.pdf") Then 'if doesn't exist, wait for another 2 seconds
                    Threading.Thread.Sleep(2000)
                End If

                If Not System.IO.File.Exists("C:\PDFs\pdf.pdf") Then 'if doesn't exist, wait for another 2 seconds
                    Threading.Thread.Sleep(2000)
                End If

                If Not System.IO.File.Exists("C:\PDFs\pdf.pdf") Then 'shows error message
                    MsgBox("Error creating PDF. Please try again")
                    Exit Do
                End If
            Loop

            'sets default printer name
            Shell(String.Format("rundll32 printui.dll,PrintUIEntry /y /n ""{0}""", DefaultPrinterName))

            'saves file as
            Dim saveFileDialog As New SaveFileDialog()
            saveFileDialog.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*"
            If saveFileDialog.ShowDialog() = DialogResult.OK Then 'if OK clicked
                FileName = saveFileDialog.FileName 'get file name and move to new location/name
                FileNameOnly = System.IO.Path.GetFileName(FileName)
                Try

                    If IO.File.Exists(FileName) Then 'if file exists it deletes it before saving
                        IO.File.Delete(FileName)
                    End If

                    IO.File.Move(AttachmentName, FileName)
                Catch ex As Exception
                    MsgBox(Err.Description)
                Finally
                    Shell(String.Format("rundll32 printui.dll,PrintUIEntry /y /n ""{0}""", DefaultPrinterName))
                End Try
            Else 'user clicked cancel
                FileName = ""
            End If

            Maintain__Loading.Dispose()

            'GETS TO HERE AND AFTER THIS I GET THE ERROR MESSAGE DISPLAYED IN THE IMAGE ABOVE

            If IO.File.Exists(FileName) Then
                Try
                    Send_Email()
                Catch ex As Exception
                    MsgBox(Err.Description)
                End Try
            Else
                If Not FileName = "" Then MsgBox("An error has occured. Please try again")
            End If

            Shell(String.Format("rundll32 printui.dll,PrintUIEntry /y /n ""{0}""", DefaultPrinterName))

            PhysicallyPrintedOrEmailed = True
    End If 'Printmode = Email

I found a thread that I thought would solve my problem however unfortunately it didn't: Unable to cast COM object - Microsoft outlook & C#

Any advice will be greatly appreciated

Was it helpful?

Solution 2

I have managed to solve this. Probably not the best way but we reinstalled Outlook and this solved the issue. We believe another program (most likely one that syncs Outlook contacts with phone) that has been installed has corrupted something. Thanks anyway :)

OTHER TIPS

You have hardcoded the path to the program files location so if you are running this on a 32 bit machine it won't find the PDFCreator.exe.

Use Environment.GetFolderPath to find the path on the machine instead of hard coding it.`

Also it might help if you wrap the whole thing in a Try Catch block to see if there is an error somewhere you didn't expect

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top