Exchanging variables between powerpoint and excel VBA - excel

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)

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

Check data in Excel using PowerPoint VBA

I have a PowerPoint presentation that is has data links to an Excel file,
I am attempting to create an endless PowerPoint loop that every slide change runs a macro. This macro needs to connect to the Excel file, and if cell B2 > 0 output an action that causes an animation on the screen.
I have attempted to make PowerPoint open the Excel file but I have not been able to get further than this.
Private Sub OnSlideShowPageChange()
Dim i As Integer
Dim xlApp As Excel.Application
Dim xlWorkBook As Object
Dim Test As Object
'i = ActivePresentation.SlideShowWindow.View.CurrentShowPosition
'If i <> i Then Exit Sub
Test = 0
Set xlApp = New Excel.Application
Set xlWorkBook = xlApp.Workbooks.Open("C:\Users\Michael\Documents\Wallboard Test Data.xlsx", True, False)
Set Test = xlWorkBook.sheets("Hall of Fame").Select.Range("B2").Value
MsgBox Test
End Sub
Siddharth Rout "Remove .value as Test is an object and then use Msgbox Test.Value . Now you can use Val(Test.Value)>0 for the condition"

Open Excel OLEObject in a new Excel instance

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

Resources