PDA

View Full Version : Find and Replace, sort of



samuelimtech
04-16-2014, 03:33 AM
Morning all,
I have a document (sample below) which contains specifications representes by M: or I: and sub specs in the body represented by (M:.x) or (I:.x)
I have a piece of code that will find these but if possible could someone modify it such that after the colon numbers are added in increments of ten and the sub specs also reflect the spec that they refer.
and example of how I would like to result is shown at the bottom.

thanks

With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.Wrap = wdFindStop
.MatchWholeWord = True
.MatchWildcards = True
.MatchCase = True
'Find expressions between matched pairs of double quotes,
'allowing for the fact that 'smart quotes' may not be in use.
.Text = "([MID]{1})([:]{1})([^t)]{1,})"
.Execute
End With

SAMPLE
M: If the link to the WOIF fails at any point while the connection to the remote Unit is established, the control must:


Discard any outstanding request. (M:.1)
Close the socket connection to the remote RCC. (M:.2)
Cancel the poll timer for the connection. (M:.3)
Cancel the poll received timer for the connection. (M:.4)



RESULT
M:10 If the link to the WOIF fails at any point while the connection to the remote Unit is established, the control must:


Discard any outstanding request. (M:10.1)
Close the socket connection to the remote RCC. (M:10.2)
Cancel the poll timer for the connection. (M:10.3)
Cancel the poll received timer for the connection. (M:10.4)

macropod
04-16-2014, 05:12 AM
Adding the '10' between the M: and whatever follows it is trivial - you don't even need a macro for that. However, you also refer to a requirement that "after the colon numbers are added in increments of ten". Your sample data give no indication of when such incrementing occurs or how one might differentiate one set of specs from the others.

samuelimtech
04-16-2014, 05:39 AM
Very true I thought this was cleaer than it apparently was,
SAMPLE
M: If the link to the WOIF fails at any point while the connection to the remote Unit is established, the control must:



Discard any outstanding request. (M:.1)
Close the socket connection to the remote RCC. (M:.2)
Cancel the poll timer for the connection. (M:.3)
Cancel the poll received timer for the connection. (M:.4)

M: The list of RCCs to which this RCC can generate remote WOIF access requests must be defined in Configuration Data.


The data for each RCC must include its Control Office Number, Control Office Name and WAN IP Address. (M:.1)


SAMPLE
M:10 If the link to the WOIF fails at any point while the connection to the remote Unit is established, the control must:



Discard any outstanding request. (M:10.1)
Close the socket connection to the remote RCC. (M:10.2)
Cancel the poll timer for the connection. (M:10.3)
Cancel the poll received timer for the connection. (M:10.4)


M: 20 The list of RCCs to which this RCC can generate remote WOIF access requests must be defined in Configuration Data.


The data for each RCC must include its Control Office Number, Control Office Name and WAN IP Address. (M:20.1)

samuelimtech
04-16-2014, 05:42 AM
Edit:
Very true I thought this was cleaer than it apparently was,
SAMPLE
M: If the link to the WOIF fails at any point while the connection to the remote Unit is established, the control must:



Discard any outstanding request. (M:.1)
Close the socket connection to the remote RCC. (M:.2)
Cancel the poll timer for the connection. (M:.3)
Cancel the poll received timer for the connection. (M:.4)

M: The list of RCCs to which this RCC can generate remote WOIF access requests must be defined in Configuration Data.


The data for each RCC must include its Control Office Number, Control Office Name and WAN IP Address. (M:.1)


RESULT
M:10 If the link to the WOIF fails at any point while the connection to the remote Unit is established, the control must:



Discard any outstanding request. (M:10.1)
Close the socket connection to the remote RCC. (M:10.2)
Cancel the poll timer for the connection. (M:10.3)
Cancel the poll received timer for the connection. (M:10.4)


M: 20 The list of RCCs to which this RCC can generate remote WOIF access requests must be defined in Configuration Data.


The data for each RCC must include its Control Office Number, Control Office Name and WAN IP Address. (M:20.1)

macropod
04-16-2014, 07:06 PM
It's still not clear where 'I' comes into the scenario. For the data you've provided, you could use:

Sub Demo()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "M:"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = True
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
Do While .Find.Found
If .Duplicate.Characters.First.Previous = vbCr Then i = i + 10
.InsertAfter i
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
Application.ScreenUpdating = True
MsgBox i / 10 & " specifications updated."
End Sub

samuelimtech
04-17-2014, 12:15 AM
the M in the text shown could either be an M or an I in the text, obviously if the if the lead is an I: then all the sub references in the text will also be an I.

macropod
04-17-2014, 12:24 AM
Try:

Sub Demo()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "[MI]:"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Execute
End With
Do While .Find.Found
If .Duplicate.Characters.First.Previous = vbCr Then i = i + 10
.InsertAfter i
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
Application.ScreenUpdating = True
MsgBox i / 10 & " specifications updated."
End Sub
Still not clear when the counter increases in a document with both I and M...

snb
04-22-2014, 06:59 AM
Sub M_snb()
y = 1
sn = Split(vbCr & ThisDocument.Content, vbCr & "M:")

For j = 1 To UBound(sn)
x = UBound(Split(sn(j), vbCr))
ThisDocument.Range(ThisDocument.Paragraphs(y).Range.Start, ThisDocument.Paragraphs(y + x).Range.End).Find.Execute "M:", , , , , , , , , "M:" & j * 10,2
y = y + x
Next
End Sub

macropod
04-22-2014, 02:43 PM
Sub M_snb()
y = 1
sn = Split(vbCr & ThisDocument.Content, vbCr & "M:")

For j = 1 To UBound(sn)
x = UBound(Split(sn(j), vbCr))
ThisDocument.Range(ThisDocument.Paragraphs(y).Range.Start, ThisDocument.Paragraphs(y + x).Range.End).Find.Execute "M:", , , , , , , , , "M:" & j * 10,2
y = y + x
Next
End Sub
Once again, snb, ignoring the user's clearly statement requirements. Will you ever learn to address those instead of just showing off???

fumei
04-22-2014, 04:11 PM
No kidding. snb it is REALLY annoying. Even if it was correct (which in this case it is not) it is super annoying to test because of your - all I can think of is that it is out and out laziness - habit of not declaring variables. Which means the rest of us who DO, have to either comment out Option Explicit or write in our own declarations.

I do want to test what you post because you clearly do have knowledge of VBA; why are you making it so bloody hard???? I want to see what you are doing but it is getting to the point that I will not because it is so annoying. Please either BE helpful or just stop posting.