Bring Excel file to front when running Outlook VBA code - vba

I wrote a macro in Outlook to set the value of a cell in Excel file which is opening.
Sub test()
Dim objExcel As Object, WB As Object, WS As Object
Set objExcel = GetObject(, "Excel.Application")
objExcel.Visible = True
Set WB = objExcel.Workbooks("Book1.xlsm")
WB.Activate
Set WS = WB.Worksheets("Sheet1")
AppActivate "Microsoft Outlook"
WS.Range("A1").Value = "hoho"
End Sub
It sets the value of the cell, but I still stand in Outlook.
How can I display the Excel file instead of Outlook?

The way to do that is to minimize the Application Window and then maximize it.
objExcel.WindowState = xlMinimized
objExcel.WindowState = xlMaximized

Related

Window not visible despite running in background

I have a macro that I'm trying to run through Access that will open up an excel sheet, do some actions on it, and then leave the sheet open.
I have most of it working, with the exception of not being able to get my excel document to open visibly. If I check the task manager, an excel process is running in the background so something does happen, just nothing that I can physically see.
I've attempted to sample some code found through stackoverflow and other resources, which I'm sure you'll see some of that in my current code. But I tried for about an hour with no avail.
Private Sub Command1_Click()
Dim fd As FileDialog
Dim MySheetPath As String
Dim Xl As Excel.Application
Dim XlBook As Excel.Workbook
Dim XlSheet As Excel.Worksheet
On Error GoTo ErrorHandler
'allowing selection of the time
Set fd = Application.FileDialog(msoFileDialogFilePicker)
fd.AllowMultiSelect = False
If fd.Show = True Then
If fd.SelectedItems(1) <> vbNullString Then
MySheetPath = fd.SelectedItems(1)
End If
Else
End
End If
Set Xl = CreateObject("Excel.Application")
Set XlBook = GetObject(MySheetPath)
ShowaWindow (MySheetPath)
Set XlSheet = XlBook.Worksheets(1)
XlSheet.Rows(2).EntireRow.Insert
XlSheet.Range("D2") = "ABC"
Set Xl = Nothing
Set XlBook = Nothing
Set XlSheet = Nothing
Exit Sub
ErrorHandler:
Set fd = Nothing
MsgBox "Error " & Err & ": " & Error(Err)
End Sub
Sub ShowaWindow(sFileName As String)
Dim oWb As Workbook
Set oWb = GetObject(sFileName)
For Each oWb In Workbooks
If LCase(oWb.Name) <> LCase(sFileName) Then
oWb.Windows(1).Visible = True
Exit For
End If
Next
End Sub
Ideally I would like to be able to see the worksheet appear.
Set Xl = CreateObject("Excel.Application")
Xl.Visible=True
You don't need to put it immediately after creating the object, just before you set the object to Nothing.
Set XlBook = GetObject(MySheetPath)
This is wrong. Don't use GetObject to open the workbook, use the Excel.Application instance you just created:
Set XlBook = Xl.Workbooks.Open(MySheetPath)
Later you iterate all opened workbooks:
For Each oWb In Workbooks
But that's not the Workbooks collection from the Xl application instance, it's the Workbooks collection from the instance that's currently running your code - you need to qualify it with the Xl object:
Private Sub ShowaWindow(ByVal app As Excel.Application, ByVal sFileName As String)
'...
For Each oWb In app.Workbooks
Also, make the app instance visible after you created it, and don't forget to invoke XlBook.Close and Xl.Quit to properly tear down that EXCEL.EXE process when you're done.

Convert VBA unmerge and fil to VBscript unmerge and fill

Hello I am trying to convert VBA to VBscript but having trouble as I do not know where to start. Any help is appreciated.
VBA: Un-merging cells and then filling data to those cells from original merged data cell.
Sub UnMergeFill()
Dim cell As Range, joinedCells As Range
For Each cell In ThisWorkbook.ActiveSheet.UsedRange
If cell.MergeCells Then
Set joinedCells = cell.MergeArea
cell.MergeCells = False
joinedCells.Value = cell.Value
End If
Next
End Sub
VBScript: I have the start of the code to open the correct excel file and save and close but not the modification the VBA script is doing.
'create the excel object
Set objExcel = CreateObject("Excel.Application")
'view the excel program and file, set to false to hide the whole process
objExcel.Visible = True
'open an excel file (make sure to change the location) .xls for 2003 or earlier
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\Desktop\vbsTest.xlsx")
'save the existing excel file
objWorkbook.Save
'close the workbook
objWorkbook.Close
'exit the excel program
objExcel.Quit
'release objects
Set objExcel = Nothing
Set objWorkbook = Nothing
VBscript Code:
'Microsoft Excel Automation
':: Open, unmerge cells and fill then save
'---------------------------------
'create the excel object
Set objExcel = CreateObject("Excel.Application")
'view the excel program and file, set to false to hide the whole process
objExcel.Visible = True
'open an excel file (make sure to change the location) .xls for 2003 or earlier
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\Desktop\Book3.xlsx")
Dim cell
Dim joinedCells
For Each cell In objWorkbook.ActiveSheet.UsedRange
If cell.MergeCells Then
Set joinedCells = cell.MergeArea
cell.MergeCells = False
joinedCells.Value = cell.Value
End If
Next
'save the existing excel file
objWorkbook.Save
'close the workbook
objWorkbook.Close
'exit the excel program
objExcel.Quit
'release objects
Set objExcel = Nothing
Set objWorkbook = Nothing

