【问题标题】:Function to open .pdf file not opening file打开.pdf文件的功能不打开文件
【发布时间】:2013-07-27 15:19:56
【问题描述】:

我创建了以下函数来从我的 winform 上的按钮打开 .pdf 文件:

Function OpenReports(fileName As String) As String
  Dim xlWBPath As String

  Try
    xlWBPath = Globals.ThisWorkbook.Application.ActiveWorkbook.Path
    System.Diagnostics.Process.Start(xlWBPath & "\fileName")
  Catch ex As Exception
    MsgBox("The " & fileName & "is not found on directory")
  End Try
  Return ""
End Function

当我在这里调用函数时:

Private Sub btnRptEmployeePayToMarket_Click(sender As Object,
                          e As EventArgs) Handles btnRptEmployeePayToMarket.Click
  OpenReports("Ranges to Market.pdf")
End Sub

它进入错误陷阱。它找不到文件。但是,如果我不运行该函数,而是像这样将其作为 Private Sub 执行:

Private Sub btnRptEmployeePayToMarket_Click(sender As Object, e As EventArgs) Handles btnRptEmployeePayToMarket.Click
  Dim xlWBPath As String
  Try
    xlWBPath = Globals.ThisWorkbook.Application.ActiveWorkbook.Path
    System.Diagnostics.Process.Start(xlWBPath & "\Ranges to Market.pdf")
  Catch ex As Exception
    MsgBox("The file Ranges to Market.pdf is not found on directory")
  End Try
End Sub

然后它工作正常。所以我认为它与我的功能有关,但我无法弄清楚它是什么。

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    如果您的代码示例与您在程序中的完全相同,那么您的函数有错误,它应该如下所示:

    Try
    
        xlWBPath = Globals.ThisWorkbook.Application.ActiveWorkbook.Path
        System.Diagnostics.Process.Start(xlWBPath & "\" & fileName)
    
    Catch ex As Exception
    
        MsgBox("The " & fileName & "is not found on directory")
    
    End Try
    

    您使用的字符串 "\filename" 未在变量 "\" & filename 后附加反斜杠

    【讨论】:

    • 谢谢。就是这样,我不敢相信我错过了这么明显的东西。
    猜你喜欢
    • 2014-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多