?
Solved

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

Posted on 2013-11-08
2
Medium Priority
?
1,014 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 52

Accepted Solution

by:
Rgonzo1971 earned 2000 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

752 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