Write in an already opened Excel workbook from Powerpoint Slide - vba

I am trying to write in an already manually opened workbook using VBA from my PowerPoint presentation.
Using CreateObject and then Workbook.Open it opens a new instance of the file.
I'm trying to use GetObject as found in several examples over the web.
Here's my code :
Dim xlApp As Object
Dim xlWorkbook As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = Trueme
Set xlWorkbook = xlApp.Workbooks.Open(ActivePresentation.Path & "\Suivi individuel.xlsx", True, False)
xlWorkbook.Sheets(1).Range("C14").Value = "Hello"
SlideShowWindows(1).View.GotoSlide nextSlide
I've tried:
Dim xlApp As Object
Dim xlWorkbook As Object
Set xlWorkbook = GetObject(ActivePresentation.Path & "\Suivi individuel.xlsx", "Excel.Application")
xlWorkbook.Sheets(1).Range("C14").Value = "Hello"
SlideShowWindows(1).View.GotoSlide nextSlide
It says runtime error 432: File Name or Class Name not found during automation operation.

Try:
Dim xlApp As Object
Dim xlWorkbook As Object
Set xlWorkbook = GetObject(ActivePresentation.Path & "\Suivi individuel.xlsx")
If you just want to get an open instance of Excel then
Set xlApp = GetObject(,"Excel.Application")
Set xlWorkbook = xlApp.ActiveWorkbook
Wrap that in an error handler if there's a chance Excel might not be open
See: https://support.microsoft.com/en-us/kb/288902

Related

Copying Named range Graph into powerpoint

I am coding up a macro enable powerpoint presentation, the problem I am having is that I am trying to copy a named range (which is a graph) from an excel sheet, into a power point presentation.
Dim xlApp As Object
Dim xlWorkBook As Object
Dim path As String
Dim filename As String
Set xlApp = CreateObject("Excel.Application")
Set PPApp = GetObject(, "Powerpoint.Application")
Set PPPres = PPApp.ActivePresentation
path = "path"
filename = "name.xlsx"
Set xlWorkBook = xlApp.Workbooks.Open(path & filename)
Set positionsheet = xlWorkBook.Sheets("Graphs")
'problem is in the below line
positionsheet.Range("Graph1").CopyPicture Appearance:=xlScreen, Format:=xlPicture
Set osh = PPPres.Slides(1).Shapes.PasteSpecial(ppPasteEnhancedMetafile)(1)
'adjust size here...
With xlWorkBook
.Save
.Close
End With
Set xlApp = Nothing
Set xlWorkBook = Nothing
The error I receive is
runtime error 1004, object-defined error
I have tried:
positionsheet.Range("Graph1").Select
in front of the problem line...with no success.
Replace Range with Shapes, like this:
positionsheet.Shapes("Graph1")...
You must make sure that you've got the right name. It is not the same as the range, but it will show in the same box as the range names does.

PowerPoint VBA to Update Embedded Spreadsheets

