2013-07-27 161 views
2

我已經創建了下面的函數從按鈕打開.PDF在我的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 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 

然後它工作正常。所以我收集它與我的功能有關,但我無法弄清楚它是什麼。

回答

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

+1

謝謝。就是這樣,我不能相信我錯過了那些顯而易見的事情。 –