Consulting

Results 1 to 8 of 8

Thread: One variable, several possible values?

  1. #1
    VBAX Regular
    Joined
    Jul 2014
    Posts
    11
    Location

    One variable, several possible values?

    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!

  2. #2
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,433
    Location
    If .Characters.Last Like "[.:;,?!]"
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Regular
    Joined
    Jul 2014
    Posts
    11
    Location
    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

  4. #4
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,433
    Location
    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 ...
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Regular
    Joined
    Jul 2014
    Posts
    11
    Location
    Quote Originally Posted by macropod View Post
    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

  6. #6
    VBAX Regular
    Joined
    Jul 2014
    Posts
    11
    Location
    ... 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

  7. #7
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,433
    Location
    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!
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  8. #8
    VBAX Regular
    Joined
    Jul 2014
    Posts
    11
    Location
    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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •