PDA

View Full Version : achieve same result as find dialog



saban
10-22-2007, 10:08 AM
I want to find and select all instances of bold text in document how do I do that ??

the same as find function does

fumei
10-22-2007, 10:59 AM
If you want to search for all words that are bold, then search for words. Declare a Variant object and use it with the document Range.Words.

If you want to search for all paragraphs that are bold, then search for paragraphs. Declare a Paragraph variable, and use the document Paragraphs.

saban
10-22-2007, 11:07 AM
could you point me to the right direction because I would like this to be fast as find dialog box which selects all bold text in a second

i need all bold text to be selected at the same time so I can apply reset characters to selections

saban
10-22-2007, 11:40 AM
Sub Macro1()
For Each Character In ActiveDocument.Characters
Selection.Find.ClearFormatting
With Selection.Find.Font
.Bold = True
.Italic = True
.Hidden = False

End With
Selection.Find.Execute
Selection.Font.Reset
Selection.Font.Bold = wdToggle
Selection.Font.Italic = wdToggle
Selection.Collapse direction:=wdCollapseEnd
Next
end sub

How do I make this stop ?? (it goes into a loop)

TonyJollans
10-22-2007, 12:42 PM
I want to find and select all instances of bold text in document
The code you posted needs some attention but appears to be trying to look for bold, italic, text and then removing all manual formatting from the font and then toggling bold and italic so, depending on the style of the found text it may end up bold, italic, both, or neither.

So what do you actually want - and this is not a moot point, the code you need depends on your answer.

FWIW, the loop is what you have coded:

For Each Character In ActiveDocument.Characters

(even though you never use Character inside the loop)

saban
10-22-2007, 12:57 PM
I need all bold text to be reseted (ctrl+space) and then putted back to original bold beacuse I have some trados issue when translating and it is the only workaround


What would I need to put in the code for the loop to exit when last bold word in document is found

Thnx very much fo your replies

Just one Q:
How can I assign selection.find.font to a variable??

TonyJollans
10-22-2007, 04:02 PM
Dim Rng As Range
Set Rng = ActiveDocument.Range.Duplicate
With Rng.Find.Font
.Bold = True
.Italic = True
.Hidden = False
End With
While Rng.Find.Execute
With Rng.Font
.Reset
.Bold = True
.Italic = True
End With
Rng.Collapse wdCollapseEnd
Wend


I'm not sure how this will help you with anything, but ...

Dim MyVariable As Font
Set MyVariable = Selection.Find.Font.Duplicate

saban
10-23-2007, 01:49 AM
Thnx for your help will let you know how it worked

saban
10-23-2007, 04:37 AM
I dont get this this one loops and does not stop:why ????

Sub normalni()
Dim rng2 As Range
Set rng2 = ActiveDocument.Range.Duplicate
With rng2.Find.Font
.Bold = False
.italic = False
.Hidden = False
.Underline = False
.Name = "Times New Roman"
End With
While rng2.Find.Execute
With rng2.Font
.Reset
.Bold = False
.italic = False
End With
rng2.Collapse wdCollapseEnd
Wend

But this one does stop as it should
Dim Rng As Range
Selection.HomeKey Unit:=wdStory
Set Rng = ActiveDocument.Range.Duplicate
With Rng.Find.Font
.Bold = True
.italic = True
.Hidden = False
.Name = "Times New Roman"
.Underline = False
End With
While Rng.Find.Execute
With Rng.Font
.Reset
.Bold = True
.italic = True
End With
Rng.Collapse wdCollapseEnd
Wend
Do you have any idea why does this goes into a loop (it does not stop)

And if I put some character which is bold and underlined L at the end of document then it stops why is that??

TonyJollans
10-23-2007, 08:52 AM
It really needs a little more error checking.

The reason, I suspect is that the final paragraph mark matches the search and is not changed by the replace - the collapse repositions the start of the range immediately before it (because it can't go after it) and finds it again and sets it back as it was and collapses before it and finds it again ...

Changing it slightly shoud fix it ...


Dim rng2 As Range
Set rng2 = ActiveDocument.Range.Duplicate
With rng2.Find.Font
.Bold = False
.Italic = False
.Hidden = False
.Underline = False
.Name = "Times New Roman"
End With
Do While rng2.Find.Execute
With rng2.Font
.Reset
.Bold = False
.Italic = False
End With
If rng2.End = ActiveDocument.Range.End Then Exit Do
rng2.Collapse wdCollapseEnd
Loop



The one which 'works' should really have some failsafe logic as well.

saban
10-23-2007, 12:56 PM
thnx man I really appreciate your help

(let me know if you need anything from me maybe something for Navigation systems (PNA or PDA) I am good at it:))

TonyJollans
10-24-2007, 12:20 AM
My pleasure :)

saban
10-24-2007, 01:04 AM
sorry to bother you but do you have any idead why this still goes into a loop

Sub test2()
Dim rng As Range
Set rng = ActiveDocument.Range.Duplicate
With rng.Find.Font
.Bold = False
.italic = True
.Hidden = False
.Underline = False
.Name = "Times New Roman"
End With
Do While rng.Find.Execute
With rng.Font
.Reset
.Bold = False
.italic = True
End With
If rng.End = ActiveDocument.Range.End Then Exit Do
rng.Collapse wdCollapseEnd
Loop

Thnx

