Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-19-2017, 05:47 AM
slaycock slaycock is offline A wish for enumerated strings Windows 7 64bit A wish for enumerated strings Office 2013
Expert
A wish for enumerated strings
 
Join Date: Sep 2013
Posts: 255
slaycock is on a distinguished road
Default A wish for enumerated strings

I know these can't be done in VBA but in my current project I have a number of cases where a string based enumeration would save a lot of sideways coding.



So this is really a question of how do you program around such a restriction.
Reply With Quote
  #2  
Old 07-19-2017, 03:25 PM
macropod's Avatar
macropod macropod is offline A wish for enumerated strings Windows 7 64bit A wish for enumerated strings Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,375
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

What do you mean by 'string based enumeration' and 'sideways coding'? What are you trying to achieve?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 07-20-2017, 01:23 AM
slaycock slaycock is offline A wish for enumerated strings Windows 7 64bit A wish for enumerated strings Office 2013
Expert
A wish for enumerated strings
 
Join Date: Sep 2013
Posts: 255
slaycock is on a distinguished road
Default

I'm just noticing that there are a number of items which use strings labels where the 'elegance' of the code might be improved by enumertion based on string rather than Longs

Such items include

• Custom Document Properties
• Custom XML Parts
• CustomUI Ribbon Control Identifiers
• Collections/Scripting Dictionaries
• User types that need string labels (e.g. user defined styles.

For example I have a set of names for elements in a CustomXML part. There are many sets of these elements.

Code:
Enum XMLTags

  TagName1 = "Name1"
  TagName2 = "Name2"
  TagName3 = "Name3"
....
  TagName25

End enum


For myTag = TagName1 to TagName13

  <Code for each tag>

Next
Rather than

defining the TagNames as Constants, Adding the Constant to an Array, defining an enumeration to index the TagNames in the array and then

Code:
Enumeration for tag index
Array for tag names
Assgning values to the name array
 
then

For myTagIndex = TagNameArray(enTagName1) to TagNameArray(enTageName13)

<code relating to tag>
Next
Or constructing the tag name on the run

Code:
Const  TagBase as string="Name"
Dim myTag as String
Dim myTagIndex as Long

For myTagIndex=1 to 13
  
  myTag = TagBase & myTabIndex
 if Dictionary(Key:=myTag).objectitem=<a value> then

end if

Next

None of the above cause me problems, but they do add an amount of coding 'cruft' that could be avoided with enumerated strings.

Now I hold my hands up and admit that I'm self taught with the aid of this forum, VBA in a Nutshell book, the sometimes useful MS help pages and other random reading via Google so it may be that I'm missing something very obvious.

In a nutshell I'm not stuck, just wondering about the best way to improve the readability of my code.
Reply With Quote
  #4  
Old 07-20-2017, 04:31 PM
macropod's Avatar
macropod macropod is offline A wish for enumerated strings Windows 7 64bit A wish for enumerated strings Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,375
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You could just add the items to an array directly:
Code:
Sub Demo()
Dim i As Long, myArray()
myArray = Array("Name1", "Name2", "Name3", "Name4", "Name5", "Name6", "Name7", "Name8")
For i = LBound(myArray) To UBound(myArray)
  MsgBox myArray(i)
Next
End Sub
If need be, such an array can even be constructed on the fly.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 07-21-2017, 08:11 AM
slaycock slaycock is offline A wish for enumerated strings Windows 7 64bit A wish for enumerated strings Office 2013
Expert
A wish for enumerated strings
 
Join Date: Sep 2013
Posts: 255
slaycock is on a distinguished road
Default

The array approach was one of my first solutions (except I was using split with a single string). But to make the code more meaningful I also defined an enumeration to index the array.

This allowed me to say things like

docXMLProp(<propertyname>)

Which still leaves what should be a constant writable. So I made the array behave like a constant by putting it in a class. That leaves me with the choice of one get statement per enumeration value

docXMLProp.<propertyname>

or a single get property with a case statement to allow the form

docXMLProp.item(<propertyname>) or
docXMLProp(<propertyname>)

if you go through the rigamarole of exposting the class, setting the default property as item then reimporting.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
A wish for enumerated strings How to Dim a substring between two strings dwirony Word VBA 4 10-28-2016 07:51 AM
VBA code to extract strings twozedz Excel Programming 1 05-15-2016 06:00 AM
A wish for enumerated strings Search with multiple strings silverspr Excel 7 03-03-2013 04:45 PM
A wish for enumerated strings Removing Character Strings digitalhecht Word 2 10-17-2011 12:53 PM
A wish for enumerated strings update style of all strings available between two specific strings vikrantkale Word 1 03-28-2011 06:13 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:15 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft