Open Excel OLEObject in a new Excel instance - excel

I just learned to open programmatically embedded OLEObjects by following VBA-Excel code:
mySheet.OLEObjects(myName).Verb xlVerbOpen
However, if "myName" corresponds to an Excel object, the Excel file is opened in the same Excel instance in which I am running the program. Since at that time there are a couple of forms opened, I would like this object to be opened in a new Excel instance (and not behind the forms, as it is now happening). Is this possible? How could I do it? Thanks a lot in advance.

You can create new Excel Instance in your code (in the below sample - xlApp) and refer to that instance in whatever you do next (however this will work only if OLEObject is created with link):
Dim xlApp As New Excel.Application, xlWb As Workbook
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True 'Make new instance visible (by default it's not visible)
Set xlWb = xlApp.Workbooks.Open(Filename) ' Filename is path to the linked workbook
If OLEObject is not linked the only way to open in new Excel instance is to open with the main excel file, which is not what you want:
xlApp.Workbooks.Open(workbookpath).Worksheets("mySheet").OLEObjects(myName).Verb xlVerbOpen

Following Richard's suggestion, see below the code I built to be able to open an embedded excel object through VBA in a new workbook within a new Excel instance:
Dim xlApp As excel.Application
Dim xlWb As Workbook
Dim mySheet As Worksheet
Dim myName as String
Set mySheet = Sheets("write sheet name")
Set myName = Sheets("write object name")
ThisWorkbook.Sheets(mySheet).OLEObjects(myName).Copy
Set xlApp = New excel.Application
xlApp.Visible = True
Set xlWb = xlApp.Workbooks.Add
xlWb.Sheets(1).Paste
xlWb.Sheets(1).OLEObjects(myName).Verb xlVerbOpen
Regards

Related

How to read/set Excel Textbox property from MS-Access VBA

I have a simple Textbox in Excel worksheet (.xlsx) where I can read the BackColor property in Excel VBA sub with:
Debug.Print TextBox1.BackColor
I'm trying to reference that same textbox from MS-Access using the Excel 16 Object Model, but it can't see the textbox under any of the Excel Worksheet objects I'm looking at
It errors out on the line marked with asterisks below with error message
Method or Data Member Not Found
Public Sub SetHexColor()
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Set xlApp = GetObject(, "Excel.Application")
Set xlBook = Workbooks.Open("C:\Users\.........\Documents\TextBox.xlsx")
Set xlSheet = xlBook.Worksheets(1)
**Debug.Print xlSheet.TextBox1.BackColor**
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
Is there another way to reference and preferably set properties of a Textbox control in Excel?
I'd prefer not to have call an Excel function to set the property if possible - or maybe that's the issue - it has to be an xlsm file?
Excel.Worksheet is a generic worksheet object - it only provides access to "out of the box" methods of the Worksheet object: if you've "subclassed" your worksheet by adding members such as TextBox1 then you can't access those added members via the generic Worksheet type.
You can either do this
Dim xlSheet As Object
or leave the declaration as-is, and use something like
Debug.Print xlSheet.OLEObjects("TextBox1").Object.BackColor
Note this is not specific to automating Excel from access VBA - the same would be true if working entirely within Excel.

Is there a way to keep an instance of Excel open, even after all workbooks have been closed?

Condition: Currently, I have two separate Excel instances - xlApp and xlApp2.
xlApp is the Excel instance in which my personal workbook was
opened.
xlApp2 is the new Excel instance created through the VBA code
in my workbook to house other workbooks.
Issue: If I close my personal workbook in xlApp, and open a new workbook, it will be added to xlApp, which is what I am looking for; however, if I close xlApp by pressing the "X" or end the task in Task Manager, then any new workbook I open will be added to xlApp2, which is not what I am looking for.
Goal: The program should be keeping the xlApp instance open or create a new instance if it is closed, so that new workbooks will be added to it rather than to xlApp2. Would this be possible in VBA?
Public Sub keepExcelInstance()
Dim xlApp as Excel.Application
Dim xlApp2 as Excel.Application
Set xlApp = GetObject(, "Excel.Application") 'Get the current instance of Excel.
Set xlApp2 = New Excel.Application 'Create a separate instance of Excel.
End Sub

