Jose M.
Jose M.

Reputation: 2350

Function to open .pdf file not opening file

I have created the following function to open .pdf files from a button on my winform:

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

When I call the function here:

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

It goes into the error trap. It cannot find the file. However, if instead of running the function I do it as Private Sub like this:

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

then it works fine. So I gather it has something to do with my function, but I cannot figure out what it is.

Upvotes: 2

Views: 545

Answers (1)

Mark Hall
Mark Hall

Reputation: 54562

If your code example is exactly how you have it in your program then your function has an error, it should look like:

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

You were using a string "\filename" not appending a backslash to your variable "\" & filename

Upvotes: 3

Related Questions