#1
|
|||
|
|||
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! |
#2
|
|||
|
|||
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 |
#3
|
|||
|
|||
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 |
#4
|
|||
|
|||
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. |
#5
|
||||
|
||||
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 |
#6
|
|||
|
|||
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 |
#7
|
|||
|
|||
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. |
#8
|
||||
|
||||
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:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
||||
|
||||
Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#10
|
|||
|
|||
Thank you, Macropod. Your macros always amaze me! Wonderful.
|
#11
|
||||
|
||||
They sometimes amaze me too!!
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#12
|
|||
|
|||
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. |
#13
|
||||
|
||||
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] |
#14
|
|||
|
|||
Hi,
I've attached a sample data that I copied and pasted from Excel into Word (unformatted text) Thanks again! |
#15
|
||||
|
||||
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] |
|
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 |
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 |
Need help on Macro 03- Find text - if text is blank then remove line | simpleonline1234 | Word VBA | 1 | 02-25-2011 02:28 AM |