1

我遇到了一个非常奇怪的问题,我们公司的一台计算机无法打印 MS Access 报告并将其附加到电子邮件中。我做了很多研究,但其中大部分并不适用于我的案例,因为这个问题只发生在我们公司 20 多台 PC 中的一台 PC 上。这是我们得到的错误的打印屏幕: 在此处输入图像描述

我正在使用的代码如下:

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

我找到了一个我认为可以解决我的问题的线程,但不幸的是它没有: 无法转换 COM 对象 - Microsoft Outlook 和 C#

任何建议将不胜感激

4

2 回答 2

0

您已经硬编码了程序文件位置的路径,因此如果您在 32 位机器上运行它,它将找不到 PDFCreator.exe。

用于Environment.GetFolderPath在机器上查找路径,而不是对其进行硬编码。`

如果您将整个内容包装在一个Try Catch块中以查看您没想到的某个地方是否存在错误,这也可能会有所帮助

于 2013-04-04T11:29:49.433 回答
0

我已经设法解决了这个问题。可能不是最好的方法,但我们重新安装了 Outlook,这解决了问题。我们认为已安装的另一个程序(很可能是将 Outlook 联系人与手机同步的程序)损坏了某些东西。不管怎么说,还是要谢谢你 :)

于 2013-04-05T12:05:30.557 回答