简体   繁体   中英

Feasibility of using macros/VBA for my formatting needs in Word? (Dates, currencies, styles)

I've had some success finding existing code for some of my current needs but I was wondering whether/how I can use VBA to do the following in Microsoft Word as I currently spend a lot of time on formatting. I'm not sure whether some of these are even possible under VBA, so I would really appreciate an indication of their feasibility before I spend even more time than I already have looking into this. Thank you so much!

  1. Change dates to my desired format. eg 24/9/2019, 24 September 2019, 24 Sep 2019, Tuesday (24 Sep) ——> 24 Sep 19

  2. Change currencies. eg £2 billion to GBP2bn, 179 billion euros to EUR179bn, US$70 million to USD70mn

  3. Change “last week”, “this week”, “next week” to “the week ending d mmm yy”. eg last week ——> the week ending 1 Sep 19

I understand that all of the edits above, if made using a macro, would be prone to a lot of error. My plan is to track changes before I use the macro so I can still accept or reject each change. I'm also unsure about how feasible changing the weeks are - can I use dates in real-time?

  1. Format my paragraphs to fit a style quickly Let's say my work takes the structure of:

Style 1

Style 2

  • Body (Style 3)

  • Body (Style 3)

  • Body (Style 3)

Style 1

Style 2

  • Body (Style 3)

  • Body (Style 3)

  • Body (Style 3)

so on and so forth. The paragraphs in Style 1s are all correctly formatted to begin with, but what I have to do now is format the Style 2s and Style 3s. Could a Macro somehow detect that my Style 2 paragraphs are always below a Style 1 paragraph, and format it for me? And likewise for how my Style 3 paragraphs are always below a Style 2/another Style 3 paragraph.

Too many questions... Anyhow. Regarding the dates. It is not a simple macro as you want to change a lot of formats. Code to change from dd/mm/yyyy to dd mm yyyy:

Sub DateRepl()
    Dim Test As Boolean

    Selection.HomeKey Unit:=wdStory, Extend:=wdMove
    Test = True
    Do While Test
        With Selection.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = "([0-9]{1,2})[/]([0-9]{1,2})[/]([0-9]{4})"
            .Format = True
            .Forward = True
            .MatchWildcards = True
        End With

        Selection.Find.Execute Replace:=wdReplaceNone

        If IsDate(Selection.Text) Then
            Selection.Text = Format(Selection.Text, "dd mm yyyy")
            Selection.Collapse wdCollapseEnd
        Else
            Test = False
        End If
    Loop
End Sub

Regarding the currency items, it will be very hard as you have many chances (million, billion...).

Regarding the week substitution:

Dim Today, Workday
Today = Now
Workday = Weekday(Today)

With Selection.Find 
 .ClearFormatting 
 .Text = "last week" 
 .Replacement.ClearFormatting 
 .Replacement.Text = "the week ending " & DateAdd("d", -Workday, Now)
 .Execute Replace:=wdReplaceAll, Forward:=True, _ 
 Wrap:=wdFindContinue 
End With

With Selection.Find 
 .ClearFormatting 
 .Text = "this week" 
 .Replacement.ClearFormatting 
 .Replacement.Text = "the week ending " & DateAdd("d", 7-Workday, Now)
 .Execute Replace:=wdReplaceAll, Forward:=True, _ 
 Wrap:=wdFindContinue 
End With

With Selection.Find 
 .ClearFormatting 
 .Text = "next week" 
 .Replacement.ClearFormatting 
 .Replacement.Text = "the week ending " & DateAdd("d", 14-Workday, Now)
 .Execute Replace:=wdReplaceAll, Forward:=True, _ 
 Wrap:=wdFindContinue 
End With

Regarding the format, it is out of my knowledge.

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM