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:
* Get free support
* Communicate privately with other members (PM).
* Removal of this message
* 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
Go Back   Tech Support Forum > Microsoft Support > Microsoft Office support
User Name
Password
Site Map Register Donate Rules Blogs Mark Forums Read


Microsoft Office support MS Office support forum

Reply
 
LinkBack Thread Tools
Old 09-13-2008, 08:30 AM   #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
Wolfhart is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
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

Old 09-14-2008, 06:09 PM   #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)
macropod is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 09-14-2008, 08:42 PM   #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
Wolfhart is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 09-15-2008, 05:27 PM   #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)
macropod is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 09-15-2008, 06:44 PM   #5 (permalink)
Registered User
 
Join Date: Sep 2008
Posts: 8
OS: xp


Re: macro that finds and modifies expressions set in italics

Thanks again, macropod!
Wolfhart
Wolfhart is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 10-04-2008, 05:39 PM   #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
Wolfhart is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 10-05-2008, 03:58 AM   #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:
the macro should be to find all words (or series of words) that are set in italics
To work with parts of words also, you could change:
.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)
macropod is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 10-05-2008, 11:55 AM   #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
Wolfhart is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 10-14-2008, 12:52 AM   #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
Wolfhart is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 01-20-2009, 06:19 PM   #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.
mephisto8 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 01-21-2009, 03:11 AM   #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.
macropod is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 01-21-2009, 11:46 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{^&}"
Also, I worked out that I could add the line:
Code:
.Replacement.Font.Italic = False
so that the actual italic attribute would be removed once the mark-up is applied, allowing me to run the macro an indefinite number of times as I edit and rewrite bits.

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.
mephisto8 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 01-22-2009, 01:02 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)
macropod is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 05-03-2009, 02:45 AM   #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
Wolfhart is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 05-03-2009, 04:46 AM   #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)
macropod is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 05-03-2009, 11:52 PM   #16 (permalink)
Registered User
 
Join Date: Sep 2008
Posts: 8
OS: xp


Re: macro that finds and modifies expressions set in italics

That works. Thanks again, macropod!
Wolfhart
Wolfhart is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -7. The time now is 12:20 PM.



Copyright 2001 - 2009, Tech Support Forum
Home Tips Plus | Outdoor Basecamp | Automotive Support Forum

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85