Solved

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

Posted on 2013-11-08
2
995 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 51

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

717 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