Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

You cannot set text in a diagram node shape through Visual Basic for Applications in Excel 2003 or in Excel 2002


View products that this article applies to.

Symptoms

In Excel 2003 and Excel 2002, it is not possible to programmatically set the text in a diagram node shape through Visual Basic for Applications (VBA). If you try to set the text in a diagram node shape programmatically, you may receive the following error message:

Run-time error '1004': Unable to set the Text property of the Characters class

↑ Back to the top


Workaround

To work around this issue, design your macro to automate Microsoft Word or Microsoft PowerPoint in this manner, to create the diagram in the automated program, and then to copy it into the Excel workbook.

The following sample macro creates an organization chart diagram with four nodes in Word, sets the text for each node, and then copies the diagram to an Excel workbook:
Sub TextShapeAddText()
'/ Dim Integer(s)
   Dim i As Integer
   
'/ Dim Object(s)
   Dim oCurShape As Object
   Dim oCurShapeNode As Object
   Dim oCurDiagNode As Object
   Dim oCurWorkApplObj As Object
 
'/ Create a Word application object.
   Set oCurWorkApplObj = CreateObject("Word.Application")
   
'/ Open a new Word document.
   Workbooks.Add
   oCurWorkApplObj.Documents.Add
   
'/ Add a shape.
   Set oCurShape = oCurWorkApplObj.ActiveDocument.Shapes.AddDiagram _
   (msoDiagramPyramid, 10, 15, 400, 475)

'/ Add a node.
   Set oCurShapeNode = oCurShape.DiagramNode.Children.AddNode
       
'/ Add child nodes.
   For i = 1 To 3
       oCurShapeNode.AddNode
   Next

'/ Add text to the child nodes.
   For i = 1 To 4 'Inserting text in each node
      oCurShapeNode.Diagram.Nodes.Item(i) _
      .TextShape.TextFrame.TextRange.Text = Str(i)
   Next
       
'/ Copy the shape to Excel.
   oCurWorkApplObj.ActiveDocument.Shapes.SelectAll
   oCurWorkApplObj.Selection.Copy
   ActiveSheet.Paste       
       
'/ Quit Word.
   oCurWorkApplObj.Quit saveChanges:=False       

End Sub
				

↑ Back to the top


Status

Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.

↑ Back to the top


Keywords: KB317293, kbpending, kbofficexppresp2fix, kbbug

↑ Back to the top

Article Info
Article ID : 317293
Revision : 2
Created on : 7/20/2007
Published on : 7/20/2007
Exists online : False
Views : 340