Jose M.
Jose M.

Reputation: 2340

Open .pdf from excel command button

I want to open a .pdf from a click event on a button. I worked out the code, but I am getting an error on this line:

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

The error says:

            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

It seems pretty intimidating. Here is the rest of my code:

   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

Not really sure what I am doing wrong. This is the first time I have tried to open a file from the path of a workbook. I have done some research on the issue, but could not find anything to help me.

Upvotes: 0

Views: 817

Answers (2)

rwisch45
rwisch45

Reputation: 3702

You could always just do

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

It also looks like you are trying to cast (like the error message says) a COM object, in this case the active workbook, into a Microsoft.Office.Tools.Excel.Workbook. I think that your Globals.ThisWorkbook.Application.ActiveWorkbook is probably a Microsoft.Office.Interop.Excel.Workbook. You also appear to be working in a document level project for Excel. So try this:

   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

Upvotes: 1

Sorceri
Sorceri

Reputation: 8033

Add a reference to Microsoft Shell Controls and Automation then you can use something like

     Dim myShell As New Shell   
     myShell.ShellExecute pathToPDF    

Upvotes: 0

Related Questions