Dim strLSR as String
strLSR = "wdLineSpaceMultiple"
With ActiveDocument.Styles("myStyle")
.ParagraphFormat.LineSpacingRule = strLSR
End With
It produces a type mismatch error.
strLSR = "wdLineSpaceMultiple"
debug.print strLSR
actually resolves to the numerical value "5". This led me on a wild goose chase for a solution that would automagically convert my string to its numerical value.
Sub codeSnippet()
Dim strLSR As String ' Line Spacing Rule
strLSR = "wdLineSpaceSingle"
With ActiveDocument.Styles("Normal")
.ParagraphFormat.LineSpacingRule = myEnumLineSpace(strLSR) <-- = result of a function
'.ParagraphFormat.LineSpacingRule = strLSR <-- does not work
End With
End Sub
Function myEnumLineSpace(myVar As String) As Integer
By: Dr Tribos 19/06/2014 (dd/mm/yyyy)
' How to call...
' Dim myVar as String
' <.property> = myEnumLineSpace(myVar)
Select Case myVar
Case "wdLineSpace1pt5": myEnumLineSpace = 1
Case "wdLineSpaceAtLeast": myEnumLineSpace = 3
Case "wdLineSpaceDouble": myEnumLineSpace = 2
Case "wdLineSpaceExactly": myEnumLineSpace = 4
Case "wdLineSpaceMultiple": myEnumLineSpace = 5
Case "wdLineSpaceSingle": myEnumLineSpace = 0
Case Else: myEnumLineSpace = 0
End Select
End Function 'myEnumLineSpace
Sub EnumToCase()
' PURPOSE:
' Take the pain out of creating Select Case for Enumerated
' items when MS Office is expecting a number instead of
' the string.
' INSTRUCTIONS:
' 1. Find the list of Enumerated items in Word Help
' 2. Copy the list
' 3. Paste the list into MS Word (it will appear as a table)
' 4. Run EnumToCase (it will write to the Immediate Window)
' 5. Copy from Immediate Window, paste to code
Dim str As String
Dim strDefault As String
Dim i As Integer
Dim oTbl As Table
Set oTbl = ActiveDocument.Tables(1)
str = InputBox("Enter a FunctionName, no spaces")
strDefault = InputBox("Enter a Default value (for Case Else)")
Debug.Print "Function " & str & "(myVar as String) as Long"
Debug.Print "' By: " & Application.userName & " " & Date & " (dd/mm/yyyy)"
Debug.Print
Debug.Print "' How to call..."
Debug.Print "' Dim myVar as String"
Debug.Print "' <.property> = " & str & "(myVar)"
Debug.Print
Debug.Print " Select Case myVar"
For i = 1 To oTbl.Rows.Count
Debug.Print " Case " & Chr(34) & Replace(oTbl.Rows(i).Cells(1).Range.Text, ChrW(13) & ChrW(7), "") & Chr(34) & ": " & str & " = " & Replace(oTbl.Rows(i).Cells(2).Range.Text, ChrW(13) & ChrW(7), "")
If i = 101 Then
Stop ' Copy from Immediate
End If
Next i
Debug.Print " Case Else : " & str & " = " & strDefault
Debug.Print " End Select"
Debug.Print "End Function '" & str
End Sub
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (5)
Commented:
2. You should mine the name/value pairs from this web site using the MSXML2.XMLHTTP object. Once you have retrieved the responsetext, you can parse the result with a DOM object or the regular expression object. Be aware that the name values have a trailing space and should be trimmed before saving.
http://msdn.microsoft.com/en-us/library/office/aa211923(v=office.11).aspx
NOTE: There are other sources of these values from people who have encountered your problem.
An alternative to #2 is to use the TypeLibInfoFromFile() method from the TypeLibInfo library. The constants are in the msword.olb file.
If you want to construct code, you can use the VBE (VB.Project) object to create the code directly. Be aware that there are character limits to routines. It used to be 32K, but that was a long time ago.
Author
Commented:Thank you for your comment. As I said, I'm a beginner (& I'm here to learn).
I did consider using a scripting dictionary (and have several in my code already) however as far as I know a Scripting Dictionary Object would have to be created & (more to the point) populated at run time whereas the Select Case Function is right there, ready to go. So I expect the Select Case to be overall faster; particularly IF it is only called a few times (and particularly if my favored options are nearer the top).
That said, I would be interested to know from a technical perspective why you say: And, when (if ever) you'd advocate using select case.
NOTE: Yes I'm sure there are other sources - I could have googled. My code creates a select case function from a 2 column table, it can contain any information...
Your comments on: mining websites for information, using TypeLibInfoFromFile() and using VBE (VB.Project) object to create the code directly are really interesting and I will follow-up.
Thanks & Cheers,
Commented:
You can use the .INI format or some other standard delimited format, such as CSV or tab-delimited, pipe-delimited, etc. You would use the intrinsic functions or API calls to read INI files and use FileSystemObject.TextStrea
If using a delimited text file (non-INI format), you have two fairly simple and fast methods of parsing the data:
* The VBScript.regexp object (regular expression engine)
*The Split() function to parse the data by lines and then iterating each line data, using the Split() function to parse the name/value pairs.
As you are parsing the data, you populate a dictionary object with the name/value pairs. The name (string data) is the key to the item and its numeric value is the data associated with that (key) name. All of this takes place in a couple of seconds, or less. Once populated, you exploit the power and speed of the dictionary object to hash into its data store with your string value and access the associated numeric value.
I have written the first of several articles on this. The first article is under editor review now. The first article uses the tlbinf32.dll. The second article will probably be the programmatic mining of the enumeration data directly from the OLB files, using VBA code. The third article will be the mining of the data from Microsoft.com. I will definitely show the externalization (persistence) of the data in the third article.
With hash tables, you don't really worry about optimizing the most frequently occurring keys/names at the start of the list (Case clause order). Back in the late 90s, I gave a user group presentation that looked at the performance of the Select Case statement and the comparison of string values was MUCH slower that the comparison of numeric values.
Author
Commented:Thanks for the followup. I'm very much looking forward to reading your articles... I wish someone had told me all that about a year ago (on reflection they might have but I didn't understand).
I have also used INI files but only to grab single items of information. However now, where I need the flexibility you mention, I am moving to XML and find it satisfactorially fast.
I still think select case has a place in code, at least as much as an if statement, but certainly agree that depending on the task other approaches may be more appropriate.
In the meanwhile I hope the article serves to help other learners understand what select case is, how it can be used, and provides some insight into programming as they "learn to walk, before they run".
The thing that occurred to me was that here I am performing a repetitive (programming) task so that I did not have to perform a repetitive (documenting) task... so I had a crack (1st attempt) at automating the programming process...
Commented:
http:/A_14015-Iterating-Office-Enumeration-Constants.html