geeky notes on web (drupal) and .net (c#) programming, whichever prevails at the moment

 

All commits tagged "vb"

 

Search and process all numbers in a word document (vba word macro)

My wife has had a mindblowingly tedious task at her job: to lower all the prices by 10% in her word file (some sort of a tourist agency price list document). The document was about 70 pages long with numbers scattered all around the document: in paragraphs as well as in tables, so effectively exporting it to excel was not an option.

The thing I've noticed that gave me the idea of trying VBA macros, was that the numbers to be cut by 10% had the format of XXXX.XX, like 500.00, so I decided to google it and compile a VB macro that could probably be of some (future) use too.

When the document was processed in only couple of seconds, it was one of those moments when I felt that what I did was some kind of magic: "How nice it is to have a programmer by my side!" :) This piece of code saved her a day or two of her life.

Sub decrease10()

Set objWdDoc = ActiveDocument
Set objWdRange = ActiveDocument.Content
Do
    With objWdRange.Find
        .MatchWildcards = True
        .Text = "[0-9]{1;5},[0-9]{2}"
        bFound = .Execute
        If Not bFound Then
            Exit Do
        End If
        .Execute Replace:=wdReplaceOne, replacewith:=Format(Round(objWdRange.Text * 0.9 + 0.0000001, 2), "Standard")
        Set objWdRange = objWdDoc.Range(objWdRange.Start + 6, objWdDoc.Range.End)
    End With
Loop
End Sub

}