Consulting

Results 1 to 6 of 6

Thread: Solved: Object Required Error

  1. #1

    Solved: Object Required Error

    Can't figure this out for the life of me. Why am I getting an Object Require Error with the following code:

     
    Set WbAgg = ThisWorkbook
    Set WsAggChrt = WbAgg.Sheets("Chart Data")
    Set Period1 = WsAggChrt.Range("M2")
    Set Period2 = WsAggChrt.Range("M3")
    Set Period3 = WsAggChrt.Range("M4")
    The error is generated with Period3. Isn't the object already set with the line Set WbAgg = ThisWorkbook?

    Thanks,

    Sirius

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Tests OK with me. Do you have any merged cells?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    No merged cells. It's that one cell, and for some reason VBA doesn't like my code. Not sure if this will help, but here is the code in its entirety.

     
    Sub TextBox()
    
    Dim WbAgg As Workbook, WsAggChrt As Worksheet
    Dim Period1, Period2, Period3 As String
    Dim PPTApp As PowerPoint.Application
    Dim PPTPres As PowerPoint.Presentation
    Dim PPTSld As PowerPoint.Slide
    Dim Sld As Slide
    Dim TxtRng As TextRange
    Dim foundText As TextRange
    Dim ReplacePer1, ReplacePer2, ReplacePer3 As TextRange
    
    Set WbAgg = ThisWorkbook
    Set WsAggChrt = WbAgg.Sheets("Chart Data")
    Set Period1 = WsAggChrt.Range("M2")
    Set Period2 = WsAggChrt.Range("M3")
    Set Period3 = WsAggChrt.Range("M4")
    Set PPTApp = GetObject(, "PowerPoint.Application")
    Set PPTPres = PPTApp.ActivePresentation
    PPTApp.ActiveWindow.ViewType = ppViewSlide
    Set ReplacePer1 = PPTPres.Slides(147).Shapes(3).TextFrame.TextRange.Lines(2).Words(Start:=3, Length:=6)
    Set ReplacePer2 = PPTPres.Slides(147).Shapes(3).TextFrame.TextRange.Lines(3).Words(Start:=3, Length:=6)
    Set ReplacePer3 = PPTPres.Slides(150).Shapes(3).TextFrame.TextRange.Lines(2).Words(Start:=3, Length:=6)
         
        'Automated Footer - 2004 Period Replacement
        For Each Sld In PPTPres.Slides
            For Each Shp In Sld.Shapes
                If Shp.HasTextFrame Then
                    Set TxtRng = Shp.TextFrame.TextRange
                    Set foundText = TxtRng.Replace(FindWhat:=ReplacePer1, Replacewhat:=Period1)
                    Set foundText = TxtRng.Replace(FindWhat:=ReplacePer2, Replacewhat:=Period2)
                    Set foundText = TxtRng.Replace(FindWhat:=ReplacePer3, Replacewhat:=Period3)
                    Do While Not (foundText Is Nothing)
                        Set TxtRng = TxtRng.Characters(foundText.Start + foundText.Length, TxtRng.Length)
                        Set foundText = TxtRng.Replace(FindWhat:=ReplacePer1, Replacewhat:=Period1)
                        Set foundText = TxtRng.Replace(FindWhat:=ReplacePer2, Replacewhat:=Period2)
                        Set foundText = TxtRng.Replace(FindWhat:=ReplacePer3, Replacewhat:=Period3)
                    Loop
                End If
            Next
        Next
    End Sub
    Thanks,

    Sirius

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I think your problem is here
    [VBA]
    Dim Period1, Period2, Period3 As String

    [/VBA]
    This is dimming only Period3 as string, the others are Dimmed as variant. Each item requires a specific "As" to Dim it to a type.
    "Set" is used to set the variable to a Range etc. If you just require the value, omit the Set.
    Try the following
    [VBA]
    Option Explicit
    Sub TextBox()
    Dim WbAgg As Workbook, WsAggChrt As Worksheet
    Dim Period1 As String, Period2 As String, Period3 As String
    Dim PPTApp As PowerPoint.Application
    Dim PPTPres As PowerPoint.Presentation
    Dim PPTSld As PowerPoint.Slide
    Dim Sld As Slide
    Dim TxtRng As TextRange
    Dim foundText As TextRange
    Dim ReplacePer1 As TextRange, ReplacePer2 As TextRange, ReplacePer3 As TextRange
    Dim Shp
    Set WbAgg = ThisWorkbook
    Set WsAggChrt = WbAgg.Sheets("Chart Data")
    Period1 = WsAggChrt.Range("M2")
    Period2 = WsAggChrt.Range("M3")
    Period3 = WsAggChrt.Range("M4")
    Set PPTApp = GetObject(, "PowerPoint.Application")
    Set PPTPres = PPTApp.ActivePresentation
    PPTApp.ActiveWindow.ViewType = ppViewSlide
    Set ReplacePer1 = PPTPres.Slides(147).Shapes(3).TextFrame.TextRange.Lines(2).Words(Start:=3, Length:=6)
    Set ReplacePer2 = PPTPres.Slides(147).Shapes(3).TextFrame.TextRange.Lines(3).Words(Start:=3, Length:=6)
    Set ReplacePer3 = PPTPres.Slides(150).Shapes(3).TextFrame.TextRange.Lines(2).Words(Start:=3, Length:=6)

    'Automated Footer - 2004 Period Replacement
    For Each Sld In PPTPres.Slides
    For Each Shp In Sld.Shapes
    If Shp.HasTextFrame Then
    Set TxtRng = Shp.TextFrame.TextRange
    Set foundText = TxtRng.Replace(FindWhat:=ReplacePer1, Replacewhat:=Period1)
    Set foundText = TxtRng.Replace(FindWhat:=ReplacePer2, Replacewhat:=Period2)
    Set foundText = TxtRng.Replace(FindWhat:=ReplacePer3, Replacewhat:=Period3)
    Do While Not (foundText Is Nothing)
    Set TxtRng = TxtRng.Characters(foundText.Start + foundText.Length, TxtRng.Length)
    Set foundText = TxtRng.Replace(FindWhat:=ReplacePer1, Replacewhat:=Period1)
    Set foundText = TxtRng.Replace(FindWhat:=ReplacePer2, Replacewhat:=Period2)
    Set foundText = TxtRng.Replace(FindWhat:=ReplacePer3, Replacewhat:=Period3)
    Loop
    End If
    Next
    Next
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    When I dim Period1, Period2 & Period3 as String, I still get an Object Required error, this time pointing to my Set Period1 statement. I changed the dim from string to variant and the code worked without any problems.

    Thanks for all of your help!

    Sirius

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Glad to help.
    Make sure that you use Option Explicit, which can help identify this type of error.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •