PDA

View Full Version : [SOLVED:] Adding Multiple Lines of Text



hunter21188
01-23-2016, 01:59 PM
I am trying to reference one text box (named WarningText1) and output multiple lines of text depending on what the user selects in the GUI. This is what I am trying:


Private Sub WarningInfo()Call Dictionary.WarningInfo
Call Dictionary.WindInfo


'Sets the font for the warning information text.


With ActiveWindow.Selection.SlideRange.Shapes("WarningText1").TextFrame2.TextRange.Font

.Size = 24
.Name = "Aharoni"
.Shadow.Visible = True


End With


ComboBoxList = Array(CStr(ComboBox2), CStr(ComboBox3), CStr(ComboBox4), CStr(ComboBox5), CStr(ComboBox6))




For Each Ky In ComboBoxList
On Error Resume Next
'If nothing is selected in all dropdowns, do nothing and exit this sub.
If ComboBox2 = "" And ComboBox3 = "" And ComboBox4 = "" And ComboBox5 = "" And ComboBox6 = "" Then
Exit Sub
'Otherwise, if even one dropdown has a selection, insert selected text.
Else
ActiveWindow.Selection.SlideRange.Shapes("WarningText1").TextFrame2.TextRange.Text = dict2.Item(Ky)(0) & dict3.Item(Ky)(0)


End If


Next


Set dict2 = Nothing


End Sub

When I run that line of code using EITHER the dict2 or dict3 line, it works great. But it doesn't seem to be able to add them together this way.

I assume there is a simple solution, but I can't seem to find it anywhere online. Appreciate any help!

*****EDIT*****


I added code to help debug. This was added right before the line that is not working:



Debug.Print dict2.Item(Ky)(0)
Debug.Print dict3.Item(Ky)(0)


Both values printed out exactly what they were supposed to. It just won't add them both to the same text box.

Thanks,

Cory

John Wilson
01-24-2016, 05:25 AM
You have the update TextRange inside the loop so it will be overwritten with each iteration won'rt it? It's hard to see how the dictionary works so I'm not sure. Either way I would move it out of the loop.

Alternatively maybe you mean:


ActiveWindow.Selection.SlideRange.Shapes("WarningText1").TextFrame2.TextRange =ActiveWindow.Selection.SlideRange.Shapes("WarningText1").TextFrame2.TextRange _
& dict2.Item(Ky)(0) & dict3.Item(Ky)(0) & vbcrlf

hunter21188
01-24-2016, 01:33 PM
It's not letting me edit the original post, so I will add this bit of code here. This is the code I have within my dictionary. It is where I declare the variables dict2 and dict3.


Option Private Module 'This is necessary so that these modules do not show up in the PPT Macro window.
Public dict2, dict3 As Object, Key, val 'Makes the dictionaries public so they can be accessed by other Modules.


Sub WarningInfo()


'This is the dictionary for the maximum expected hail size.


Set dict2 = CreateObject("Scripting.Dictionary")


Key = "No Hail": val = Array(vbNewLine & _
"No hail expected")
dict2.Add Key, val
Key = "0.25""": val = Array("Hail: Up to 0.25""")
dict2.Add Key, val
Key = "0.50""": val = Array("Hail: Up to 0.50""")
dict2.Add Key, val
Key = "0.75""": val = Array("Hail: Up to 0.75""")
dict2.Add Key, val


End Sub




Sub WindInfo()


'This is the dictionary for the maximum expected wind speed.


Set dict3 = CreateObject("Scripting.Dictionary")


Key = "35 mph": val = Array("Wind: Up to 35 mph")
dict3.Add Key, val
Key = "40 mph": val = Array("Wind: Up to 40 mph")
dict3.Add Key, val
Key = "45 mph": val = Array("Wind: Up to 45 mph")
dict3.Add Key, val


End Sub

I'm not sure how I would move that line out of the loop since the variable Ky will be different for each ComboBox, so it needs to loop through all iterations in order to get the value of Ky for dict2 (which should correspond to what the user selected in ComboBox2) and dict3 (should correspond to what the user selected in ComboBox3).

John Wilson
01-24-2016, 02:05 PM
Your code just crashes here.