TonyJollans
10-24-2007, 02:36 AM
Not immediately. Can you post a document it loops on?

saban
10-24-2007, 05:09 AM
why does sometimes also resets hidden text even if I set .hidden = false

Any ideas

TonyJollans
10-24-2007, 05:31 AM
Again, I don't know. Can you post a document it happens on?

Processing of hidden text can be difficult to control but if you set ..

rng.TextRetrievalMode.IncludeHiddenText = False

then it should not be included. The setting defaults to whatever is shown on screen when you run the macro so if you have hidden text shown, it will be set to be included in rng - if you don't it won't but explicitly setting it false should mean it is always excluded.

saban
10-24-2007, 08:06 AM
could you plizz check this doc why is it going into a loop

In zip is also a macro I use
(when you stop the code you can also see that it has reseted some of the hidden text)

Thnx

TonyJollans
10-30-2007, 05:56 PM
I've just had a very quick look and the loop seems to be being caused by the end of cell marker in one of the tables. I'll try and have a better look tomorrow

TonyJollans
10-31-2007, 01:13 AM
OK, what is happening is ...

There are two runs of text that satisfy the search criteria: The first happens to be in a table cell but that is not relevant.

The second, and last, is a complete table cell including the end of cell mark, and the cell does not contain a paragraph mark, other than the one lumped in with the end-of-cell mark.

When the text is found, the (found) range is set to the entire contents of the cell but *not* including the end-of-cell mark. The changes are made and the range collapsed. The collapsed range is inside the cell just before the end of cell mark.

Now, this is what I do not understand, or certainly do not like --- the next execution of the Find returns True (i.e Found). It does not move the Range so it remains an insertion point just before the end of cell mark. The changes are made (although they have no effect) and the range is collapsed - well, actually it was already collapsed, so it remains where it was --- and off it goes again, round and round for ever.

What to do about it? At the moment I don't know.

Are there any other situations that may cause the same loop? Again, I don't know.

I will ponder on it.

saban
10-31-2007, 07:18 AM
that is strange ?
so if I understand it right it wont get out of table??

Thnx

TonyJollans
10-31-2007, 10:36 AM
Yes you understand correctly.

From a quick test it looks as though it *might* work correctly with the Selection instead of a Range (but doing so may require a few more changes to the code)

Unless someone can point me to some documentation that explicitly says that the behaviour is different I would say this was a bug - not that that really helps you a lot.

I am surrounded by problems today and I'm trying to look at it in between doing other things. I will report back when I can.

fumei
10-31-2007, 11:39 AM
The userform in TWBformat.dot uses Activate rather than Initalize for code. Further, Activate ends with Unload Me.

In other words, the userform appears then immediately unloads. It blinks into existence then is gone. Why bother?

Also, I do not understand the use of Duplicate. Why are you using Duplicate?

TonyJollans
10-31-2007, 12:26 PM
I suspect he's using Duplicate because that is what I coded much earlier in the thread - it's never a bad thing in my book to have Range objects that are your own as you can sometimes get caught out with those defined by Word not getting redefined exactly as you might expect.

TonyJollans
10-31-2007, 12:27 PM
There's a UserForm? :)

fumei
10-31-2007, 12:38 PM
Yes, in the file DWBformat.dot in the ZIP file.

Hmm, as we working/looking at different files?

The problem and macro.zip file contains two files: problem.doc and TWBformat.dot

What are you looking at?

TonyJollans
10-31-2007, 12:48 PM
I was only joking. Yes, we are looking at the same thing - I had just ignored the UserForm and was looking at the F&R code in Module1 that (I presumed) was looping.

I have had the two files (one document and one template) open for more than a day and, not half an hour ago, everything started working. Just when I thought I could see what was wrong (if not how best to work around it), suddenly it wasn't. What's that quote? "Word rarely misses an opportunity to perplex".

TonyJollans
10-31-2007, 02:01 PM
There is something really kooky about this. At one point it seemed to be working but I don't know what I did to trigger that. Closing and re-opening Word made it fail again.

The best I can suggest is that after collapsing the range you check if it is an end of cell and if so move on. This isn't really satisfactory but may work for you ...


' ....
While rng.Find.Execute
With rng.Font
.Reset
.Bold = True
.Italic = True
End With

rng.Collapse wdCollapseEnd
If rng.Characters(1) = Chr(13) & Chr(7) Then rng.Move wdCharacter, 1
Wend
' ....

saban
11-05-2007, 06:40 AM
thnx man will try it and let you know

haxsaw
12-03-2007, 12:50 PM
Here's my correction to end the loop. I'm using the code to replace section breaks with page breaks in a mail merged file.


Sub SectionStripper()
For Each Character In ActiveDocument.Characters
Selection.Find.ClearFormatting
With Selection.Find
.Text = "^b"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
If .Execute = True Then
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.InsertBreak Type:=wdPageBreak
Else
Exit For
End If
End With
Next
End Sub

TonyJollans
12-03-2007, 12:57 PM
If you have a question, please start a new thread.

haxsaw
12-03-2007, 01:54 PM
No question here.

I've found some valuable ideas, insight and help in these threads and thought I had an option to a question someone asked above that I didn't see presented in the other responses. I thought I'd offer my 2 cents.

TonyJollans
12-03-2007, 02:36 PM
No problem - I just wasn't sure from your post :)