View Full Version : One variable, several possible values?
kobber
11-24-2014, 07:05 AM
Hi everyone,
I was just wondering if whether it is possible in VBA to have a variable which has several possible values, like:
Dim Punctuation As String
...
Punctuation = "." OR ";" OR "," OR ":" (etc.)
This could save some time and code. What I need to do is find a specific object in a Word document (next footnote) and determine whether the character following it is one of the possible values I would like to assign to my variable "Punctuation" (assuming this is even possible). If yes, the macro will transpose the two, putting the punctuation before the footnote, which is preferred style in English.
I suppose I could do something long-winded like this:
If [selected character following footnote] = "." OR ";" OR "," OR ":" (etc.) Then
[Transpose]
End If
... but I just wanted to find out if there was another, perhaps better way.
Thanks for your help and have a great day!
macropod
11-24-2014, 07:40 AM
If .Characters.Last Like "[.:;,?!]"
kobber
11-24-2014, 08:01 AM
Hi Paul,
That looks sweet! I knew there had to be a smoother way of going about it.
Just one question, though. Can you use .Characters.Last in this case? I will be dealing with footnotes, which are active fields in Word. Is the punctuation considered to the be the last character of the field?
How about .Characters.Next ?
Thanks again,
Andrew
macropod
11-24-2014, 01:14 PM
Footnotes in Word are not fields.
The last character of a footnote is it's paragraph break. You therefore want to test the character before that. For example:
If ActiveDocument.Footnotes(1).Range.Characters.Last.Previous Like "[.:;,?!]" Then ...
kobber
11-24-2014, 02:11 PM
Footnotes in Word are not fields.
You are of course right and I apologise for my sloppy explanation. I'm actually working with footnote cues, in the body of the text. I may be mistaken in calling them fields, but they are numbered automatically by Word. Regarding .Last , it seemed odd to me that the character following a footnote cue could be considered the last character of that cue. Sorry I wasn't more precise.
Would my suggestion of .Next be feasible in this case? I'm at home now far from my work PC, so I can't test this stuff right away.
Thanks again and have a fine evening.
Andrew
kobber
11-24-2014, 02:14 PM
... and you' ve just given me a great idea on how to make sure all footnotes end with a full stop! Again, our preferred style. Thanks!
Andrew
macropod
11-24-2014, 02:52 PM
So you're not even referring to the footnotes, but to their references in the body of the document? In that case, try:
Sub FootnoteEndnoteFix()
Dim FtNt As Footnote, EndNt As Endnote, Rng As Range
With ActiveDocument
For Each FtNt In .Footnotes
Set Rng = FtNt.Reference
With Rng
'Eliminate any spaces before the footnote
While .Characters.First.Previous.Text = " "
.Characters.First.Previous.Text = vbNullString
Wend
'Swap the footnote/punctuation, as applicable
Select Case .Characters.Last.Next
Case ".", ",", "!", "?", ":", ";"
.InsertBefore .Characters.Last.Next
.Characters.Last.Next.Delete
End Select
End With
Next
For Each EndNt In .Endnotes
Set Rng = EndNt.Reference
With Rng
'Eliminate any spaces before the endnote
While .Characters.First.Previous.Text = " "
.Characters.First.Previous.Text = vbNullString
Wend
'Swap the endnote/punctuation, as applicable
If .Characters.Last.Next Like "[.,!?:;]" Then
.InsertBefore .Characters.Last.Next
.Characters.Last.Next.Delete
End If
End With
Next
End With
End Sub
Just for variety, I've use a Select Case test for footnotes and an If Like test for endnotes!
kobber
11-28-2014, 07:57 AM
Hi Paul,
That works just great. Thanks very much. I have adapted it for my French co-workers, making it do the opposite change, so the punctuation is after the footnote cue! I'm sure you can imagine the hours of work this is going to save some poor souls.
Sub FootnoteFixFR()
Dim FtNt As Footnote, EndNt As Endnote, Rng As Range
With ActiveDocument
For Each FtNt In .Footnotes
Set Rng = FtNt.Reference
With Rng
'Eliminate any spaces before the footnote
While .Characters.First.Previous.Text = " "
.Characters.First.Previous.Text = vbNullString
Wend
'Swap the footnote/punctuation, as applicable
Select Case .Characters.First.Previous
Case ".", ",", "!", "?", ":", ";"
.InsertAfter .Characters.First.Previous
.Characters.First.Previous.Delete
End Select
End With
Next
For Each EndNt In .Endnotes
Set Rng = EndNt.Reference
With Rng
'Eliminate any spaces before the endnote
While .Characters.First.Previous.Text = " "
.Characters.First.Previous.Text = vbNullString
Wend
'Swap the endnote/punctuation, as applicable
If .Characters.First.Previous Like "[.,!?:;]" Then
.InsertAfter .Characters.First.Previous
.Characters.First.Previous.Delete
End If
End With
Next
End With
End Sub
Take it easy, eh?
Andrew
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.