Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-06-2012, 09:17 AM
georgekung84 georgekung84 is offline Macro for text formatting Windows 7 32bit Macro for text formatting Office 2007
Novice
Macro for text formatting
 
Join Date: Aug 2012
Posts: 5
georgekung84 is on a distinguished road
Default Macro for text formatting

Hello,



I'm new to the forums and was wondering if someone could help me with creating a macro in MS word. I've had a little experience with macros in Excel, but can't seem to figure out how to do this for Word.

Is it possible to change the formatting of a specific text?

For example,

If the text was:

AAA/BBB - CCC: "DDDDD" http://on.nyt.com/pheudio

I want to make "AAA/BBB" bold and the website a clickable link. Everything else can stay the same.

Is there a macro I could create to do this formatting for a list of texts like this? It would be a huge list, so doing it one by one individually could take a while.

Thank you very much in advance!
Reply With Quote
  #2  
Old 08-07-2012, 05:22 AM
OTPM OTPM is offline Macro for text formatting Windows 7 32bit Macro for text formatting Office 2010 32bit
Expert
 
Join Date: Apr 2011
Location: West Midlands
Posts: 981
OTPM is on a distinguished road
Default

Hi George
When you type in web site addresses into Word they automatically convert to hyperlinks so this should not be an issue for you.

As for searching for specific text and making that text BOLD you can do this with a simple search and replace.

Hope I have understood you correctly. If not then come back to the forum with more detail/example.

Tony
Reply With Quote
  #3  
Old 08-07-2012, 07:04 AM
georgekung84 georgekung84 is offline Macro for text formatting Windows 7 32bit Macro for text formatting Office 2007
Novice
Macro for text formatting
 
Join Date: Aug 2012
Posts: 5
georgekung84 is on a distinguished road
Default

Hi Tony,

Thanks for your reply. I don't think I explained things clearly first time around, sorry about that.

I'm copying a list of information from a column in excel into Word. Once I have the list of information in Word I want to format the text. However, the list will usually be fairly large - 50 to 100 or so different items. The text that I want bolded will also be different for most of the items so a search and replace would take too long. (Basically, I want everything left of the "-" bolded)

For example:

Original Version: SHAW / CBI - FT (Lex): "CB&I/Shaw: building a powerhouse" http://on.ft.com/Q7y48X

Formatted Version: SHAW / CBI - FT (Lex): "CB&I/Shaw: building a powerhouse" http://on.ft.com/Q7y48X

Original version: YHOO / Third Point - NYT (DealBook): "Levinsohn Confirms That He’s Leaving Yahoo" http://nyti.ms/LZTu7u

Formatted Version: YHOO / Third Point - NYT (DealBook): "Levinsohn Confirms That He’s Leaving Yahoo" http://nyti.ms/LZTu7u

Also, it is not formatted in Excel, so I can't do a paste special for the desired format in Word.

One last thing, when I paste the information into Word I can only get the website to show up as a link if I go back to each line and press the Enter key after the link. Is there another way to do this?

Thanks again for the help!

George
Reply With Quote
  #4  
Old 08-07-2012, 01:35 PM
Venky62 Venky62 is offline Macro for text formatting Windows 7 64bit Macro for text formatting Office 2010 32bit
Advanced Beginner
 
Join Date: Jul 2012
Posts: 58
Venky62 is on a distinguished road
Default

Yes, what OTPM said is correct. It may be simplest to do Find and Replace for the AAA/BBB string and Word does convert urls into hyperlinks automatically unless you have unchecked this feature in the Word options.

To create a macro, we should be able to identify a logic which will suit your needs. If you upload a page or two of your text, then others will be better able to understand your exact problem and suggest the best solution.
Reply With Quote
  #5  
Old 08-07-2012, 04:07 PM
macropod's Avatar
macropod macropod is offline Macro for text formatting Windows 7 64bit Macro for text formatting Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

Hi George,

Try the following macro:
Code:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
  .InsertBefore vbCr
  .AutoFormat
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "^13[!^13\-]{1,}"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = True
    .Execute
  End With
  Do While .Find.Found
    With .Duplicate
      .Start = .Start + 1
      .Font.Bold = True
    End With
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
ActiveDocument.Paragraphs.First.Range.Delete
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]

Last edited by macropod; 08-08-2012 at 04:48 PM. Reason: Code fix
Reply With Quote
  #6  
Old 08-08-2012, 02:59 AM
OTPM OTPM is offline Macro for text formatting Windows 7 32bit Macro for text formatting Office 2010 32bit
Expert
 
Join Date: Apr 2011
Location: West Midlands
Posts: 981
OTPM is on a distinguished road
Default

Hi
Pauls Macro works fine but there is another way as follows:

1. Enter CTrl+H to display the Search and Replace Dialogue Box.
2. Make sure the "Use WIldcards" box is ticked.
3. In the Find box enter "(^013[!a-z]* )" without the quotes.
4. Click in the replace with box and enter Ctrl+B.
5. Enter Alt+H.

This will replace all instances of AAA/BBB with AAA/BBB.

In addition all hyperlihnks copied into a word document are entered as hyperlinks. To activate them you need to hold the Ctrl key down when you click on them.

Good luck.

Tony
Reply With Quote
  #7  
Old 08-08-2012, 03:11 AM
Venky62 Venky62 is offline Macro for text formatting Windows 7 64bit Macro for text formatting Office 2010 32bit
Advanced Beginner
 
Join Date: Jul 2012
Posts: 58
Venky62 is on a distinguished road
Default

