#1
|
|||
|
|||
Find text, format part of text in italic
I would like to create code (in MS Word 2007) that searches a document, finds some text, and changes the formatting of part of that text. More specifically:
Find: Plessy v. Ferguson, 163 US 537 Replace: Plessy v. Ferguson, 163 US 537 Essentially, I want to remove the italics from the comma. However, I only want to remove the italics if the text before the comma is also italic, and the text after the comma is not italic. Thanks for any help. |
#2
|
||||
|
||||
What about if neither of the text before/after is italic? Assuming you don't want italics for the comma then either, try:
Code:
Sub Demo() Application.ScreenUpdating = False With ActiveDocument.Range With .Find .ClearFormatting .Replacement.ClearFormatting .Text = "[!0-9],*>" .Replacement.Text = "" .Forward = True .Wrap = wdFindStop .Format = False .MatchWildcards = True .Execute End With Do While .Find.Found If .Words.Last.Characters(1).Font.Italic = False Then .Characters(2).Font.Italic = False End If .Collapse wdCollapseEnd .Find.Execute Loop End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
You are awesome. Thanks. I can't try it on my docs until I return to work, tomorrow. I'll let you know.
|
#4
|
|||
|
|||
Thank you again, macropod. This is fantastic. I am fairly new to VBA, and was not able to write the do while/loop.
I would like to make one change. On the .Text line, the first character is [!0-9], which finds any character except numbers. I have tried changing this character to [!,], [!`] or ? in attempt to force the macro to find numbers as well as letters. But this doesn't seem to make the changes I want. I have instances like the one below: In re Grand Jury Empanelled January 28, 2004, 401 F.3d 247 Here, the comma after 2004 remains italic after running the macro. Again, thanks for any help. Last edited by d4okeefe; 04-26-2013 at 10:13 AM. |
#5
|
||||
|
||||
You could simply delete the [!0-9]. That was only there to exclude commas following numbers, as the examples you gave suggested (to me, at least) you were only concerned with commas following letters.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#6
|
|||
|
|||
Thanks again for all your help, macropod.
I believe I figured out the problem, though I may have a difficult time explaining it clearly. Here goes: The .Text = "[!0-9],*>" line, generally, is fine. When I change [!0-9] to ?, reading .Text = "?,*>", the macro finds numbers as well as letters. However, in the second example I gave, In re Grand Jury Empanelled January 28, 2004, 401 F.3d 247 When "8, 2004" is found, the macro recognizes that the comma here should remain in italics. But it then jumps past the "4, 401" phrase, I believe. This seems to be because the "4" in 2004 is already part of the preceding range. To give another example, when I run your original macro in a document with the text, FW/PBS, Inc. v. City of Dallas, Texas, 493 U.S. 215 the comma after Texas remains in italics. There seems to be an issue with how the macro is identifying ranges. Help if you can. Thanks. Last edited by d4okeefe; 04-29-2013 at 09:17 AM. |
#7
|
||||
|
||||
To deal with that, insert:
.End = .Start + 3 before: .Collapse wdCollapseEnd
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#8
|
|||
|
|||
Thanks Paul.
I have really appreciated your help. I have often wanted to manipulate what Find identifies. I believe I have a better understanding of how to do this now. Thanks again. |
#9
|
|||
|
|||
Hi,
This is very interesting to me. I've been trying to decipher the code that macropod wrote, but I'm not sure that I understand it enough to fumble with it. I want to do just the opposite of d4okeefe. Whenever a word is in italics, Macbeth, I want the punctuation that follows it to be in italics too. I'd need to run the sequence a number of times: Macbeth, Macbeth. Macbeth; Macbeth: Macbeth? Macbeth! With parentheses Macbeth) I'd do it one by one, I suppose. Could any of you help me? I've got a very long document in front of me… Thanks! |
#10
|
||||
|
||||
The following clumsy modification appears to work
Code:
Sub Demo() Dim rng As Range Application.ScreenUpdating = False With ActiveDocument.Range With .Find .ClearFormatting .Replacement.ClearFormatting .Font.Italic = True .Text = "*>" .Replacement.Text = "" .Forward = True .Wrap = wdFindStop .Format = False .MatchWildcards = True .Execute End With Do While .Find.Found .Collapse wdCollapseEnd .MoveEnd Unit:=wdCharacter, Count:=1 Debug.Print .Characters(1) Select Case .Characters(1) Case ".", ",", ":", ";", "?", "!" .Characters(1).Font.Italic = True End Select .MoveStart wdCharacter, 1 .Find.Execute Loop End With Application.ScreenUpdating = True End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#11
|
|||
|
|||
Thanks!! This is so kind of you. I can't try your solution right now, but next week I hope I can tell you, not how it works (I'm sure it does), but how I deal with it... Thank you!
|
#12
|
|||
|
|||
Hello again,
Sorry for not having written before. I'm afraid the macro, as proposed, doesn't work. After running it, every punctuation sign has been changed and set to italics, not just the ones following a word in italics, which is what I wanted. (Of course it could be my fault; I'm rather new to all this. It took a few tries, too--I have a long document full of images and tables and what not, rather unwieldy, that needs to be saved as a Word 2003 doc; the program couldn't cope with the macro and would stall, so I had to try with a single separated chapter. Also, I've thought it better: I'd suppress !, ? and ) from the macro, because of double signs; my text is in Spanish.) I will add a bit from The Chicago Manual of Style (15th ed.) to explain what I'm after: "6.3. […] All punctuation marks should appear in the same font--roman or italic--as the main or surrounding text, except for punctuation that belongs to a title or an exclamation in a different font. This departure from Chicago's former usage…[=] 6.5. Punctuation and font: alternative system. According to a more traditional system, periods, commas, colons, and semicolons should appear in the same font as the word, letter, character, or symbol immediately preceding them if different from the main or surrounding text. […]" This last option is what I want to do: traditional typographic usage. Thank you again for your very kind help. |
#13
|
||||
|
||||
It is a bit difficult to determine what is a 'title' without knowing how the document is formatted, but the following should format any of the listed characters (separated by the pipe character) that follows a character formatted as italic.
Code:
Sub Demo() Const strList As String = ".|,|:|;|?|!" Dim vChar As Variant Dim i As Long Dim oRng As Range vChar = Split(strList, "|") For i = LBound(vChar) To UBound(vChar) Set oRng = ActiveDocument.Range With oRng.Find Do While .Execute(vChar(i)) oRng.Start = oRng.Start - 1 If oRng.Characters(1).Font.Italic = True Then oRng.Font.Italic = True End If oRng.Collapse 0 Loop End With Next i lbl_Exit: Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#14
|
|||
|
|||
It works. As you already knew; thanks! I'm so happy!
I'll just add a couple of comments--you can skip them, of course. It's just to add another example of what we ignorant people fight with, in case it's useful for anybody. 1. As mentioned, I'm working with (proofreading) a document in compatibility mode, written by somebody who has Word 2003. It's quite big and crashes now and then. I can't run the macro through the whole document; it produces error 5981, something like "could not open macro storage". I've run the macro through smaller chunks, chapter by chapter, and then it works. I wondered if it has to do with the .dot I'm using, but it's the same one, both for the big document and the small ones. I haven't created a master document, but maybe I should think about it. 2. I've suppressed ! and ? from the macro; double signs, ¿?, ¡!, should really be equal. I will manually correct …, which shouldn't be a problem in this document. 3. The "titles" mention was confusing, sorry. The Chicago Manual of Style is referring to titles of creative works, not Word titles. 4. In my previous message, I copied part of par. no. 6.3 to explain that nowadays it is good practice, at least in the English-speaking world that uses Chicago as a reference, to write punctuation as in the main text, not as in the immediately preceding word; 6.3 explains, though, that it's a recent change for them. But I don't want to do that; I want to do what is described in 6.5, the "traditional system", for aesthetic reasons, and because for the moment I really can't see a good reason for changing this particular traditional usage. Now I have to go back to your site and learn more. It's a great place. Thanks again! |
#15
|
|||
|
|||
Hm. The macro doesn't work for footnotes. Any idea?
|
Tags |
find, italics, msword, punctuation |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Macro to find text and replace with form field containing that text | iiiiifffff | Word VBA | 16 | 06-04-2016 01:47 AM |
Macro to find text in between two characters and then format selected text? | qcom | Word | 5 | 02-19-2015 11:23 PM |
Need help on Macro 03- Find text - if text is blank then remove line | simpleonline1234 | Word VBA | 1 | 02-25-2011 02:28 AM |
Objective: Automatically export email text,attachment text to DB friendly format | SilentLee | Outlook | 0 | 11-14-2010 02:45 PM |
Can't print bold or italic text | cornettd | PowerPoint | 1 | 05-10-2010 10:07 AM |