Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find and Insert Character as well as replace special chars in Word 2010 1

Status
Not open for further replies.

sxschech

Technical User
Jul 11, 2002
1,033
US
Would like to know if it is possible to insert a character such as * in front of a sentence of a alphabetic bullet list in word 2010 and perhaps automate the process. I found out how to find what I'm looking for because the sentence is highlighted in yellow, so used the More, Format, Highlight in the Find and Replace Window.

[tt]Currently[/tt]
a. This is a list item.
[highlight #EDD400]b. Another list item.[/highlight]
c. Next list item.
d. Final list item.

[tt]Result after update[/tt]
a. This is a list item.
[highlight #EDD400]b. *Another list item.[/highlight]
c. Next list item.
d. Final list item.

Actually, what I'm trying to do is bring this into a plain text file and that will look like this when complete. Format is: (Letter.) (Tab) (Sentence.)

[pre]a. This is a list item.
*b. Another list item.
c. Next list item.
d. Final list item.
[/pre]

This is a laborious process that needs to be done on many files containing up to 120 questions and 4 answer choices a-d. Currently, been manually keying in a * after the file pasted into a plain text text editor by looking at the word document to identify which question letter should get the *.

Also need to identify and replace fraction symbols if they exist to a plain text equivalent.
 
Since the a., b., etc. are produced by automatic numbering, you cannot do what you want. You would have to convert the 'numbering' to plain text first.

Cheers
Paul Edstein
[MS MVP - Word]
 
Thanks, but that didn't seem to help (partly my fault?). I guess in my example I highlighted too much. It is actually like this:

a. [highlight #FCE94F]list item one.[/highlight]

rather than

[highlight #FCE94F]a. list item one.[/highlight]

So after I did what you suggested, it couldn't find the item because the tab was not highlighted.

I did notice that I could use find and replace to put the * at the end of the sentence of the highlighted text, but I don't know how I would move the * to the beginning.

a. [highlight #FCE94F]list item one.[/highlight]

after find and replace

a. [highlight #FCE94F]list item one.*[/highlight]

but how to get it to

*a. [highlight #FCE94F]list item one.[/highlight]
 
In that case, you could use a macro like:
Code:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .Highlight = True
    .Execute
  End With
  Do While .Find.Found
    .MoveStartUntil ".", wdBackward
    .Start = .Start - 2
    .InsertBefore "*"
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub

Cheers
Paul Edstein
[MS MVP - Word]
 
Not sure if this is would be a new thread as a variation of above...
Have some new files and instead of Highlighted Answers, they are bold. I thought I could modify the code to search for Bold, but didn't work as expected. Seemed to put asterisks in randomly throughout the document regardless of being bold or not. I italicized the line that I changed.

Code:
Sub BoldStar()
ActiveDocument.ConvertNumbersToText
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    [i].Font.Bold = True[/i]
    .Execute
  End With
  Do While .Find.Found
    .MoveStartUntil ".", wdBackward
    .Start = .Start - 2
    .InsertBefore "*"
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub

Also, these files seems to have both the question and correct answer in bold, so I'm thinking that the code would probably end up putting * in both places, if so, in anticipation, I tried playing around with regex and came up with this using the macro recorder, but not sure if will be needed. If so, how best to incorporate into the code as a call from the sub or would it need to be modified and placed into the sub?

Code:
Sub RegStar()
'
' RegStar Macro to remove stars in bolded lines that are questions
'and begin with a number rather than a letter between A and D.
'
    Selection.WholeStory
    Selection.Find.ClearFormatting
    Selection.Find.Font.Bold = True
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Font.Bold = True
    With Selection.Find
        .Text = "(\*)([0-9])"
        .Replacement.Text = "\2"
        .Forward = True
        .Wrap = wdFindAsk
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub
 
The code I posted is dependent on where the highlighting/bold starts. Obviously:
a. list item one.
isn't the same as:
a. list item one.
and:
a. list item one.
isn't the same as:
a. list item one.
You can handle both variants, though, by changing:
.MoveStartUntil ".", wdBackward
.Start = .Start - 2
to:
.Start = .Paragraphs.First.Range.Start


Cheers
Paul Edstein
[MS MVP - Word]
 
Thanks for the modification. Too bad I'm not up to speed on Word VBA as I've worked mainly with Access VBA and this is my first time using word vba. There must be some other issue I'm overlooking because while now it is putting the asterisk at the beginning, it is doing it for multiple answers within a question regardless of being bold or not. I changed the long sentences to "Question" and "Answer" so that I'm not posting the actual questions. I turned on the show paragraph marks to see if there was something different, but looks like there is a paragraph marker between each line. I included the first five because each one is different. Question 3 did not contain a period in the source document, so wasn't that I forgot to type it in case that has an impact on the code.
[tt]
*1. Question:
*A. Answer 1.
B. Answer 2.
C. Answer 3.
*D. Answer 4.

2. Question:
A. Answer 1.
B. Answer 2.
*C. Answer 3.
*D. Answer 4.

3. Question:
A. Answer 1.
*B. Answer 2
C. Answer 3.
*D. Answer 4.

4. Question:
*A. Answer 1.
B. Answer 2.
C. Answer 3.
*D. Answer 4.

*5. Question:
A. Answer 1.

B. Answer 2.
C. Answer 3.
*D. Answer 4.
[/tt]
 
The only way I can see that behaviour being accounted for is if your questions and their answers are not all in separate paragraphs (e.g. some are joined by line breaks instead of being separated by paragraph breaks). Since I can't see your actual document, I can't really comment further on that aspect, since the code works fine for me otherwise.

To prevent the questions themselves being bolded, you could change:
.InsertBefore "*"
to:
If Not IsNumeric(.Characters.First) Then .InsertBefore "*"

Cheers
Paul Edstein
[MS MVP - Word]
 
The following, which incorporates all the mods that have been discussed so far, works fine for me:
Code:
Sub Demo()
Application.ScreenUpdating = False
ActiveDocument.ConvertNumbersToText
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .Font.Bold = True
    .Execute
  End With
  Do While .Find.Found
    .Start = .Paragraphs.First.Range.Start
    If Not IsNumeric(.Characters.First) Then .InsertBefore "*"
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub

Cheers
Paul Edstein
[MS MVP - Word]
 
That is fantastic. Thanks.

Too bad that I still have to manually update the files that don't have bold or highlighting as the answer keys are in a separate word doc file.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top