I have added an Excel sheet into a PowerPoint 2010 document using the Insert tab --> Object --> Microsoft Excel 97-2003 Worksheet (Create New) option. I want to reference some of the cells in the Excel sheet in another slide of my PowerPoint. Is there a way to do this?
The purpose is I have a client who insists on a PPT report, except I need to use Excel to create the information required. Rather than constantly having two documents open and transferring the info from the Excel sheet to the PPT slide, I wanted to consolidate into one document, thus the Excel sheet added into the PPT file.
I'm not an expert at VBA by any means, but I know enough to muddle my way through if I need to use VBA to accomplish this.
I've uploaded pictures of an example (I hope). On slide 1, I have three cells filled in using the inserted Excel sheet. Slide 2 is where I need to reference those cells (text boxes with text in red). The information in those cells will change week from week and I need the text boxes in slide 2 to update with it. Any help would be appreciated.
Use the Selection Pane in PowerPoint to identify the embedded object's name, and reference it, then use the OLEFormat.Object to get a handle on the Workbook object, and from there you're just working with an instance of Excel.Workbook class, so all your familiar Excel properties & methods should be available:
Option Explicit
'Requires reference to Excel
Sub populate_text_box()
Dim obj As Object
Dim tb As Shape 'TextBox
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Set obj = ActivePresentation.Slides(1).Shapes("Object 3")
Set wb = obj.OLEFormat.Object
Set ws = wb.Sheets(1)
Set tb = ActivePresentation.Slides(2).Shapes("TextBox 1") 'Modify slide/shape name as needed
tb.TextFrame2.TextRange.Text = ws.Range("B2").Value
Set tb = ActivePresentation.Slides(2).SHapes("TextBox 2") 'Modify slide/shape name as needed
tb.TextFrame2.TextRange.Text = ws.Range("D2").Value
Set tb = ActivePresentation.Slides(2).SHapes("TextBox 3") 'Modify slide/shape name as needed
tb.TextFrame2.TextRange.Text = ws.Range("F2").Value
End Sub
Related
I have multiple slides with multiple charts having sample data. I want to update these charts with actual data which is present in excel files located in the same folder. Can I do this via VBA inside ppt and how?
I read this answer edit chart data in powerpoint but this does not specify where I can mention the excel file and select the specific data.
Please help!
On the question you linked to, the top answer provided the following code:
With ActivePresentation.Slides(sl).Shapes(sh).Chart.ChartData
.Activate
.Workbook.Sheets(1).Range("A1").Value = "test_data"
.Workbook.Close
End With
One of the comments on the answer pointed out that once you Activate the ChartData, you're basically dealing with Excel. You can also launch an actual instance of Excel inside the Powerpoint VBA, then just open your workbooks, extract the data from them and put it into your ChartData objects like you would do if you were working in VBA in excel.
In order to launch an instance of Excel in powerpoint VBA try the following:
Dim xlApp As Excel.Application
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
xlApp.Workbooks.Open "C:\lol\Book1.xlsx", True, False
'once Book1 is open you can extract data from Book1 and move it to
'the appropriate place in .ChartData.Workbook
Set xlApp = Nothing
(code above sourced from this answer)
Since my documents usually have certain phrases that can be used over and over again, I want to hotkey them all to save time. To be more specific, I plan to make, let say, Macro1 (insert "Of course"), Macro2 (insert "The issue is"), and Macro3 (insert "by the time"), hotkeyed to Alt +1,2,3 respectively. If I press Alt + 2, "The issue is" got inserted instantly at my cusor.
Here's the base macro I'm using:
Sub Macro1()
Selection.TypeText Text:="sample text"
End Sub
The problem is for any new document, the content of Macro1, 2, and 3 will often need to be changed. I plan on creating an excel file to house all the phrases for ease of edit, but don't know how to link its specific cells back to the word macros. Is there a way to do that?
Note: I'm aware of the AutoText or Building Block function in word, but I don't find them as easy to edit as an excel table.
Here is an example, assuming yor Excel file is located in "C:\temp\" and yor phrases are saved in first Worksheet and First column:
This Function will read the first cell (A1) from this excel file:
Function Read_Excel_Cell(cellRin As Long) As String
Dim oExcel As Excel.Application
Dim myWB As Excel.Workbook
Set oExcel = New Excel.Application
Set myWB = oExcel.Workbooks.Open("C:\temp\phrases.xlsx")
Read_Excel_Cell = myWB.Sheets(1).Cells(cellRin, 1)
myWB.Close
Set myWB = Nothing
Set oExcel = Nothing
End Function
And this Macro will insert it into document:
Sub InsertText1()
Selection.TypeText Text:=Read_Excel_Cell(1)
End Sub
So you can create in this way as many macros as you need and using the same function Read_Excel_Cell() you can refere to another row.
To make this running in the Word vba you must add Excel Reference (in Tools\References tick Microsoft Excel Object Library).
I have a PowerPoint presentation with several Excel charts pasted as links. Using VBA, I have checked, and the type of these shapes is msoLinkedOLEObject.
Knowing this, I would like to first open the Excel file that contains the original chart through VBA and then use the "update link" command of the chart, like this:
I wish to open the workbooks first because using the "update link" command with an open Excel file is usually faster for me than just using the "update link" command directly (maybe because some PowerPoint presentations and some workbooks are really extense).
For the sld.Shapes(i).LinkFormat part of the code, I can use .UpdateLinks to directly refresh the data, but I cannot find any way to directly open the Excel source file (in the same way I can by manually clicking the linked chart).
Could this be achieved?
Sub UpdateExcelLinkedCharts()
Dim pres As Presentation
Dim sld As Slide
Dim shp As Shape
Set pres = Application.ActivePresentation
'Loop through all active slides in the presentation
For Each sld In pres.Slides
If sld.SlideShowTransition.Hidden = msoFalse Then
'If the slide is a msoLinkedOLEObject, proceed to update its link
For i = 1 To sld.Shapes.Count
If sld.Shapes(i).Type = 10 Then
sld.Shapes(i).LinkFormat.UpdateLinks
End If
Next i
End If
Next sld
MsgBox ("All Links Updated!")
End Sub
You can either use
sld.Shapes(i).OLEFormat.Activate
Or
sld.Shapes(i).OLEFormat.DoVerb
followed by sld.Shapes(i).LinkFormat.Update
Both will open the linked object so you can edit it. But notice that this is not enough to have a reference to an Excel Object that you can manipulate through VBA.
I have two sheets in "Property" Workbook that I am copying to "UQP" Workbook.
PropertyCoverage - Houses formula =VLOOKUP("PF",tPremium,2,0) among other text and simple SUM formula.
xmlProperty - Houses table tPremium (among other tables; all tables mapped to XML Source)
When I copy these two sheets (simultaneously) into the UQP workbook the tPremium reference in the formula in the PropertyCoverage sheet automatically changes to tAutoForms, which is another table in the UQP workbook (that has nothing to do with the formula). UQP workbook has several other sheets and tables.
I don't know why this changes automatically, but I think it has something to do with the fact that Excel cannot copy multiples sheets when at least one sheet contains a table.
I have tried the solution below (referenced from here). However, the same issue happens. The issue does not happen if I copy to a new workbook. ... Bear with ActiveWorkbook, etc, I only used this as test to see if it would work
Sub Copy_Worksheets()
Dim wbMaster As Workbook
Set wbMaster = Workbooks("UniversalQuoteProposal.xlsb")
Dim TheActiveWindow As Window
Dim TempWindow As Window
With ActiveWorkbook
Set TheActiveWindow = ActiveWindow
Set TempWindow = .NewWindow
.Sheets(Array("PropertyCoverage", "xmlProperty")).Copy Before:=wbMaster.Worksheets(1)
End With
TempWindow.Close
End Sub
Here is my original copy code:
Sub CopySheetsToMaster()
Dim wbMaster As Workbook
Set wbMaster = Workbooks("UniversalQuoteProposal.xlsb")
Dim sSheetName As String
sSheetName = Replace(ThisWorkbook.Name, ".xlsb", "")
Dim sSheets As Variant
sSheets = Array(sSheetName & "Coverage", "xml" & sSheetName)
'*** => This is where tPremium gets automatically changed to tAutoForms
ThisWorkbook.Worksheets(sSheets).Copy Before:=wbMaster.Worksheets(1)
wbMaster.ChangeLink ThisWorkbook.FullName, wbMaster.FullName, xlLinkTypeExcelLinks
End Sub
Lastly, I can work around this by doing a Replace after the sheets are copied, but since this will scale out to a need to bring a possible 7-10 other workbooks with potential formula references to tables and such, I would rather find a cleaner solution if it exists.
Lastly, I am using Excel 2010.
What I have:
An Excel file where in a column (actually it is free formatted but aligned to be within a column) some elements are embedded bmp pictures that show the formula =EMBED("Paint.Picture","") when you click on them. When you look at the Excel sheet, only the icon representing the picture is displayed, not the picture itself.
What I want:
The embedded picture (not the icon) copied to a new Word document.
The Code I have thus far:
'Image Objects
Dim myObjs As Shapes
Dim myObj As Shape
Set myObjs = ActiveSheet.Shapes
'Traversing objects
Dim row As Integer
Dim myRange As Range
Dim myRange2 As Range
Dim isAddressMatch As Boolean
'Word Document Objects
Dim wordApp As New Word.Application
Dim myWord As Word.Document
'Prepare word for output
Set myWord = wordApp.Documents.Add
wordApp.Visible = True
'Initalize traversing objectts
Set myRange = Sheets("myWorksheet").Range("Q5")
Set myRange2 = Sheets("myWorksheet").Range("E5")
row = 0
'Loop through range values in the desired column
While (myRange2.Offset(row).Value <> "")
'Loop through all shape objects until address match is found.
For Each myObj In myObjs
On Error Resume Next
isAddressMatch = (myObj.TopLeftCell.Address = myRange.Offset(row).Address)
If Err.Number <> 0 Then
isAddressMatch = False
On Error GoTo 0
End If
'When match is found copy the bmp picture from Excel to Word
If (isAddressMatch) Then
myObj.Select
''''''''This copies the excel default picture,'''''''''''''''
''''''''not the picture that is embeded.'''''''''''''''''''''
myObj.CopyPicture 'What is the correct way to copy myObj
myWord.Range.Paste
'Rest of the code not yet implement
End If
Next
row = row + 1
Wend
What happens when I run my code:
My code goes through all "shapes" that are within the bounds of the column and copies that objects picture. However, when I paste it into word, it literally made a copy of the link image (icon), and not the underlying embedded image.
What I've found thus far:
This code which shows me how to create an embedded object, but not how to copy one.
Update: Simpler solution
As specified in the comments by jspek, the image can actually be copied by using the Copy method of the OLEObject, e.g.:
Dim obj As OLEObject
Set obj = ActiveSheet.OLEObjects(myObj.Name)
'Copy the OLE object representing a picture.
obj.Copy
'Paste the picture in Word.
myWord.Range.Paste
Old solution
I've found a suboptimal solution that involves both the clipboard and SendKeys - inspired by this link. I'm quite convinced that you can do this more elegantly by exploring ways to extract the OLEObject's properties. Extracting these is beyond the scope of my expertise at this time of writing :-)
It revolves around OLEObject. This code executes the OLE object's host application (which is Paint in this case) of your picture, sends keys to copy the picture and finally, pastes it into Word.
'Get the OLE object matching the shape name.
Dim obj As OLEObject
Set obj = ActiveSheet.OLEObjects(myObj.Name)
'Activate the OLE host application.
obj.Activate
'Send CTRL+A to select the picture in Paint and CTRL+C to copy it.
Application.SendKeys "^a"
Application.SendKeys "^c"
'Paste the picture in Word.
myWord.Range.Paste
I am not a coder, but I found that if you "Define Name" for a cell range, you can do all kinds of things with the defined names. For example:
Linking Excel Workbook rows to a Word document
1. Open your Excel work book go to Formulas -> Define NAME
2. Create a "NAME" for each of the cells or groups of cells that you would like to link.
For example, I hyper-linked a Question # in a Word document to my Excel document that is used for importing questions into our Learning Management System. Example NAME = Question_22 and refers to cell range =WBT16DS058!$A$90 (=worksheet!cellrange)
3. Save & close Excel workbook.
4. Open the Word document and create your text (Question 022) , highlight and insert a hyperlink.
5. Browse & Select your Excel document, append the end of the address to include #NAME. (i.e. - R312Test.xlsx#Question_22).
6. Select the new link, and your Excel document will open to the cell range.
Because you are defining a NAME for the range of cells, the link will stay active even when the cells are moved around.
I am wondering if you used "Define Name" for your cell range that includes the picture you are trying to embed, you will have luck.
My apologies if you have already defined the cell range's name and tried this.