how to save macro code as vbs?

I don't know why I can't call my macro code from R, This is my code that I'm trying to save as vbs file: (should I save it in Notepad application?)
Sub vb()
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("path.xlsm", 0, False)
xlApp.Visible = True
xlApp.Run "Countries"
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
How can I save the above code as vbs ?
You can export a code module from the project explorer, right-click on it and select Export File....
You can also do it with VBA, i.e. to export "Module1":
With ThisWorkbook.VBProject.VBComponents("Module1")
.Export "c:\so\" & .Name & ".bas"
End With
Use the below VBS code as you cannot save VBA code as VBS as the schema or say they are not in the same page as C and C++ are different it is in the same way
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("path.xlsm")
objExcel.Application.Visible = True
objExcel.Application.Run "path.xlsm!Countries" 'Refer to the below if the code is under sheet
objExcel.ActiveWorkbook.Close
WScript.Echo "Finished."
WScript.Quit
If you have placed the code in the sheet. user this line
objExcel.Application.Run "path.xlsm!sheet1.dog"
Hope this resolve your query. Happy Coding.

Using Access 2010 VBA list all open Excel Workbooks

Ok, I must be missing something subtle here.
In Excel 2010 this VBA works:
Private Sub ExcelTest()
Dim wb As Workbook
For Each wb In Workbooks
Debug.Print wb.Name
Next wb
End Sub
I'm trying to replicate this in Access VBA and I've tried the following approaches in Access 2010 with no success.
Private Sub AccessTest1()
'Derived from http://stackoverflow.com/questions/15958061/controlling-excel-
'workbook-from-access-2010-vba
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Set xlApp = New Excel.Application
For Each xlWB In Excel.Workbooks
Debug.Print xlWB.Name
Next xlWB
'Nothing happens.
'Stepping through and hovering:
'xlApp = "MicroSoft Excel"
'xlWB = Nothing
'xlWB.Name = Object variable or With block variable not set.
'But doesn't throw an error.
End Sub
Private Sub AccessTest2()
'Derived from http://stackoverflow.com/questions/5729195/how-to-refer-to-excel-
'objects-in-access-vba
Dim xlApp As Excel.Application
Dim wb As Excel.Workbook
Set xlApp = New Excel.Application
For Each wb In Excel.Workbooks '<--- Like this nothing happens
Debug.Print wb.Name
Next wb
'Stepping through and hovering:
'xlApp = "MicroSoft Excel"
'wb = Nothing
'wb.Name = Object variable or With block variable not set. But doesn't throw an error.
For Each wb In xlApp.Workbooks '<--- This throws a "Object variable or
'With block variable not set" error
Debug.Print wb.Name
Next wb
End Sub
Private Sub AccessTest3()
'Derived from http://stackoverflow.com/questions/5729195/how-to-refer-to-excel-
'objects-in-access-vba
Dim objExcelApp As Object
Dim wb As Object
Set objExcelApp = CreateObject("Excel.Application")
Set wb = objExcelApp.Workbook '<--- Throws "Object doesn't support this property
'or method error"
'Hovering after error:
'objExcelApp = "MicroSoft Excel"
'wb = Nothing
For Each wb In objExcelApp.Workbooks
Debug.Print wb.Name
Next wb
End Sub
I definitely have the "Microsoft Excel 14.0 Object Library" reference checked in Access.
I'm just trying to list any open Excel Workbooks so that I can then act against that information.
Thanks for your help in advance.
Set xlApp = New Excel.Application creates a new instance of Excel - and of course you do not have any workbooks in there.
What you want to achive is to go thru an already opened Excel instance. Therefore you have to use getObject().
Another Error is in your For statement ... you have to use xlApp.Workbooks instead of Excel.Workbooks.
The following code should provide the exprected result:
Dim xlApp As Excel.Application
Set xlApp = GetObject(, "Excel.Application")
Dim xlWB As Excel.Workbook
For Each xlWB In xlApp.Workbooks
Debug.Print xlWB.Name
Next xlWB
Set xlApp = Nothing
Set xlWB = Nothing
Please keep also in mind to destry object variables on the end by setting them to Nothing.

