I am trying to open a workbook from powerpoint using VBA. Although the workbook object is getting set but the workbook is no where to be seen. [ See snap at the end for clarity ]
Code:
Public Path As String
Sub CheckThisSub()
Path = ActivePresentation.Path
Debug.Print (Path & "\" & "QC_Log.xlsm") 'prints D:\QC\Test\QC_Log.xlsm
Dim QCWbk As New Excel.Workbook
Set QCWbk = Excel.Workbooks.Open(Path & "\" & "QC_Log.xlsm")
'QCWbk is getting set but the excel QC_Log.xlsm is nowhere to be seen.
Debug.Print QCWbk.Name 'prints QC_Log.xlsm
QCWbk.Windows.Item(1).Visible = True
' Rest of code
End sub
Snap:
So, creating one Excel session can be done in different ways.
Dim QCWbk As Excel.Workbook
to Dim QCWbk As New Excel.Workbook
your code problem resides in the way you try setting Excel application. Your code tries to set a workbook, not the Excel application. A piece of working code starting from yours should be the next one:Dim QCWbk As New Excel.Workbook, wb as Excel.Workbook, Path As String 'Using New the Excel COM object is already instantiated:
Path = ActivePresentation.Path
Set wb = QCWbk.Workbooks.Open(Path & "\" & "QC_Log.xlsm") 'the workbook is set in this way!
QCWbk.Visible = True
Dim QCWbk As Object, wb as Object, Path As String '
Path = ActivePresentation.Path
Set QCWbk = CreateObject("Excel.Application")
QCWbk.Visible = True
Set wb = QCWbk.Workbooks.Open(Path & "\" & "QC_Log.xlsm") 'the workbook is set in this way!
Dim QCWbk As Excel.Workbook, wb as Excel.Workbook, Path As String 'Using New the Excel COM object is already instantiated:
Path = ActivePresentation.Path
Set QCWbk = CreateObject("Excel.Application")
QCWbk.Visible = True
Set wb = QCWbk.Workbooks.Open(Path & "\" & "QC_Log.xlsm") 'the workbook is set in this way!
Dim QCWbk Excel.Workbook, wb as Excel.Workbook, Path As String
Path = ActivePresentation.Path
On Error Resume Next
Set QCWbk = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Err.Clear: On Error GoTo 0
MsgBox "No Excel Open session..."
Exit Sub 'the code exists if only an open session is required...
End If
'Usually in such a case a new session is created (as above). Here is only the part showing how to use an existing session
Set wb = QCWbk.Workbooks.Open(Path & "\" & "QC_Log.xlsm") 'the workbook is set in this way!
QCWbk.Visible = True
Dim Ex As Excel.Application
Set Ex = GetObject("Book1").Application
Debug.Print Application.ActiveWindow.hwnd, Ex.ActiveWindow.hwnd