I am not able to understand the wildcard string used by macropod. Will it search for all text that precedes the "-" as desired by George?

Also, he wants to convert all urls into hyperlinks without having to go to each url and pressing enter or a space. If you just cut and paste a url in Word it does not convert it into a hyperlink unless you type a space after it. George is transferring data from Excel to Word, and so the urls may remain as plain text. So I think George's problem is not fully solved.

Tony's Find and Replace will work only if the text to be bold formatted is all caps, but from the sample text George has posted, this is not true always.

George requires a more comprehensive code that will convert all text preceding "-' into bold and converting urls into hyperlinks. At least, that is how i have understood his message.
Reply With Quote
  #8  
Old 08-08-2012, 03:22 AM
macropod's Avatar
macropod macropod is offline Macro for text formatting Windows 7 64bit Macro for text formatting Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

Hi Tony,

A very practical difference between your F/R code and mine is that your's won't work in paragraphs where there are spaces in the data to be bolded (all of George's examples have these) or where lower-case letters are used before the hyphen (two of George's examples have these).

Also, it is evident from what George has posted that his hyperlinks may not be not formatted as such. Simply reassuring him that:
Quote:
In addition all hyperlihnks copied into a word document are entered as hyperlinks. To activate them you need to hold the Ctrl key down when you click on them.
Whether that is so depends on how they're input. If they come from unformatted Excel data, simply pasting them into Word doesn't automatically turn them into hyperlinks. The macro does that.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 08-08-2012, 03:24 AM
macropod's Avatar
macropod macropod is offline Macro for text formatting Windows 7 64bit Macro for text formatting Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

Quote:
Originally Posted by Venky62 View Post
I am not able to understand the wildcard string used by macropod. Will it search for all text that precedes the "-" as desired by George?

Also, he wants to convert all urls into hyperlinks without having to go to each url and pressing enter or a space. If you just cut and paste a url in Word it does not convert it into a hyperlink unless you type a space after it. George is transferring data from Excel to Word, and so the urls may remain as plain text. So I think George's problem is not fully solved.

Tony's Find and Replace will work only if the text to be bold formatted is all caps, but from the sample text George has posted, this is not true always.

George requires a more comprehensive code that will convert all text preceding "-' into bold and converting urls into hyperlinks. At least, that is how i have understood his message.
If you pasted George's examples into Word as unformatted text and ran the macro, you would find it does all that George requires.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #10  
Old 08-08-2012, 03:28 AM
Venky62 Venky62 is offline Macro for text formatting Windows 7 64bit Macro for text formatting Office 2010 32bit
Advanced Beginner
 
Join Date: Jul 2012
Posts: 58
Venky62 is on a distinguished road
Default

Thank you, Macropod. Your macros always amaze me! Wonderful.
Reply With Quote
  #11  
Old 08-08-2012, 03:33 AM
macropod's Avatar
macropod macropod is offline Macro for text formatting Windows 7 64bit Macro for text formatting Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

Quote:
Originally Posted by Venky62 View Post
Thank you, Macropod. Your macros always amaze me! Wonderful.
They sometimes amaze me too!!
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #12  
Old 08-08-2012, 06:30 AM
georgekung84 georgekung84 is offline Macro for text formatting Windows 7 32bit Macro for text formatting Office 2007
Novice
Macro for text formatting
 
Join Date: Aug 2012
Posts: 5
georgekung84 is on a distinguished road
Default

Hi everyone,

Thanks for all your help!

I'm not sure if I'm doing something wrong...I copied the Macro exactly, but it's not changing the text left of the hyphen to Bold format. It changes the links perfectly though. Thank you very much!

Macropod you were able to get it to work prefectly? I copied my examples in Word unformatted, but could only get the links to work.

Tony, thanks for your help as well. As venky62 mentioned, unfortunately it only works for the texts that are all caps.
Reply With Quote
  #13  
Old 08-08-2012, 02:48 PM
macropod's Avatar
macropod macropod is offline Macro for text formatting Windows 7 64bit Macro for text formatting Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

Hi George,

Yes, the macro works fine for me with the data you posted - everything up to the '-' gets formatted as bold text and the hyperlinks become active.

Be aware that subtle differences between what you posted and what's actually present in Word could have an effect. Can you attach a document with a representative sample of the data to a post (delete any sensitive data)? You do this via the paperclip symbol on the 'Go Advanced' tab.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #14  
Old 08-08-2012, 03:49 PM
georgekung84 georgekung84 is offline Macro for text formatting Windows 7 32bit Macro for text formatting Office 2007
Novice
Macro for text formatting
 
Join Date: Aug 2012
Posts: 5
georgekung84 is on a distinguished road
Default

Hi,

I've attached a sample data that I copied and pasted from Excel into Word (unformatted text)

Thanks again!
Attached Files
File Type: docx Sample Data for Articles.docx (10.3 KB, 10 views)
Reply With Quote
  #15  
Old 08-08-2012, 04:48 PM
macropod's Avatar
macropod macropod is offline Macro for text formatting Windows 7 64bit Macro for text formatting Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
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

Hi George,

Somehow a ':' found its way into the Find string in the posted code. I've deleted it and now it works fine.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro for text formatting 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
Formatting macro Ulodesk Word Tables 9 04-10-2012 06:37 PM
Formatting Standard Deviations (Macro) judicial85 Excel Programming 1 08-28-2011 05:28 PM
Macro for text formatting Need help on Macro 03- Find text - if text is blank then remove line simpleonline1234 Word VBA 1 02-25-2011 02:28 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:32 PM.


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