Solved

"Unable to get the Characters property of the rectangle class" error in Excel 2007 VBA

Posted on 2013-11-08
2
929 Views
Last Modified: 2013-11-08
I have some code that worked fine in Excel 2003 :

' Set environment
   
    Application.ScreenUpdating = False

' Create text box
' NOTE: UserInterfaceOnly protection does not allow a shape to be added
   
    With ActiveSheet
        .Unprotect
        .Shapes.AddShape(msoShapeRectangle, BoxLeft, BoxTop, BoxWidth, BoxHeight).Select
    End With
   
    With Selection
   
' Use production order number as a name for the shape object

    .Name = PrdOrdNo
       
' Set the text sections

        CharCount = 0
        For I = 1 To 3
       
' Header text is in bold font; body in regular
            .Characters(CharCount + 1).Insert String:=HeaderText(I)
            TextCount = Len(HeaderText(I))
            .Characters(Start:=CharCount + 1, Length:=TextCount).Font.Bold = True
            CharCount = CharCount + TextCount
            .Characters(CharCount + 1).Insert String:=BodyText(I)
            TextCount = Len(BodyText(I))
            .Characters(Start:=CharCount + 1, Length:=TextCount).Font.Bold = False
            CharCount = CharCount + TextCount
        Next
       
' Set common font details

        With .Characters
            With .Font
                .Name = BoxFontName
                .Size = BoxFontSize
                .ColorIndex = BoxFontColour
            End With
        End With
       
' Set shape's colour

        With .ShapeRange
            With .Fill
                .ForeColor.RGB = RGB(BoxRed, BoxGreen, BoxBlue)
                .Transparency = 0
            End With
            .Line.Visible = True
        End With
       
' Use .Autosize to match size to contents, set protection and macro

        .AutoSize = True
        .Locked = True
        .OnAction = "'Select_Text_Box """ & PrdOrdNo & """'"
    End With
   
' Restore worksheet protection

    ActiveSheet.Protect UserInterfaceOnly:=True
   
' Reset environment

    Application.ScreenUpdating = True
End With


I get the error message at :

            .Characters(CharCount + 1).Insert String:=HeaderText(I)

I presume something has changed in Excel 2007/2010 and I now need to select the shape in a different way ?
0
Comment
Question by:lindsay_kirby
2 Comments
 
LVL 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 39634227
Hi,

pls try to replace
        CharCount = 0
        For I = 1 To 3
        
' Header text is in bold font; body in regular
            .Characters(CharCount + 1).Insert String:=HeaderText(I)
            TextCount = Len(HeaderText(I))
            .Characters(Start:=CharCount + 1, Length:=TextCount).Font.Bold = True
            CharCount = CharCount + TextCount
            .Characters(CharCount + 1).Insert String:=BodyText(I)
            TextCount = Len(BodyText(I))
            .Characters(Start:=CharCount + 1, Length:=TextCount).Font.Bold = False
            CharCount = CharCount + TextCount
        Next

Open in new window

with this code

' Set the text sections

        CharCount = 0
        strShapeString = ""
        For i = 1 To 3
                strShapeString = strShapeString & HeaderText(i) & BodyText(i)
                .ShapeRange(1).TextFrame2.TextRange.Characters.Text = strShapeString
        Next
        For i = 1 To 3
            
' Header text is in bold font; body in regular

            ' Header text is in bold font; body in regular
            TextCount = Len(HeaderText(i))
            .ShapeRange(1).TextFrame2.TextRange.Characters(Start:=CharCount + 1, Length:=TextCount).Font.Bold = True
            CharCount = CharCount + TextCount
            TextCount = Len(BodyText(i))
            .ShapeRange(1).TextFrame2.TextRange.Characters(Start:=CharCount + 1, Length:=TextCount).Font.Bold = False
            CharCount = CharCount + TextCount
        Next

Open in new window

The Method insert doesn't work anymore in XL2007 and further. The code creates firstly the text and then apply the formatting.

 Regards
0
 

Author Closing Comment

by:lindsay_kirby
ID: 39634370
Excellent response in terms of timing and detail !!  

Thankyou
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now