PDA

View Full Version : Solved: Probably a simple QA



sassora
03-27-2008, 12:35 PM
I made some code to find words, make them bold and put a hyphen after them. I messed around with it and now it doesn't work. The error talks about end with. I'm sure it's something small but I can't spot what it is!

Here is the code

Sub sd()
Dim iCount As Integer
Dim i As Integer
Dim j As Integer
Dim sResponse(5)
sResponse(1) = "Enjoying and achieving"
sResponse(2) = "Being Healthy"
sResponse(3) = "Staying Safe"
sResponse(4) = "Positive Contribution"
sResponse(5) = "Organisation"
j = 1
Do Until j = 5
If sResponse(j) > "" Then
' Set the counter to zero for each loop
iCount = 0
Application.ScreenUpdating = False
With Selection
.HomeKey Unit:=wdStory
With .Find
'.ClearFormatting
.Text = sResponse(j)
' Loop until Word can no longer
' find the search string and
' count each instance
Do While .Execute
iCount = iCount + 1
Selection.MoveRight
Loop
End With
'Puts in the bold names and inserts a hyphen
For i = 1 To iCount
Selection.Find.ClearFormatting
With Selection.Find
.Text = sResponse(j)
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute
Selection.Font.Bold = wdToggle
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeText Text:=" - "
Next i
Application.ScreenUpdating = True
End If
j = j + 1
Loop
End Sub

Any ideas?

Tinbendr
03-27-2008, 02:46 PM
.......



Application.ScreenUpdating = True

End With '<-Right here

End If
j = j + 1
Loop

End Sub

fumei
03-28-2008, 11:34 AM
"I made some code to find words, make them bold and put a hyphen after them."

Here is perhaps a more efficient way, and shorter so you can see what is happening.
Sub yadda()
Dim r As Range
Dim sResponse()
Dim j As Long
sResponse = Array("Enjoying and achieving", _
"Being Healthy", "Staying Safe", _
"Positive Contribution", "Organisation")

For j = 0 To UBound(sResponse)
Set r = ActiveDocument.Range
With r.Find
Do While .Execute(findtext:=sResponse(j), _
Forward:=True) = True
With r
.Text = r.Text & " - "
.Font.Bold = True
.Collapse Direction:=wdCollapseEnd
End With
Loop
End With
Next
End SubWhat it does:

1. fills array
2. makes range object of document
3. starts loop through array
4. searches Range for array string
5. if found, it adds a hypen and makes the found string (and hypen) Bold
6. the range is collapsed so it can continue (otherwise it just finds itself!)
7. continues looking for current search string (repeating 5. and 6. if found)
8. if not found, it exits the Do loop
9. and goes to the Next item in the string array

The end.

Notice there is no use of Selection (e.g. Selection.MoveRight ).

...nor use of any separate counter...(' Set the counter to zero for each loop)

fumei
03-28-2008, 11:53 AM
Here is a demo. I made it so you can toggle back and forth. Click "Yadda" on the top toolbar.

"Enjoying and achieving" becomes "Enjoying and achieving - " and Bold.

Click "Yadda" again, and all them become shorter (without the hypen) and NOT bold.

The difference to the code I posted previously is:
Public Maybe As Boolean
Sub yadda()
If Maybe Then
UnBoldIt
Else
BoldIt
End If
End Sub

with the procedure Sub BoldIt() making the Boolean Maybe True, and the procedure UnBoldIt() making Maybe False.

In other words, if Maybe is TRUE (the items are bolded with a hypen), then unbold things; if it is FALSE (items are not bolded and have no hypen), then bold them and add the hypen.

sassora
03-28-2008, 03:41 PM
Thanks, I'm ok at Excel VBA but less so at word VBA so thanks for breaking things down :)