How to open a powerpoint chart backend sheet in new instance

I am updating a powerpoint presentation using vba.
There are 30 charts in the ppt and I will be coping the data from some excel sheets to the backend excel sheets of the powerpoint charts. but each time I do it, I can see the new excel sheets getting opened in the taskbar. even though they don't display on the whole screen
Set CExcel = New Excel.Application
CExcel.Visible = False
Set CWB2 = CExcel.Workbooks.Open(PPPres.Slides(lngSldNo).Shape(strChartName).Chart.ChartData.Workbook)
This is the code I am using, but it is giving me error that you can not open a file like this
can you tell me how the backend sheets of powerpoint charts can be opened in a new instance and without displaying the file and its tab also
Thanks in advance
The 1st argument to the Workbooks.Open method is a string containing the file path, e.g.
Sub Test()
Dim EAPP As Excel.Application, EWB As Excel.Workbook
Set EAPP = New Excel.Application
Set EWB = EAPP.Workbooks.Open("C:\Users\Myself\Desktop\MyFile.xlsx")
Set EWB = Nothing
Set EAPP = Nothing
End Sub
Not sure that is delivered by your argument. The Application.Visible property by default is False, so no need to explicitely set it. And I guess you have created a reference to the Excel Object Library to make use of the Excel objects in Powerpoint for early binding.
Logic:
Get the shape which has the Chart object
Get your Chart object and then Get the Chartdata
Launch the chart and set your workbook and your Excel application objects
Assumptions:
For demonstration purpose I am assuming the following
Slide1 in the presentation has one shape which is a chart
I have added both scenarios. i.e Automating Excel from PowerPoint and Automating PowerPoint from Excel
Is this what you are trying?
Code: Automating Excel from PowerPoint
Option Explicit
Sub Sample()
'~~> Excel Objects
Dim oXlApp As Object, oXlWb As Object, oXlSheet As Object
'~~~> Powerpoint objects
Dim oPPChart As Chart
Dim oPPChartData As ChartData
'~~> Working with shape1
With ActivePresentation.Slides(1).Shapes(1)
If .HasChart Then
Set oPPChart = .Chart
Set oPPChartData = oPPChart.ChartData
oPPChartData.Activate
'~~> Set your Excel objects here
Set oXlWb = oPPChartData.Workbook
Set oXlApp = oXlWb.Parent
oXlApp.Visible = False
Debug.Print oXlApp.Name
Debug.Print oXlWb.Name
'
'~~> Rest of your code
'
End If
End With
'~~> Close And Cleanup
oXlWb.Close False
oXlApp.Quit
Set oXlSheet = Nothing
Set oXlWb = Nothing
Set oXlApp = Nothing
End Sub
CODE: Automating PowerPoint from Excel
Option Explicit
Sub Sample()
'~~> Excel Objects
Dim oXlApp As Application, oXlWb As Workbook, oXlSheet As Worksheet
'~~~> Powerpoint objects
Dim oPPApp As Object, oPPprsn As Object, oPPSlide As Object
Dim oPPChart As Object, oPPChartData As Object
Application.ScreenUpdating = False
'~~> Establish a Popwerpoint application object
On Error Resume Next
Set oPPApp = GetObject(, "PowerPoint.Application")
'~~> If not found then create new instance
If Err.Number <> 0 Then
Set oPPApp = CreateObject("PowerPoint.Application")
End If
Err.Clear
On Error GoTo 0
'~~> open relevant powerpoint file
Set oPPprsn = oPPApp.Presentations.Open("C:\Presentation1.pptx")
Set oPPSlide = oPPprsn.Slides(1)
'~~> Working with shape1
With oPPSlide.Shapes(1)
If .HasChart Then
Set oPPChart = .Chart
Set oPPChartData = oPPChart.ChartData
oPPChartData.Activate
'~~> Set your Excel objects here
Set oXlWb = oPPChartData.Workbook
Set oXlApp = oXlWb.Parent
'~~> This is required if powerpoint chartdata
'~~> opens in the same instance of this excel
oXlWb.Windows(1).Visible = False
'oXlApp.Visible = False
Debug.Print oXlApp.Name
Debug.Print oXlWb.Name
'
'~~> Rest of your code
'
End If
End With
oXlWb.Windows(1).Visible = True
'~~> Close And Cleanup
oXlWb.Close False
oPPprsn.Close
oPPApp.Quit
Application.ScreenUpdating = True
Set oPPChartData = Nothing
Set oPPChart = Nothing
Set oPPChartData = Nothing
Set oPPSlide = Nothing
Set oPPprsn = Nothing
Set oPPApp = Nothing
End Sub