PDA

View Full Version : Removing numbers from word document via macro



neodjandre
11-23-2007, 03:53 AM
Hello,

I have a word document which I would like to sanitise . ie. remove all numbers denoted in ? or $ for example but I don't want to delete numbers for the headings and sub-headings.

is there a macro which is able to do that ?

thanks
andy

OTWarrior
11-23-2007, 04:34 AM
do you want to delete every number that is a currency? and the currency character?

are they in tables or within text?

PS: Welcome to the Forum :)

neodjandre
11-23-2007, 04:42 AM
OTWarrior

I want to delete every number and leave the currency intact . ideally I would like ?12,345 to be replaced with ?[x]

the numbers appear both in tables and text

obviously in tables and some other parts of the document they don't have the currency sign so i would like to replace in that case

12,345 with [x]

all these without affecting the header numbers ! very tricky i would say!

OTWarrior
11-23-2007, 05:13 AM
are you able to modify the original document? if so you could add bookmark references to each number and chnage it that way.

if not, then you would need to search for the currency sign, and remove everything until there is a space.

for the numbers without the currency symbol, you would need some way of uniquely identifying them.

for the currency in a table, it is a little bit easier, providing the tables (and where the currency value is) are always in the same position.

for example:

Sub replacetableval()
With ActiveDocument.Tables(1).Cell(1, 1)
.Range = Replace(.Range, (.Range), "?[x]")
End With
End Sub


this will (always) replace the first cell in table one with "?[x]", rather than serach for it. if that is suitable, I will convert it to work on the whole table (so long as you let me know which tables and which cells will contain the values)

I hope that is useful to you.

OTWarrior
11-23-2007, 05:41 AM
this code replaces every cell in table 1, doesn't matter how many columns or rows there are.

Sub replacetableval()
Dim i As Integer
Dim k As Integer
k = 0
i = 0
For k = 1 To ActiveDocument.Tables(1).Rows.Last.Index
For i = 1 To ActiveDocument.Tables(1).Columns.Last.Index
With ActiveDocument.Tables(1).Cell(k, i)
.Range = Replace(.Range, (.Range), "?[x]")
End With
Next i
Next k
End Sub

TonyJollans
11-24-2007, 01:41 AM
OTWarrior

I want to delete every number and leave the currency intact . ideally I would like ?12,345 to be replaced with ?[x]

the numbers appear both in tables and text

obviously in tables and some other parts of the document they don't have the currency sign so i would like to replace in that case

12,345 with [x]

all these without affecting the header numbers ! very tricky i would say!

When asking for this kind of thing in Word, terminology is critical. When you say "headers" do you mean Page Headers or do you really mean Headings? And if you mean Headings, how are they numbered? Are the numbers just typed in or have they been numbered by Word?

And what is "obvious" about the fact that in "tables and some other parts of the document they don't have the currency sign"?

If you could post a sample document it might be possible to provide a simple solution. Without one it's hard to know the best way to start.

fumei
11-24-2007, 07:26 AM
Hate to be a bugbear, but terminology is criticial, regardless. So is being specific and exact.

For example:

you mention 12 345, with a preceding pound symbol. Then mention, the same number is in a table.

Do you really mean the same number? Is there some logic you could use? Say look for, ?43,291 and know that there will be a 43,291 elsewhere.

In other words, can you combine logic? Find ?xxxxxx and use that number to find it else where.

Otherwise, as OTWarrior points out, you need some way (some logic) to determine how to uniquely identify them. Otherwise, how is your code to know that 41,678 should be made into ?[x], but 37,237 should not - if that is ever the case? Since WE don't know, and there is no sample document, it is a valid question.

If ALL numbers - besides what is in a heading (which I assume you mean, but Tony also has a valid point) - should be turned into ?[x], then that is fairly easy.