Solved

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

Posted on 2013-11-08
2
943 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 49

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

910 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

22 Experts available now in Live!

Get 1:1 Help Now