2

我创建了以下函数来从我的 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

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

4

1 回答 1

3

如果您的代码示例正是您在程序中的样子,那么您的函数有错误,它应该如下所示:

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

于 2013-07-27T15:25:19.993 回答