2013-07-12 81 views
0

我想从按钮上的点击事件打开.pdf。我计算出的代码,但我在这条线得到一个错误:从excel命令按钮打开.pdf

xlsWB = CType(Globals.ThisWorkbook.Application.ActiveWorkbook, Excel.Workbook) 

错误说:

  System.InvalidCastException was unhandled by user code 
     HResult = -2147467262 
    Message=Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Tools.Excel.Workbook'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{B74CBB86-9C9F-4172-9AE7-3CE4A7BFA5EB}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)). 
    Source=Compensation Template 
StackTrace: 
     at ExcelWorkbook2.dsbWelcomePage.btnRptEmployeePayToMarket_Click(Object sender, EventArgs e) in F:\Test Environment\Compensation Template\Compensation Template\dsbWelcomePage.vb:line 104 
     at System.Windows.Forms.Control.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 
     at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 
     at System.Windows.Forms.Control.WndProc(Message& m) 
     at System.Windows.Forms.ButtonBase.WndProc(Message& m) 
     at System.Windows.Forms.Button.WndProc(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
     at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
InnerException 

似乎很吓人。这里是我的代码的其余部分:

Option Explicit On 
Option Strict On 

'Libraries to use 
Imports System.Drawing 
Imports System 
Imports System.IO 
Imports System.Drawing.Printing 
Imports System.Windows.Forms 
Imports Microsoft.Office.Tools 
Imports System.Windows 

Private Sub btnRptEmployeePayToMarket_Click(sender As Object, e As EventArgs) Handles btnRptEmployeePayToMarket.Click 

    Dim xlsWB As Excel.Workbook 
    Dim xlsWBPath As String 


    xlsWB = CType(Globals.ThisWorkbook.Application.ActiveWorkbook, Excel.Workbook) 
    xlsWBPath = xlsWB.Path() 

    xlsWB.FollowHyperlink(xlsWBPath & "\Employee Pay To Ranges.pdf") 

End Sub 

不知道我在做什么错。这是我第一次尝试从工作簿的路径打开文件。我对这个问题做了一些研究,但找不到任何帮助我的东西。

+0

这个DLL用作Excel地址吗??? –

回答

1

你总是可以只是做

System.Diagnostics.Process.Start(xlsWBPath & "\Employee Pay To Ranges.pdf") 

它也像你想投(如错误消息说)一个COM对象,在这种情况下,活动工作簿,到Microsoft.Office.Tools.Excel.Workbook。我认为你的Globals.ThisWorkbook.Application.ActiveWorkbook可能是Microsoft.Office.Interop.Excel.Workbook。您似乎也在使用Excel的文档级项目。所以试试这个:

Option Explicit On 
Option Strict On 

'Libraries to use 
Imports System.Drawing 
Imports System 
Imports System.IO 
Imports System.Drawing.Printing 
Imports System.Windows.Forms 
Imports Microsoft.Office.Tools 
Imports System.Windows 

Private Sub btnRptEmployeePayToMarket_Click(sender As Object, e As EventArgs) Handles btnRptEmployeePayToMarket.Click 

    Dim xlsWBPath As String = Globals.ThisWorkbook.Application.ActiveWorkbook.Path 

    System.Diagnostics.Process.Start(xlsWBPath & "\Employee Pay To Ranges.pdf") 

End Sub 
+0

就是这样,谢谢。 –

0

加入微软壳牌控制和自动化的引用,那么你可以使用像

 Dim myShell As New Shell 
    myShell.ShellExecute pathToPDF