Change style name without changing anything else?

E

Ed

I'd like to search a document to certain text strings and mark them somehow
in a way that is invisible to the reader. I thought it would be nice if I
could change the style name to "FOUND" or some such thing, but I'd have to
do it without changing any of the current style's formatting
characteristics. Or I'd have to pick up each characteristic - font, size,
bold, underline, color, paragraph, etc. and apply it to my FOUND style at
each instance.

Is there any way to do this? Or is there a better way to mark the text?

Ed
 
D

Dave Lett

Hi Ed,

The problem with styles is that you might find your text in more than one style; therefore, you'd have to have a new "Found" style for each style you find your text in.

You might want to consider hidden bookmarks. The following is a sample:

Dim iCount As Integer
iCount = 1
With Selection.Find
.Text = "text"
Do While .Execute
ActiveDocument.Bookmarks.Add _
Name:="_Found" & iCount, _
Range:=Selection.Range
iCount = iCount + 1
Loop
End With


Of course, you _might_ want to change the starting number for iCount if you've already run the routine on the document.

HTH,
Dave
 
J

Jonathan West

Hi Ed,

You could create a character style, whose format is Default Paragraph Font
plus nothing. You can then apply that style to those places you want to
mark. Provided that no manual fornt formatting had been applied, this would
have no visible effect on the text at all.
 
E

Ed

Thank you, Jonathan. That sounds like what I was thinking of. If, though,
manual formatting has been applied (which is most likely the case if it's
anything other than default, because *no one*! here uses styles - it's all
Ctrl+B, Ctrl+U, AllCaps, etc.), is it a big pain to detect and apply to new
text? (I'd rather do it the easiest way, but I don't my users - who all
outrank me! - screaming because I took their emphasis off or made a header
look like plain body text.)

Ed
 
E

Ed

Hi, Dave.

Dave Lett said:
Hi Ed,

The problem with styles is that you might find your text in more than one
style; therefore, you'd have to have a new "Found" style for each style you
find your text in.

That's why I was asking about picking up the formatting of the found text
and applying it to my "Found" style. It seems simple - redefine style based
on text, which is a built-in function. But I get these wild ideas and have
no clue as to the trouble I'm letting myself into!
You might want to consider hidden bookmarks.

The only concern I have with a bookmark is that if my "mark" is invisible to
the reader, and the reader edits the docuemnt, it's possible for the
bookmark to get deleted, right? And there goes the function I was going to
build on these!
The following is a sample:

Dim iCount As Integer
iCount = 1
With Selection.Find
.Text = "text"
Do While .Execute
ActiveDocument.Bookmarks.Add _
Name:="_Found" & iCount, _
Range:=Selection.Range
iCount = iCount + 1
Loop
End With


Of course, you _might_ want to change the starting number for iCount if
you've already run the routine on the document.
HTH,
Dave

Thanks again.
Ed
 
K

Klaus Linke

Hi Ed,

What's your reason for marking up the text?
I think the "best" way would possibly depend on what you want to do with the
marked-up text.

Regards,
Klaus
 
E

Ed

Thanks for the reply, Klaus. It's a way for me to search through a report
and flag things that might need updating in a later version, or that need to
be cross-checked and verified, and so forth. If I could do a Find and
Replace with some kind of tag that the reader would not see either in print
or on a monitor, and could not be accidentally deleted (that was why I shied
away from a bookmark or comment), it could make my life a bit easier.

Ed
 
B

Bruce Brown

Ed, one simple way to mark text invisibly so it can't be seen in print
or on the screen: select the text and turn it into a doc variable. It
will then live inside the document, where users can't see or touch it.

Later on you just search for it and do your thing. It doesn't matter
if or how users have re-formatted the text, as long as it's there when
you go searching for it.

The first macro sets up a doc variable with text you've selected. The
other finds the text block hidden in each doc variable. There's plenty
of room to customize the second one to suit your purposes. - Bruce


Sub SetUpVs()
If Selection.Type = wdSelectionIP Then
MsgBox "Quitting.", , "Nothing Selected"
Exit Sub
End If
Dim V As Variable, Vs As Variables, Str As String
Set Vs = ActiveDocument.Variables
MsgBox Vs.Count, , "Hidden Text Count"
Str = Selection.Range.Text
Vs(Str).Value = Str
MsgBox Vs.Count, , "New Hidden Text Count"
End Sub


Sub FindVs()
Dim V As Variable, Vs As Variables, R As Range, Ans As Byte
Set Vs = ActiveDocument.Variables
MsgBox Vs.Count, , "Hidden Text Count"
Set R = ActiveDocument.Range
For Each V In Vs
With R.Find
On Error GoTo EndLoop
.Text = V.Value
Do While .Execute
R.Select
Ans = MsgBox(V.Value & ". " & vbCr & "Continue?", vbYesNo,
"Hidden Text Found")
R.Collapse wdCollapseEnd
If Ans <> 6 Then Exit Sub
Loop
EndLoop:
Set R = ActiveDocument.Range
End With
Next
End Sub
 
B

Bruce Brown

Had a simpler idea.

Instead of messing around with with styles or doc variables, why not
change the font color to black?

Text in black font will survive any kind of re-formatting, even
highlighting, as long as the font color itself is not changed.

The advantage is that, even though black font isn't hidden like doc
variables, the user has no real *incentive* to change it back to
automatic color, since both look exactly the same and changing the
color back to automatic would involve extra work, namely, finding
where the black font begins and ends.

You're in trouble if a user deletes the entire selection, naturally.
But as long as only *one character* of the original selection remains
in black font, you can find it again. With the doc variables method,
if the user changes so much as a comma, the text is beyond retrieval.
- Bruce


Sub CreateBlack()
If Selection.Type = wdSelectionIP Then
MsgBox "Quitting.", , "Nothing Selected"
Exit Sub
End If
Selection.Range.Font.Color = wdColorBlack
MsgBox "Selection now in black font.", , "Changed to Black"
End Sub



Sub FindBlack()
Dim R As Range, Ans As Byte
Set R = ActiveDocument.Range
With R.Find
.Font.Color = wdColorBlack
Do While .Execute
R.Select
Ans = MsgBox("Found hidden text. Continue?", vbYesNo, "Black
Font")
R.Collapse wdCollapseEnd
If Ans <> 6 Then Exit Sub
Loop
End With
End Sub
 
E

Ed

*That* is a great idea! It's so obvious, and it's so easy. That's probably
why I missed it altogether!

Thank you!
Ed
 
K

Klaus Linke

Hi Ed,

Bookmarks seem a good choice in your case.

You might also try { PRIVATE } fields as an alternative. You can put any
text or private tags you want in them, if you need to keep some sort on
info.
If you need to keep track of ranges, you'd need fields at the start and at
the end of the range, though.
And they get about as easily clobbered as bookmarks (deleted, overwritten,
copied/pasted somewhere else...) if someone edits the document.

Hidden text may not be a good idea, because it can confuse users who don't
know about it terribly.
If they start typing at a point where there is some hidden text, the
keyboard will seem to be dead, and you'll end up with a document with lots
of trash typed in.

Bruce's idea to use color "black" sounds also like a good idea, if you don't
need to keep any info (like you can store in a bookmark name or private
field).

Greetings,
Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top