I have a PowerPoint with 21 embedded spreadsheets that are used to populate charts in the PowerPoint. Each week data from two spreadsheets is copied into the embedded spreadsheets to update the charts with new data. I've started working on a macro to automate this process. I have the macro to open the spreadsheets with the new data, copy the data, then open the first embedded file. I am getting a run-time error'438' Object doesn't support this property or method on the paste function.
Note: I know linked spreadsheets would be desirable, but my customer wants the embedded excel files.
Sub UpdatedEmbeddedSpreadsheets()
Dim oData As ChartData
Dim oCht As Chart
Dim xlApp As Object
Dim xlWorkBook As Object
'Open Weekly File
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlWorkBook = xlApp.Workbooks.Open("C:\abc.xlsx", True, False)
xlWorkBook.sheets("Weekly ABC Chart").Range("B2:B5").Copy
Set xlApp = Nothing
Set xlWorkBook = Nothing
'Open Embedded Spreadsheet
ActivePresentation.Slides(1).Shapes(1).Select
Set oCht = ActiveWindow.Selection.ShapeRange(1).Chart
oCht.ChartData.Activate
Set oData = oCht.ChartData
'The line below has the run-time error '438'
Debug.Print oData.Workbook.sheets("123").End(x1toright).Offset(0,-1).Paste
oData.Workbook.Close
Set oData = Nothing
Set oCht = Nothing
End Sub
Second Attempt:
Sub UpdatedEmbeddedSpreadsheets()
Dim xlApp As Object
Dim xlWorkBook As Object
'Open Weekly File
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlWorkBook = xlApp.Workbooks.Open("C:\abc.xlsx", True, False)
xlWorkBook.sheets("Weekly AVA Chart").Range("B2:B5").Copy
Set xlApp = Nothing
Set xlWorkBook = Nothing
'Open Embedded Spreadsheet
ActivePresentation.Slides(1).Shapes(1).Select
ActiveWindow.Selection.ShapeRange(1).Chart.ChartData.Activate
Dim ChartData As Object
Set ChartData = CreateObject("Excel.Application")
ChartData.Visible = True
ChartData.Workbook.sheets("123").End(xltoright).Offset(0, -1).Paste
ChartData.Workbook.Close
End Sub

Creating Excel Files using VB2010

I have a form with 1 button to create an Excel file on my desktop.
I get error message:
NullReferenceException Was Unhandled
Object reference not set to an instance of an object
and it highlights the code:
WB = excelapp.workbooks.add
I did add the reference "Microsoft excel 14.0" and my full code is below:
imports excel = microsoft.office.interop.excel
dim excelapp as excel.application
dim WB as excel.workbook
sub button1()
WB = excelapp.workbooks.add
excelapp.visible=true
end sub
Missing New on your Excel instance for starters:
Dim xlApp As New Excel.Application
Dim xlWorkbook As Excel.Workbook = xlApp.Workbooks.Add()
Dim xlWorksheet As Excel.Worksheet = CType(xlWorkbook.Sheets("sheet1"), Excel.Worksheet)
xlWorksheet.Cells(1, 1) = "data in first cell"
xlWorksheet.SaveAs(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\" & "Test.xlsx")
xlWorkbook.Close()
xlApp.Quit()
xlApp = Nothing
xlWorkBook = Nothing
xlWorkSheet = Nothing
You should probably put this in Try/Catch/Finally block to catch errors in case you run into issues, but mostly because if the program doesn't continue properly finish that code block, an EXCEL.EXE will remain open in your task manager, as well as whatever Excel file it was accessing will be "in use by another program" when you try to access/modify/delete it.
just add one line, then it'll work
imports excel = microsoft.office.interop.excel
dim excelapp as excel.application
dim WB as excel.workbook
sub button1()
excelapp = new excel.application
WB = excelapp.workbooks.add
excelapp.visible=true
end sub

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.

Modifying a template based excel in VB.NET

I am tyring to run code like this
xlWorkSheet.Cells(rownumber, 1) = "Some Value"
the problem is when I open the excel document the cell value is not updated.
any help will be appreciated
I use the following code to open the file
Dim pathtofile As String = Server.MapPath("~/UserControls/Upload/MyUpload.xlsx")
Dim xlApp As Excel.Application =New Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlworkbooks As Excel.Workbooks
Dim xlWorksheets As Excel.Worksheets
Dim misValue As Object = System.Reflection.Missing.Value
Dim xlWorkSheet As Excel.Worksheet = Nothing
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlworkbooks = xlApp.Workbooks
xlWorkBook = xlworkbooks.Open(pathtofile )
xlWorkSheet = xlWorkBook.Sheets("Sheet1")
You need to save, close, and release the com objects to make sure excel doesn't remain running as a process.
Sub save(filename As String) ' Saves the sheet under a specfic name
xlWorkBook.SaveAs(filename)
End Sub
Sub closexl()
xlWorkBooks.Close()
xlApp.Quit()
End Sub
Sub cleanup() ' Releases all of the com objects to object not have excel running as a process after this method finishes
GC.Collect()
GC.WaitForPendingFinalizers()
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
xlApp = Nothing
End Sub