0

This VBA code, titled "UpdateTextboxesFromExcel," is designed to interact with both Microsoft Excel and PowerPoint to read a specific cell value from an Excel workbook and update a textbox in each slide of an active PowerPoint presentation with that value. Break down of what the code is trying to do below:

1. Declare Variables: excelApp: Object variable for the Excel application. wb: Object variable for the Excel workbook. ws: Object variable for the Excel worksheet (sheet 1 in this case). excelFilePath: String variable containing the path to the Excel file. cellValue: Variant variable to store the value read from Excel. slide: Object variable to iterate through each slide in the PowerPoint presentation.

  1. Set Excel File Path: Specify the path to the Excel file (excelFilePath) you want to read data from.

2. Create Excel Application and Open Workbook: Create a new instance of the Excel application using CreateObject("Excel.Application"). Open the Excel workbook located at excelFilePath using excelApp.Workbooks.Open(excelFilePath). Set ws to refer to the first worksheet (assuming data is on the first sheet).

3. Read Data from Excel: Read the value from cell J316 of the Excel worksheet and store it in the cellValue variable using ws.Range("J316").Value.

4. Display Message Box: Show a message box with the value read from Excel using MsgBox.

5. Iterate Through PowerPoint Slides: Loop through each slide in the active PowerPoint presentation using a For Each loop. Check if a textbox named "TextBox 1" exists on the current slide using the ShapeExists function. If the textbox exists, update its text with the value from Excel formatted as a percentage (e.g., "50.00%"). Display a message box indicating which slide is being updated.

6. Clean Up: Close the Excel workbook without saving changes (wb.Close False). Quit the Excel application (excelApp.Quit). Set objects (ws, wb, excelApp) to Nothing to release resources and free up memory.

7. ShapeExists Function: This is a helper function that checks if a shape with a specified name exists on a given slide. It uses error handling to handle cases where the shape may not exist, returning True if the shape exists and False otherwise.

VBA code below:

Sub UpdateTextboxesFromExcel()
    Dim excelApp As Object
    Dim wb As Object
    Dim ws As Object
    Dim excelFilePath As String
    Dim cellValue As Variant
    Dim slide As Object

    ' Path to the Excel file
    excelFilePath = "\\sydfpr05a\IPG\AUS-MBW\_Initiative\Clients\2023\IAG\_Comms Design\Charlie Dox\INI-VENTORS\Ini Ventors Draft Excel V1.xlsm"

    ' Create a new Excel application and open the workbook
    Set excelApp = CreateObject("Excel.Application")
    Set wb = excelApp.Workbooks.Open(excelFilePath)
    Set ws = wb.Sheets(1) ' Assuming data is on the first sheet

    ' Read data from cell J316
    cellValue = ws.Range("J316").Value

    ' Display a message box with the value read from Excel
    MsgBox "Read from Excel: " & cellValue

    ' Iterate through each slide in the PowerPoint presentation
    For Each slide In ActivePresentation.Slides
        ' Check if the textbox named "TextBox 1" exists in the slide
        If ShapeExists("TextBox 1", slide) Then
            ' Update the textbox text with data from Excel
            slide.Shapes("TextBox 1").TextFrame.TextRange.Text = Format(cellValue, "0.00%")
            ' Display a message box indicating the textbox is being updated
            MsgBox "Updating TextBox 1 on slide " & slide.SlideIndex
        End If
    Next slide

    ' Clean up
    wb.Close False
    excelApp.Quit
    Set ws = Nothing
    Set wb = Nothing
    Set excelApp = Nothing
End Sub

Function ShapeExists(shapeName As String, slide As Object) As Boolean
    Dim shp As Object
    On Error Resume Next
    Set shp = slide.Shapes(shapeName)
    On Error GoTo 0
    ShapeExists = Not shp Is Nothing

End Function

1

Problem: The macro appears to understand what excel path to look at but it's not updating the textbook with the data from the excel cell I have identified. If anyone could advise where I may be going wrong that would be great :)

5
  • Please double check the shape's name. It may be Rectangle 1 or TextBox n. Commented Jan 17, 2024 at 4:53
  • Shape name is all good because I've run a macro that inserts static text just within PP and it works. Commented Jan 17, 2024 at 4:56
  • Have you seen MsgBox "Updating TextBox 1 on slide "? If not, it means ShapeExists() return False. The only reason I can speculate is a mismatched name. Commented Jan 17, 2024 at 5:02
  • Yes the message box appears and will say "Updating Text 1 on Slide 2" then I click ok but nothing will appear in the textbox. If that is appearing, does that prove the names are matched? Commented Jan 17, 2024 at 6:19
  • 2
    Have you checked the content on the first message box in your screenshot? It shows J316 is a blank cell. So nothing is in Textbox 1. Commented Jan 17, 2024 at 6:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.