Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-25-2013, 08:32 AM
d4okeefe d4okeefe is offline Find text, format part of text in italic Windows Vista Find text, format part of text in italic Office 2010 64bit
Advanced Beginner
Find text, format part of text in italic
 
Join Date: Apr 2013
Posts: 77
d4okeefe is on a distinguished road
Default 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.
Reply With Quote
  #2  
Old 04-25-2013, 03:37 PM
macropod's Avatar
macropod macropod is offline Find text, format part of text in italic Windows 7 64bit Find text, format part of text in italic Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 04-25-2013, 06:25 PM
d4okeefe d4okeefe is offline Find text, format part of text in italic Windows Vista Find text, format part of text in italic Office 2010 64bit
Advanced Beginner
Find text, format part of text in italic
 
Join Date: Apr 2013
Posts: 77
d4okeefe is on a distinguished road
Default

You are awesome. Thanks. I can't try it on my docs until I return to work, tomorrow. I'll let you know.
Reply With Quote
  #4  
Old 04-26-2013, 07:31 AM
d4okeefe d4okeefe is offline Find text, format part of text in italic Windows Vista Find text, format part of text in italic Office 2010 64bit
Advanced Beginner
Find text, format part of text in italic
 
Join Date: Apr 2013
Posts: 77
d4okeefe is on a distinguished road
Default

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.
Reply With Quote
  #5  
Old 04-26-2013, 05:24 PM
macropod's Avatar
macropod macropod is offline Find text, format part of text in italic Windows 7 64bit Find text, format part of text in italic Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #6  
Old 04-29-2013, 08:16 AM
d4okeefe d4okeefe is offline Find text, format part of text in italic Windows Vista Find text, format part of text in italic Office 2010 64bit
Advanced Beginner
Find text, format part of text in italic
 
Join Date: Apr 2013
Posts: 77
d4okeefe is on a distinguished road
Default

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.
Reply With Quote
  #7  
Old 04-29-2013, 04:15 PM
macropod's Avatar
macropod macropod is offline Find text, format part of text in italic Windows 7 64bit Find text, format part of text in italic Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

To deal with that, insert:
.End = .Start + 3
before:
.Collapse wdCollapseEnd
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #8  
Old 04-30-2013, 03:03 PM
d4okeefe d4okeefe is offline Find text, format part of text in italic Windows Vista Find text, format part of text in italic Office 2010 64bit
Advanced Beginner
Find text, format part of text in italic
 
Join Date: Apr 2013
Posts: 77
d4okeefe is on a distinguished road
Default

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.
Reply With Quote
  #9  
Old 03-06-2015, 10:43 AM
Salix Salix is offline Find text, format part of text in italic Windows 8 Find text, format part of text in italic Office 2007
Novice
 
Join Date: Mar 2015
Posts: 9
Salix is on a distinguished road
Default

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!
Reply With Quote
  #10  
Old 03-07-2015, 02:57 AM
Guessed's Avatar
Guessed Guessed is offline Find text, format part of text in italic Windows 7 32bit Find text, format part of text in italic Office 2010 32bit
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #11  
Old 03-07-2015, 05:16 AM
Salix Salix is offline Find text, format part of text in italic Windows 8 Find text, format part of text in italic Office 2007
Novice
 
Join Date: Mar 2015
Posts: 9
Salix is on a distinguished road
Default

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!
Reply With Quote
  #12  
Old 04-06-2015, 04:08 PM
Salix Salix is offline Find text, format part of text in italic Windows 8 Find text, format part of text in italic Office 2007
Novice
 
Join Date: Mar 2015
Posts: 9
Salix is on a distinguished road
Default

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.
Reply With Quote
  #13  
Old 04-06-2015, 09:45 PM
gmayor's Avatar
gmayor gmayor is offline Find text, format part of text in italic Windows 7 64bit Find text, format part of text in italic Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #14  
Old 04-07-2015, 08:00 AM
Salix Salix is offline Find text, format part of text in italic Windows 8 Find text, format part of text in italic Office 2007
Novice
 
Join Date: Mar 2015
Posts: 9
Salix is on a distinguished road
Default

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!
Reply With Quote
  #15  
Old 04-07-2015, 09:00 AM
Salix Salix is offline Find text, format part of text in italic Windows 8 Find text, format part of text in italic Office 2007
Novice
 
Join Date: Mar 2015
Posts: 9
Salix is on a distinguished road
Default

Hm. The macro doesn't work for footnotes. Any idea?
Reply With Quote
Reply

Tags
find, italics, msword, punctuation

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find text, format part of text in italic 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
Find text, format part of text in italic 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
Find text, format part of text in italic Can't print bold or italic text cornettd PowerPoint 1 05-10-2010 10:07 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:56 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft