PDA

View Full Version : Changing values in Word - Macro



ilan_buskila
12-02-2012, 02:03 AM
Hi,
I have a word file that looks like that:
T1 63.5000-61.5000 p tower new P0
T2 61.5000-59.5000 p tower new P0
T3 59.5000-53.5500 P2 D3''
T4 53.5500-47.6000 P2 D3''
T5 47.6000-41.6500 P2 D3''
T6 41.6500-35.7000 P3 D4''
T7 35.7000-29.7500 P4 D2''
T8 29.7500-23.8000 p tower new P5 D2.5''
T9 23.8000-17.8500 P6 D2.5''
T10 17.8500-11.9000 p tower new P7 D2.5''
T11 11.9000-5.9500 p tower new P8 D2.5''
T12 5.9500-0.0000 p tower new P9 D3''

I would like to write a Macro which will search the first T1, look for the P value in the same line and then replace the T with the right P. this should be done for all T's and P's
for example,

T1 should be replaced with Po
T8 should be replaced with P5 D2.5"

Thanks.
Ilan.

gmaxey
12-02-2012, 08:56 AM
Iana,

"I would like to write a macro ...." usually requires you open the VB Editor and begin to write code. When you get stuck then you post what you tried here and most contributors are happy to assist.

Since this appears to be your first post, I'll be gently :-)

Try something like:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Dim lngIndex As Long
Dim arrFind() As String
Dim arrReplace() As String
arrFind = Split("T1|T8", "|")
arrReplace = Split("PO|P5 D2.5", "|")
For lngIndex = 0 To UBound(arrFind)
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = arrFind(lngIndex)
.Replacement.Text = arrReplace(lngIndex)
.MatchWholeWord = True
.Execute Replace:=wdReplaceAll
End With
Next lngIndex
End Sub

ilan_buskila
12-02-2012, 02:04 PM
Hi Greg,
I see that this is match more complicated than I imagined.
until now i did some macros in excel and it was easier. I guess i need to learn some VB from the beginning.
any way, thanks allot.
Ilan.

macropod
12-02-2012, 05:06 PM
It seems to me you don't even need a macro. Try a wildcard Find/Replace, where:
Find = (T[0-9]{1,}>)([!P]@)(P[!^13]{1,})
Replace = \3\2\3