PDA

View Full Version : Bug in my coding



Rakesh
05-01-2011, 09:57 AM
Hi Rocks,

I have the following code to insert some text at the beginning of the para based on the stylesheet naming. But it shows an error when the document does not used the stylesheet which is mentioned in the Coding.

Sub aa()
With ActiveDocument.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.Style = ActiveDocument.Styles("SOI_P_H1")
.Text = "([!^13]@[^13])"
.Replacement.Text = "@SI-major entry hd:\1"
.Forward = True
.Format = True
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
With ActiveDocument.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.Style = ActiveDocument.Styles("SOI_P_H2")
.Text = "([!^13]@[^13])"
.Replacement.Text = "@SI-minor entry hd ko:\1"
.Forward = True
.Format = True
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
With ActiveDocument.Content.Find
.Style = ActiveDocument.Styles("SOI_P_LEV1")
.Text = "([!^13]@[^13])"
.Replacement.Text = "Level2:\1"
.Forward = True
.Format = True
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
With ActiveDocument.Content.Find
.Style = ActiveDocument.Styles("SOI_P_LEV2")
.Text = "([!^13]@[^13])"
.Replacement.Text = "Level3:\1"
.Forward = True
.Format = True
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
With ActiveDocument.Content.Find
.Style = ActiveDocument.Styles("SOI_P_LEV3")
.Text = "([!^13]@[^13])"
.Replacement.Text = "Level4:\1"
.Forward = True
.Format = True
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
With ActiveDocument.Content.Find
.Style = ActiveDocument.Styles("SOI_P_STACK")
.Text = "([!^13]@[^13])"
.Replacement.Text = "Stack:\1"
.Forward = True
.Format = True
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub


How to fix it?

Herewith I have attached sample doc to run the coding.

Thanks,
Rakesh

Frosty
05-01-2011, 10:40 AM
It's not really a bug in your coding... you have to decide how you want to proceed when a specific style doesn't exist which your code relies on. Try the following:

Sub aa()
'change the first number to indicate how many different elements to find/replace
Dim aryFindReplace(5, 1) As String
Dim i As Integer
Dim styFind As Style
Dim lErr As Long

'set up the style name to search for, and the replacement text in an array
aryFindReplace(0, 0) = "SOI_P_H1"
aryFindReplace(0, 1) = "@SI-major entry hd:\1"

'style name & replacement text
aryFindReplace(1, 0) = "SOI_P_H2"
aryFindReplace(1, 1) = "@SI-minor entry hd ko:\1"

aryFindReplace(2, 0) = "SOI_P_LEV1"
aryFindReplace(2, 1) = "Level2:\1"

aryFindReplace(3, 0) = "SOI_P_LEV2"
aryFindReplace(3, 1) = "Level3:\1"

aryFindReplace(4, 0) = "SOI_P_LEV3"
aryFindReplace(4, 1) = "Level4:\1"

aryFindReplace(5, 0) = "SOI_P_STACK"
aryFindReplace(5, 1) = "Stack:\1"

'now loop through the array and perform the searches
For i = 0 To UBound(aryFindReplace)
With ActiveDocument.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
On Error Resume Next
Set styFind = ActiveDocument.Styles(aryFindReplace(i, 0))
lErr = Err.Number
On Error GoTo 0
'if the style doesn't exist in the document-- decide what to do
If lErr <> 0 Then
MsgBox "Style: " & aryFindReplace(i, 0) & " doesn't exist!", vbInformation, "Hiya!"
Err.Clear

'otherwise, keep going
Else
.Style = styFind
.Text = "([!^13]@[^13])"
.Replacement.Text = aryFindReplace(i, 1)
.Forward = True
.Format = True
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End If
End With
Next
End Sub

And please remember to use the VBA tags... makes it so much easier to read.

Rakesh
05-01-2011, 11:14 AM
Hi Frosty,

Thanks a bunch. Its working.

Regards,
Rakesh

Frosty
05-01-2011, 11:23 AM
No problem. There's a small problem with my above code... Should reset error trapping to "On Error GoTo 0" or you run the risk of ignoring all other errors too. Without knowing more, I can't really tell you where the best place to put error trapping, but at the least check the edit and use that so that you are only handling this one particular error.

Rakesh
05-01-2011, 11:26 AM
OK Fine Thanks Frosty

macropod
05-02-2011, 12:58 AM
I'd take a slightly different approach:
Sub Demo()
Application.ScreenUpdating = False
Dim StrFndSty As String, StrRep As String, i As Integer
'set up the style name to search for, and the replacement text in string 'arrays'
StrFndSty = "SOI_P_H1,SOI_P_H2,SOI_P_LEV1,SOI_P_LEV2,SOI_P_LEV3,SOI_P_STACK"
StrRep = "@SI-major entry hd:\1,@SI-minor entry hd ko:\1, Level2:\1,Level3:\1,Level4:\1,Stack:\1"
With ActiveDocument
'now loop through the 'array' and perform the searches
For i = 0 To UBound(Split(StrFndSty, ","))
On Error Resume Next
With .Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.Style = Split(StrFndSty, ",")(i)
.Text = "([!^13]@[^13])"
.Replacement.Text = Split(StrRep, ",")(i)
.Forward = True
.Format = True
.MatchWildcards = True
If Err.Number <> 0 Then
'if the style doesn't exist in the document-- decide what to do
MsgBox "Style not found: " & Split(StrFndSty, ",")(i), vbInformation, "Error Trap"
Err.Clear
Else
.Execute Replace:=wdReplaceAll
End If
End With
Next
End With
Application.ScreenUpdating = True
End Sub
The main difference is that I've used comma-delimited strings to hold the Style names and replacement text. These can be expanded/contracted without having to adjust the array size and without having to add/delete lines for each array element. The downside is they're not as easy to read.

Frosty
05-02-2011, 08:48 AM
I like your approach, Paul. I knew I was missing something easier. The only difference is I'd still use a variable to hold the results of the two splits, if only for trouble-shooting.

Also, does Split also do a Trim? I forget. If it doesn't, there are tough to recognize typo possibilities with the odd extra space after a comma.

And do you know if the wildcard search "bad karma" bug has been fixed since Word 2003?

macropod
05-02-2011, 02:20 PM
Hi Frosty,

Splits don't trim what they're given. If you want to do that, you can apply the Trim function.

wildcard search "bad karma" bug
I've never experienced it, and I've used Word 2007 extensively for developing wildcard-based Find/Replace operations.