Creating an Excel Workbook via PPT VBA

I am able to open .xlsx file by CreateObject("Excel.Application").Workbooks.Open("path")
Something like this is not allowing me to create a new Excel Workbook via a PowerPoint Macro.
Set ExcelFile = CreateObject("Excel.Application")
ExcelFile.Workbooks.Add
ExcelFile.ActiveWorkbook.SaveAs "path"
sample code, just checked on PP 2016:
(remember to close xlsApp, set obj to nothing etc.)
Public Sub StackOverflow()
Dim xlsApp As Object
Dim wkbWorking As Object
Set xlsApp = CreateObject("Excel.Application") 'basically it opens excel application
Set wkbWorking = xlsApp.Workbooks.Add 'it creates new workbook in just opened excel
xlsApp.Visible = True 'makes excel visible
wkbWorking.SaveAs "C:\Temp\PesentationExcel.xlsx"
wkbWorking.Close 'closes workbook
xlsApp.Quit 'closes excel application
'sets variables to nothing
Set wkbWorking = Nothing
Set xlsApp = Nothing
End Sub

Create a blank workbook which is not saved

I am trying to open a workbook in separate instance. Currently, this workbook is saved on the desktop. I would want to open a new workbook which is not saved or located anywhere on my system. Below is the current code which I have. Please advise.
Sub New_Excel()
'Create a Microsoft Excel instance via code
'using late binding. (No references required)
Dim xlApp As Object
Dim wbExcel As Object
'Create a new instance of Excel
Set xlApp = CreateObject("Excel.Application")
'Open workbook, or you may place here the
'complete name and path of the file you want
'to open upon the creation of the new instance
Set wbExcel = xlApp.Workbooks.Open("C:\Users\PRASHPRA\Desktop\Book.xls")
'Set the instance of Excel visible. (It's been hiding until now)
xlApp.Visible = True
'Release the workbook and application objects to free up memory
Set wbExcel = Nothing
Set xlApp = Nothing
End Sub
If you want to create a new blank workbook, stop trying to open an existing one. Just change the line
Set wbExcel = xlApp.Workbooks.Open("C:\Users\PRASHPRA\Desktop\Book.xls")
to
'Add a new, empty workbook
Set wbExcel = xlApp.Workbooks.Add
For more information, see Creating a New Workbook (the link is for Excel 2003, because it's the first one I found via Google, but it still applies, and if you want a more recent link you can probably find it the same way I did).

Exchanging variables between powerpoint and excel VBA

I have a powerpoint pres that grabs some data from an excel sheet when a button is pressed.
Set EXL = New Excel.Application
EXL.Visible = False
Dim XLApp As Excel.Application
Set XLApp = GetObject(, "Excel.Application")
This is how I set the new excel application.
What I'm wondering is how I can send over a variable from my powerpoint slide into the excel workbook? I have textfield in my powerpoint slide that I want the text to be used in a variable inside excel. Is this possible? If so, how?
And how do I, from a powerpoint module, call a Sub within the excel workbook to run?
(This is some simplified production code from an Access db, powerpoint might have a few minor differences)
What I'm wondering is how I can send over a variable from my powerpoint slide into the excel workbook?
Sub SetXLCellValue( _
FileStr As String, _
TabStr As String, _
Cell As String)
Dim XLApp As New Excel.Application
Dim ObjXL As Excel.Workbook
Set ObjXL = XLApp.Workbooks.Open(FileStr)
ObjXL.Worksheets(TabStr).Range(Cell).value = value
ObjXL.Save
ObjXL.Close True
End Sub
As for calling a Sub in your Excel application, you can use
XLApp.Run("MySub")
This also has the ability to pass parameters to the method (intellisense should show you the way)

Resources