VBA - Add Textboxes works fine, but on 2nd loop I want to update and not Add

C

Colleyville Alan

I have two nested loops that go through a couple of arrays and place
textboxes on a slide. One array has the X & Y coordinates and the other has
the names of mutual funds. I want to put the names of the mutual funds in
the textboxes and place the textboxes at specific X/Y coordinates. If there
is only one fund in a category, this works just fine. But if there is more
than one (e.g. two Large Value funds), then the first time through the loop
I would add a text box. The next time through, I do not want to add a new
box, I want to update the box just added. The category is determined by the
3rd line (the one that refers to an Order property).

Here is the code:

For x = 1 To UBound(XYArray)
For y = 1 To UBound(FundArray)
If (FundArray(y).Order + 1) > XYArray(x).Order_Low And
(FundArray(y).Order - 1) < XYArray(x).Order_High Then ' test for Category
Set oSlide = oPres.Slides(1)
Set oShape =
oSlide.Shapes.AddTextbox(msoTextOrientationHorizontal, 426#, 72#, 132#, 78#)
With oShape
.TextFrame.TextRange.Text = FundArray(y).Fund_Name
& vbCr
End With
End If
Next y
Next x


I cannot think about how to test for the presense of the textbox. Any
ideas?
Thanks
 
S

Steve Rindsberg

If I understand the problem correctly, I think you could do one of two things
to deal with multiple funds under one company name:

Name the text boxes as you add them; if the name is e.g. the name of the
company, then you could later try to change the text in .Shapes(CompanyName)
and trap for errors. If there's no error, there's already a box by that name,
update it. If there's an error, then you need to add a new text box.

or

Tag or Name the text boxes as you add them, again using the company info.
Before adding a new shape, iterate through the shapes collection testing each
for a name or tag = the company name of the fund you're about to add.

or (ok, I said two ... that was then, this is now)

As you add each new fund, iterate through the array you're already maintaining
looking for an already-existing match, from lBound(array) to current array
position - 1
 
B

Brian Reilly, MS MVP

Steve,
Yeh, I mumbled to myself about this this morning and thought I was
repeating my self since the definition of what he wants is still
unclear to me. I thought about the .Name and .Tag thingie but the
logic just doesn't work easily they way he has it laid out.

Colleyville, if Steve's thoughts don't work correctly for you, can you
try the question again. Leave this code sample in here but write a bit
of pseudo-code that would explain the logic you are after.

It's do-able but I don't know what you want to do. (g)

Brian Reilly, PowerPoint MVP
 
C

Colleyville Alan

Steve Rindsberg said:
If I understand the problem correctly, I think you could do one of two things
to deal with multiple funds under one company name:

Name the text boxes as you add them; if the name is e.g. the name of the
company, then you could later try to change the text in ..Shapes(CompanyName)
and trap for errors. If there's no error, there's already a box by that name,
update it. If there's an error, then you need to add a new text box.

or

Tag or Name the text boxes as you add them, again using the company info.
Before adding a new shape, iterate through the shapes collection testing each
for a name or tag = the company name of the fund you're about to add.

or (ok, I said two ... that was then, this is now)

As you add each new fund, iterate through the array you're already maintaining
looking for an already-existing match, from lBound(array) to current array
position - 1


Thanks. I finally came up with something that works that is similar to the
ideas you gave me above. I set a variable equal to the array's Order
property when I added a textbox. Then during the next loop, the variable is
compared against the new array element's Order property. If they are the
same, then I use the InsertAfter command to add new text to the shape object
I last assigned.

This works, but I have two problems. I want to color the lines of text
individually based on some criteria. I have done this with
TextFrame.TextRange.Font.Color = vbBlue (or vbBlack), but it colors all of
the lines in the textbox and overrides prior color selections. Is there a
way to color only the line that I am inserting?

Also, the text is aligning as centered. I can change to left alignment
interactively, but have yet to find the VBA command for this. How do I
change the alignment to left align?
 
S

Shyam Pillai

For paragraph alignment:
oShape.TextFrame.TextRange.Paragraphs.ParagraphFormat.Alignment=ppAlignCente
r

For coloring only the added text :
Dim oTxtRng as TextRange
' =======================
'
Set oTxtRng = oShape.TextFrame.TextRange.InsertAfter("my new text")
oTxtRng.Font.Color = vbRed

Also, look at the following samples to understand the text handling better:
Locate specific text and format the shape containing it
http://www.mvps.org/skp/ppt00048.htm#1

Locate and highlight instances of a specific word
http://www.mvps.org/skp/ppt00048.htm#2
 
C

Colleyville Alan

Thanks - that did the trick.


Shyam Pillai said:
For paragraph alignment:
oShape.TextFrame.TextRange.Paragraphs.ParagraphFormat.Alignment=ppAlignCente
r

For coloring only the added text :
Dim oTxtRng as TextRange
' =======================
'
Set oTxtRng = oShape.TextFrame.TextRange.InsertAfter("my new text")
oTxtRng.Font.Color = vbRed

Also, look at the following samples to understand the text handling better:
Locate specific text and format the shape containing it
http://www.mvps.org/skp/ppt00048.htm#1

Locate and highlight instances of a specific word
http://www.mvps.org/skp/ppt00048.htm#2
--
Regards
Shyam Pillai

http://www.mvps.org/skp

Colleyville Alan said:
that
name, testing
each


Thanks. I finally came up with something that works that is similar to the
ideas you gave me above. I set a variable equal to the array's Order
property when I added a textbox. Then during the next loop, the
variable
is
compared against the new array element's Order property. If they are the
same, then I use the InsertAfter command to add new text to the shape object
I last assigned.

This works, but I have two problems. I want to color the lines of text
individually based on some criteria. I have done this with
TextFrame.TextRange.Font.Color = vbBlue (or vbBlack), but it colors all of
the lines in the textbox and overrides prior color selections. Is there a
way to color only the line that I am inserting?

Also, the text is aligning as centered. I can change to left alignment
interactively, but have yet to find the VBA command for this. How do I
change the alignment to left align?


 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top