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).
Related
Situation: I am currently stuck on understanding how I can prevent other workbooks to be opened in the same Excel instance as my personal workbook.
Reason/Attempts: The VBA code is found within my personal workbook, where I am creating a separate Excel instance xlApp and adding my workbook to that instance and removing it from the previous instance Application.
Now, whenever I open other files/workbooks, they will be assigned to Application, which is what I am looking for; however, if I close all the files I opened, except for my personal workbook, and attempt to open any file again, they will be opened in xlApp.
Goal: What the program should be doing is opening all other files in the Application instance, not the xlApp instance, regardless if Application has no workbooks open.
Is there a way I can prevent them from being opened in xlApp?
Public Sub createNewInstance()
Dim xlApp as Excel.Application
Set xlApp = New Excel.Application 'Creating new Excel instance.
xlApp.Workbooks.Add ThisWorkbook.FullName 'Adding my open workbook to that instance.
xlApp.Visible = True
Set xlApp = Nothing
ThisWorkbook.Saved = True
ThisWorkbook.Close SaveChanges:=False 'Closing the workbook of this current instance (not the new one).
End Sub
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
I have a macro in my Outlook that whenever I receive an e-mail with a certain subject, it automatically opens an Excel workbook and pastes a portion of the e-mail subject in a specific cell in one of the worksheets. It works perfectly. Now I need to do this exact same process but pasting this information in an already opened workbook, instead of opening a closed file.
I've tried different solutions from my limited Excel VBA knowledge (ActiveWorkbook, worbooks(filename).activate, etc.) but none of that worked and I have not found anything similar online, as most macros are written as being run from an Excel file and not Outlook.
This is part of our current code, that opens the file and pastes the e-mail subject (which is the "ticker" value) in a specific cell on the "Lista Empresas" worksheet. What I need is a code that does the same, but in an workbook that is already opened (let's call it "test1").
Dim exapp As Excel.Application
Dim ExWbk As Workbook
Dim ExWbk2 As Workbook
Set exapp = New Excel.Application
Set ExWbk2 = exapp.Workbooks.Open("\\XXX\ListaEmpresas_ajustado.xlsm", UpdateLinks:=0)
exapp.Visible = True
ExWbk2.Sheets("Lista Empresas").Range("P2").Value = ticker
ExWbk2.Sheets("Lista Empresas").Range("P3").Calculate
There are a few scenarios to handle here. First, is Excel running? If no, then do what you are doing already. If yes - is the correct workbook open? If yes - return it, otherwise open it.
Dim ExWbk2 As Workbook
Dim wb As Workbook
Dim exapp As Excel.Application
On Error Resume Next
Set exapp = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Set exapp = Nothing
End If
On Error GoTo 0
If exapp Is Nothing Then
' Excel is not running
Set exapp = New Excel.Application
Set ExWbk2 = exapp.Workbooks.Open("\\XXX\ListaEmpresas_ajustado.xlsm", UpdateLinks:=0)
ExWbk2.Visible = True
Else
' Excel is running, but is the right book open?
For Each wb In exapp.Workbooks
Debug.Print wb.Name ' <-- This will help you find what to look for
If wb.Name = "ListaEmpresas_ajustado" Then
' Yes, it is!
Set ExWbk2 = wb
End If
Next
If ExWbk2 Is Nothing Then
' No, it wasn't
Set ExWbk2 = exapp.Workbooks.Open("\\XXX\ListaEmpresas_ajustado.xlsm", UpdateLinks:=0)
End If
End If
The trick to find out if Excel is running is GetObject. It will fail if it can't find it.
The for loop is there to allow for finding the correct workbook, based on the name. Adjust as needed.
The following code gets the object if you know the name of the sheet currently active in Excel instance. I guess this could be got from the application title using the first bit of code.
Dim exapp As Excel.Application
Dim ExWbk As Workbook
Dim ExWbk2 As Workbook
Set exapp = GetObject("ListaEmpresas_ajustado.xlsm").Application
exapp.Visible = True
ExWbk2.Sheets("Lista Empresas").Range("P2").Value = ticker
ExWbk2.Sheets("Lista Empresas").Range("P3").Calculate
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
I'm attempting to copy the contents of a text box from one workbook to another. I have no problem copying cell values from the first workbook to the 2nd, but I get an object required error when I attempt to copy the text box. This macro is being run from the workbook containing the data I want copied. Using Excel 2007 Code:
Sub UploadData()
Dim xlo As New Excel.Application
Dim xlw As New Excel.Workbook
Set xlw = xlo.Workbooks.Open("c:\myworkbook.xlsx")
xlo.Worksheets(1).Cells(2, 1) = Range("d4").Value 'Copy cell content (this works fine)
xlo.Worksheets(1).Cells(2, 2) = TextBox1.Text 'This gives me the object required error
xlw.Save
xlw.Close
Set xlo = Nothing
Set xlw = Nothing
End Sub
Thanks for any help.
The problem with your macro is that once you have opened your destination Workbook (xlw in your code sample), it is set as the ActiveWorkbook object and you get an error because TextBox1 doesn't exist in that specific Workbook. To resolve this issue, you could define a reference object to your actual Workbook before opening the other one.
Sub UploadData()
Dim xlo As New Excel.Application
Dim xlw As New Excel.Workbook
Dim myWb as Excel.Workbook
Set myWb = ActiveWorkbook
Set xlw = xlo.Workbooks.Open("c:\myworkbook.xlsx")
xlo.Worksheets(1).Cells(2, 1) = myWb.ActiveSheet.Range("d4").Value
xlo.Worksheets(1).Cells(2, 2) = myWb.ActiveSheet.TextBox1.Text
xlw.Save
xlw.Close
Set xlo = Nothing
Set xlw = Nothing
End Sub
If you prefer, you could also use myWb.Activate to put back your main Workbook as active. It will also work if you do it with a Worksheet object. Using one or another mostly depends on what you want to do (if there are multiple sheets, etc.).
I think the reason that this is happening could be because TextBox1 is scoping to the VBA module and its associated sheet, while Range is scoping to the "Active Sheet".
EDIT
It looks like you may be able to use the GetObject function to pull the textbox from the workbook.
The issue is with this line
xlo.Worksheets(1).Cells(2, 2) = TextBox1.Text
You have the textbox defined at some other location which you are not using here. Excel is unable to find the textbox object in the current sheet while this textbox was defined in xlw.
Hence replace this with
xlo.Worksheets(1).Cells(2, 2) = worksheets("xlw").TextBox1.Text