I would think you need to change it to ADD to rather that overwrite the textrange as in my alternative suggestion. Otherwise you will need to post the whole presentation with any references.

hunter21188
01-24-2016, 02:14 PM
The alternative method I can't get to work either. Another solution would be this:


Private Sub WarningInfo()
Call Dictionary.WarningInfo


'Sets the font for the warning information text.


With ActiveWindow.Selection.SlideRange.Shapes("WarningText1").TextFrame2.TextRange.Font

.Size = 24
.Name = "Calibri"
.Shadow.Visible = True


End With


ComboBoxList = Array(CStr(ComboBox3))


For Each Ky In ComboBoxList

'On Error Resume Next
'If nothing is selected in ComboBox3, do nothing and exit this sub.
If ComboBox3 = "" Then
Exit Sub
'Otherwise, if even it has a selection, insert selected text.
Else
'Debug.Print vbCrLf & dict2.Item(Ky)(0) & vbCrLf '& dict3.Item(Ky)(0)
ActiveWindow.Selection.SlideRange.Shapes("WarningText1").TextFrame2.TextRange = vbCrLf & dict2.Item(Ky)(0)


End If


Next


Set dict2 = Nothing


End Sub


Private Sub WarningInfo2()
Call Dictionary.WindInfo


'Sets the font for the warning information text.


With ActiveWindow.Selection.SlideRange.Shapes("WarningText1").TextFrame2.TextRange.Font

.Size = 24
.Name = "Calibri"
.Shadow.Visible = True


End With


ComboBoxList = Array(CStr(ComboBox4))


For Each Ky In ComboBoxList

'On Error Resume Next
'If nothing is selected in ComboBox4, do nothing and exit this sub.
If ComboBox4 = "" Then
Exit Sub
'Otherwise, if it has a selection, insert selected text.
Else
ActiveWindow.Selection.SlideRange.Shapes("WarningText1").TextFrame2.TextRange.Text = vbCrLf & dict3.Item(Ky)(0)


End If


Next


Set dict3 = Nothing


End Sub


Just create a different Sub for each variable. However, I would need to append the text in WarningInfo2 to the existing text within the shape (WarningText1). This way it wouldn't just overwrite the text from WarningInfo. Do you know of a way to append the text in this line?


ActiveWindow.Selection.SlideRange.Shapes("WarningText1").TextFrame2.TextRange.Text = vbCrLf & dict3.Item(Ky)(0)

Thanks for all your help, John.

John Wilson
01-24-2016, 02:58 PM
Unless you are able to post the whole presentation I'm afraid I'm going to bow out. Your code is pretty well impossible to follow and unless there are things you are not telling us is bound to crash. If you post the whole presentation somewhere (not just some of the code) I'll take a look next week.

hunter21188
01-24-2016, 03:37 PM
Using your original suggested solution, I was able to get that to work when implementing it with the latest code I provided above. It's probably not the cleanest solution, but it works! Thanks again, John.

John Wilson
01-25-2016, 02:23 AM
It's hard to follow your code but if you are taking this forward I would try to clean it up now.

I would not use a dictionary (presuming I understand what you need)

You could probably just use Functions which are natively supported in PPT and don't require creating an object Scripting Runtime. If you set Option Explicit (and you should) which requires vars to be properly declared I think your code will crash anyway.

If the attached is basically what you are trying to achieve have a look at how it works.

15271

hunter21188
01-25-2016, 12:18 PM
Wow, John. Very nice. The reason I have a dictionary is because I reference the drop downs and all the information included within on multiple different slides. I have attached a "working" presentation with my code. The storm direction/movement options do not work yet, but that is intended so that once the direction and storm speed are selected, it will insert an arrow facing that direction with the storm speed in a text box below.

One of the reasons I have it set up the way I do is because if the user does not make a selection for hail (the storm only has a wind threat), I need the wind text to move up to where the hail text would be if that were selected as well. This way there is no large gap between the expiration time at the top and the wind speed. Hope this all makes sense.

I think I still need the dictionary for these purposes as opposed to what you did, but I could easily be wrong. I am still very new with programming in general, especially VBA.

Thanks so much for all your help!!

15280