![]() |
![]() |
![]() |
|||||
![]() |
![]() |
![]() |
![]() |
![]() |
|||
| Welcome
to Tech Support Forum home to more then 136,000 problems solved. Issues
have included: Spyware, Malware, Virus Issues, Windows, Microsoft,
Linux, Networking, Security, Hardware, and Gaming Getting your
problem solved is as easy as: 1. Registering for a free account 2. Asking your question 3. Receiving an answer Registered members: * See fewer ads. * And much more..
|
| Want to know how to post a question? click here | Having problems with spyware and pop-ups? First Steps |
|
|||||||
| Microsoft Office support MS Office support forum |
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) |
|
Registered User
Join Date: Sep 2008
Posts: 8
OS: xp
|
macro that finds and modifies expressions set in italics
Hi,
I need help with a Microsoft Word macro I want to set up. One part of the macro should be to find all words (or series of words) that are set in italics and precede them with "\emph{" and end them with "}". (Those who know LaTeX will realize that the macro is supposed to help me translate a word document into a LaTeX file.) Since I have no idea about VBA, I don't know how to program this. Can someone help me out? Thanks! Wolfhart |
|
|
|
| Important Information |
|
Join the #1 Tech Support Forum Today - It's Totally Free!
TechSupportForum.com is a leading support website for your computer needs. We offer free, friendly and personalized computer support. Why pay to have your computer fixed when you can do it for free. Join TechSupportforum.com Today - Click Here |
|
|
#2 (permalink) |
|
TSF Enthusiast
Join Date: Apr 2008
Location: Australia
Posts: 561
OS: Vista
|
Re: macro that finds and modifies expressions set in italics
Hi Wolfhart,
Try: Code:
Sub Emphasis()
With ActiveDocument.Content.Find
.ClearFormatting
.Font.Italic = True
.Replacement.ClearFormatting
.Text = "<*>"
.Replacement.Text = "^92emph{^&}"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
.ClearFormatting
.Text = "} \emph{"
.Replacement.Text = " "
.MatchWildcards = False
.Execute Replace:=wdReplaceAll
End With
End Sub
__________________
Cheers macropod (MS MVP -Word) |
|
|
|
|
|
#3 (permalink) |
|
Registered User
Join Date: Sep 2008
Posts: 8
OS: xp
|
Re: macro that finds and modifies expressions set in italics
That works, great! Thanks, macropod!
May I ask you one more thing? What would be the code to insert an empty paragraph after each paragraph? Thanks again, Wolfhart |
|
|
|
|
|
#4 (permalink) |
|
TSF Enthusiast
Join Date: Apr 2008
Location: Australia
Posts: 561
OS: Vista
|
Re: macro that finds and modifies expressions set in italics
Hi Wolfhart,
You could add the lines: .Text = "^p" .Replacement.Text = "^p^p" .Execute Replace:=wdReplaceAll to then end of the existing macro.
__________________
Cheers macropod (MS MVP -Word) |
|
|
|
|
|
#6 (permalink) |
|
Registered User
Join Date: Sep 2008
Posts: 8
OS: xp
|
Re: macro that finds and modifies expressions set in italics
Hi again (in particular hi macropod, if you are still out there),
There is a little problem with the macro that finds and modifies expressions set in italics. If only part of a word is set in italics, the macro does not work at all, and if the expression contains punctuation signs or other special characters, it breaks the expression apart, inserting what comes before and what comes after the special character in "\emph{}" rather than the whole expression (including the special character). Is there a way to fix this? Thanks! Wolfhart |
|
|
|
|
|
#7 (permalink) | |
|
TSF Enthusiast
Join Date: Apr 2008
Location: Australia
Posts: 561
OS: Vista
|
Re: macro that finds and modifies expressions set in italics
Hi Wolfhart,
The reason the macro only works with whole words is because that's what it's coded to do. The '<' and '>' tell the macro to do that. This is what you asked for: Quote:
.Text = "<*>" to: .Text = "*" and: .Text = "} \emph{" to: .Text = "}\emph{" Note that this will make the macro much slower to execute.
__________________
Cheers macropod (MS MVP -Word) |
|
|
|
|
|
|
#8 (permalink) |
|
Registered User
Join Date: Sep 2008
Posts: 8
OS: xp
|
Re: macro that finds and modifies expressions set in italics
Thanks again, macropod! Sorry for having been imprecise in my original request.
I figured out that I also had to change .Replacement.Text = " " to .Replacement.Text = "" Wolfhart |
|
|
|
|
|
#9 (permalink) |
|
Registered User
Join Date: Sep 2008
Posts: 8
OS: xp
|
Re: macro that finds and modifies expressions set in italics
Me again... One more thing...
If I want the macro to pick out all expressions set in a particular language, say French or German, with what do I have to replace the line .Font.Italic = True in the existing macro (supposing that I want to precede and conclude these expressions with a particular string, as in the existing macro). Or would that be more complicated? Thanks again! Wolfhart |
|
|
|
|
|
#10 (permalink) |
|
Registered User
Join Date: Jan 2009
Posts: 2
OS: OS 10.4
|
Re: macro that finds and modifies expressions set in italics
Hello.
Interesting thread. I just stumbled upon it looking for a solution to the same problem Wolfhart presents. I had first thought of a find-replace solution like macropod's, but I think there's a bigger problem with it than has been raised so far. That macro works in two stages: 1. It it finds individual words in italics, then embeds each of them within the expression "\emph{}", giving: ... \emph{individual} \emph{words} \emph{in} \emph{italics} ... 2. To clean up the result, it cuts out the string "} \emph{" wherever it occurs. In this simple case, that's just what we want it to do, giving: ... \emph{individual words in italics} ... (we still have italics, but this attribute will disappear when the text is pasted across into LaTeX). I think there are a couple of problems with this however. a) If the text already contains some LaTeX formatting, step 2) may disrupt it. E.g., if the text contains the LaTeX instruction for footnotes, we might have: ... yet Biggins\footnote{Biggins 2003} insists that his theory ... Applying the macro, this gives first (by 1.): ... yet Biggins\footnote{Biggins 2003} \emph{insists} that his theory ... But then on a second pass (by 2.), this becomes: ... yet Biggins\footnote{Biggins 2003 insists} that his theory ... which gives no italic mark-up but messes with the footnote. The problem is of course worse than just footnotes, since LaTeX mark-up makes liberal use of curly braces. b) The macro searches for whole italicised words, so misses words that are only partly italicised (e.g. unethical), as Wolfhart points out. macropod might be right that a solution to b) is to find-replace individual letters (as opposed to words), then strip out occurrences of "}\emph{" (as opposed to occurrences of "} \emph{", with a space); but there may still be a)-type problems (admittedly \footnote{} probably wouldn't be one of them since this generally follows with a space). I wonder if there isn't a more elegant macro. I'm not a Visual Basic person myself either, but came up with this after reading the examples in the help file: Sub latex_emph() Selection.Find.ClearFormatting With Selection.Find .Text = "" .Font.Italic = True .Replacement.Text = "" .Forward = True .Wrap = wdFindAsk .Format = True .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Dim italRange As Range Set italRange = Selection.Range Selection.Font.Italic = False With italRange .InsertBefore "\emph{" .InsertAfter "}" End With End Sub This has the advantage of finding whole strings (not just individual words or characters) of italics, then enclosing them in \emph{}. This could be a word, a sentence, or half a word. It leaves nothing to strip out and avoids a)-type problems. (It also, takes the original text out of italics so that if the macro is re-run, it doesn't go over the same text twice.) However, not being a Visual Basic person, I don't know how to make this simple macro repeat. As it stands, it only finds the first italicised string, works its magic, then stops. Can anyone tell me what would be the obvious small addition to this bit of code to get the macro to carry on finding more italics until there are none left to find? It works with ranges and I'm not sure If I can use replace-all with those. If there were a way perhaps of invoking an array of all such ranges then adding a For Each command? Or using a While ... Do Loop command? Is there something very obvious that I'm missing? Mephisto8 Last edited by mephisto8; 01-20-2009 at 06:20 PM. |
|
|
|
|
|
#11 (permalink) |
|
TSF Enthusiast
Join Date: Apr 2008
Location: Australia
Posts: 561
OS: Vista
|
Re: macro that finds and modifies expressions set in italics
Hi Mephisto8,
I'm not familiar with LaTeX, so I wasn't aware of the potential problems you've raised. One could use a loop as you've suggested, but try the following instead: Code:
Sub AddEmphasis()
With ActiveDocument.Content.Find
.ClearFormatting
.Font.Italic = True
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = "emph{^&}\emph"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
End Sub
__________________
Cheers macropod (MS MVP -Word) Last edited by macropod; 01-21-2009 at 03:13 AM. |
|
|
|
|
|
#12 (permalink) |
|
Registered User
Join Date: Jan 2009
Posts: 2
OS: OS 10.4
|
Re: macro that finds and modifies expressions set in italics
Hi macropod,
That's marvellous. Thanks very much for your help. I hadn't appreciated it was simply a case of moving from "<*>", to "*" to "" for words, characters or any strings respectively. Excellent tips, hope to make more use of them from now on. Also, the "^&" trick gives me exactly what I wanted – quite surprising how this works, regardless of the search string. Something else I'm sure I'll make great use of in future. A couple of things: the syntax for a word in italics in LaTeX is \emph{word} not emph{word}\emph But I saw how to change this from your earlier example: Code:
.Replacement.Text = "^92emph{^&}"
Code:
.Replacement.Font.Italic = False Out of curiosity, do you know how I would get my (messy) version above to loop? (Though I'm now converted to your way of doing it!) Again, many thanks for your help. Mephisto8 Last edited by mephisto8; 01-21-2009 at 11:48 AM. |
|
|
|
|
|
#13 (permalink) |
|
TSF Enthusiast
Join Date: Apr 2008
Location: Australia
Posts: 561
OS: Vista
|
Re: macro that finds and modifies expressions set in italics
Hi Mephisto8,
You could implement a loop with something like: Code:
Sub latex_emph()
With Selection
With .Find
.ClearFormatting
.Text = ""
.Font.Italic = True
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
Do While .Find.Found
.Font.Italic = False
.Range.InsertBefore "\emph{"
.Range.InsertAfter "}"
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
End Sub
__________________
Cheers macropod (MS MVP -Word) |
|
|
|
|
|
#14 (permalink) |
|
Registered User
Join Date: Sep 2008
Posts: 8
OS: xp
|
Re: macro that finds and modifies expressions set in italics
Hi macropod, hi Mephisto8,
I just realized that there is still a little problem with the macro. When the whole expression is in uppercase letters, the macro puts the \emph also in uppercase letters. Thus WORD yields \EMPH{WORD} LaTeX commands are case sensitive, though, and so the above doesn't work. It should be \emph{WORD} Is there an easy way to fix this? Thanks, Wolfhart |
|
|
|
|
|
#15 (permalink) |
|
TSF Enthusiast
Join Date: Apr 2008
Location: Australia
Posts: 561
OS: Vista
|
Re: macro that finds and modifies expressions set in italics
Hi Wolfhart,
You can get the desired result by changing: .MatchCase = False to: .MatchCase = True Cheers
__________________
Cheers macropod (MS MVP -Word) |
|
|
|
![]() |
| Thread Tools | |
|
|