Consulting

Results 1 to 7 of 7

Thread: Change certain words to UPPER case.

  1. #1
    VBAX Tutor PAB's Avatar
    Joined
    Nov 2011
    Location
    London (UK)
    Posts
    243
    Location

    Change certain words to UPPER case.

    Good evening,

    In WORD 2007.

    I have a list of certain words [about 20 words] that I would like to change to UPPERCASE using VBA.
    Is there any way that I can do this please using something like two arrays. One for the way the words are currently, and the other for the way that I want the words changed to please?

    Thanks in advance.
    -----------------------------------------∏-

    12:45, restate my assumptions.
    Mathematics is the language of nature.
    Everything around us can be represented and understood through numbers.
    If you graph the numbers of any system, patterns emerge. Therefore, there are patterns everywhere in nature.

    -----------------------------------------∏-

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,059
    Location
    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey
    Dim oRng As Range
    Dim arrFind() As String
    Dim lngIndex As Long
      arrFind = Split("now|is|the|time|for|all|good|men", "|")
      For lngIndex = 0 To UBound(arrFind)
        Set oRng = ActiveDocument.Range
        With oRng.Find
          .Text = arrFind(lngIndex)
          .Replacement.Text = UCase(arrFind(lngIndex))
          .Execute Replace:=wdReplaceAll
        End With
      Next lngIndex
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Tutor PAB's Avatar
    Joined
    Nov 2011
    Location
    London (UK)
    Posts
    243
    Location
    That's brilliant Greg, thank you so much, it is appreciated!

    If I wanted to change them back to LOWERCASE, I thought that changing the UCase to . . .

    .Replacement.Text = LCase(arrFind(lngIndex))
    . . . would work but it doesn't!

    Thanks in advance.
    -----------------------------------------∏-

    12:45, restate my assumptions.
    Mathematics is the language of nature.
    Everything around us can be represented and understood through numbers.
    If you graph the numbers of any system, patterns emerge. Therefore, there are patterns everywhere in nature.

    -----------------------------------------∏-

  4. #4
    The following should work
    arrFind = Split(UCase("now|is|the|time|for|all|good|men"), "|")    For lngIndex = 0 To UBound(arrFind)
            Set oRng = ActiveDocument.Range
            With oRng.Find
                .MatchCase = True
                .ClearFormatting
                .Text = arrFind(lngIndex)
                .Replacement.Text = LCase(arrFind(lngIndex))
                .Execute Replace:=wdReplaceAll
            End With
        Next lngIndex
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    VBAX Tutor PAB's Avatar
    Joined
    Nov 2011
    Location
    London (UK)
    Posts
    243
    Location
    That's brilliant Graham, thank you so much, it is appreciated!

    I can just go through the few that change the words like copying [after using UPPERCASE] to COPYINg [because I used copy and in in the array].

    Have a great weekend
    Greg and Graham and thank you both again, it is appreciated!


    -----------------------------------------∏-

    12:45, restate my assumptions.
    Mathematics is the language of nature.
    Everything around us can be represented and understood through numbers.
    If you graph the numbers of any system, patterns emerge. Therefore, there are patterns everywhere in nature.

    -----------------------------------------∏-

  6. #6
    Add
    .MatchWholeWords = True
    and that should overcome the part word issue.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  7. #7
    VBAX Tutor PAB's Avatar
    Joined
    Nov 2011
    Location
    London (UK)
    Posts
    243
    Location
    That's brilliant Graham, thank you so much, it is appreciated!

    I just had to change . . .
    .MatchWholeWords = True
    to . . .
    .MatchWholeWord = True
    and it works perfectly!
    -----------------------------------------∏-

    12:45, restate my assumptions.
    Mathematics is the language of nature.
    Everything around us can be represented and understood through numbers.
    If you graph the numbers of any system, patterns emerge. Therefore, there are patterns everywhere in nature.

    -----------------------------------------∏-

Posting Permissions

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