【问题标题】:PC cannot print to Email using OutlookPC 无法使用 Outlook 打印到电子邮件
【发布时间】:2013-03-26 06:53:11
【问题描述】:

我遇到了一个非常奇怪的问题,我们公司的一台计算机无法打印 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

我发现了一个我认为可以解决我的问题的线程,但不幸的是它没有: Unable to cast COM object - Microsoft outlook & C#

任何建议将不胜感激

【问题讨论】:

    标签: vb.net com outlook office-automation


    【解决方案1】:

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

    使用Environment.GetFolderPath 查找机器上的路径而不是硬编码。`

    如果您将整个内容包装在 Try Catch 块中以查看您未预料到的某处是否存在错误,这可能会有所帮助

    【讨论】:

    • 正如您正确指出的那样,路径已被硬编码。但是,我们确保该路径存在于每台 PC 上,与操作系统版本无关,因此我们必须手动绕过 32 位 PC(其中大约 5 台)并确保 PDF Creator 位于该文件夹中。我同意您对Try Catch 的建议,但是该代码在所有PC 上都可以正常工作,只是由于某种原因该代码无法正常工作。有什么建议/想法吗?
    • 当你说“它不起作用”时——它实际上做了什么。它必须以某种方式出错?意外错误的问题在于它们是意外的。
    • @OopsieDaisie - 帖子中没有图片尝试编辑帖子以添加它
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2020-11-26
      • 2017-08-22
      • 2017-01-05
      • 1970-01-01
      • 2020-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-20
      相关资源
      最近更新 更多