#1
|
|||
|
|||
Inserting and rearranging text by inspecting existing text
I would like to convert the following paragraph:
1. regular text & character part1 bold or underlined text & character partA regular text & character part2 bold or underlined text & character partB Into following: MYTEXT1 1. MYTEXT2 regular text & character part1 regular text & character part2 MYTEXT3 bold or underlined text & character partA bold or underlined text & character partBMYTEXT4 bold or underlined text & character partA bold or underlined text & character partBnumber of regular or bold text part can be one or more. Would really appreciate the help |
#2
|
||||
|
||||
You need to explain how the text you want to process is supposed to be recognised by a macro, what the 'MYTEXT1' - 'MYTEXT4' strings refer to and where they come from. Somehow I doubt that what you want to process and the desired outcome is literally what you posted...
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Thanks for the quick reply.
So Basically the text documents will have the first paragraph. Once I select the paragraph and run the macro it will convert the selected paragraph to the second paragraph. each of MYTEXT are hardcoded text strings (i.e. a sentence). The macro will -look for number followed by apostrophe (i.e. 1. 2. etc) and insert MYTEXT1 before it -look for bold text and non-bold portion in the selected paragraph and rearrange them and insert MYTEXT2, MYTEXT3 & MYTEXT4 so that the outcome looks like following. It also unbold the bold text finally. Basically I am trying to use the bold formatting as indicator of what text need to move where. MYTEXT1 1. MYTEXT2 regular text & character part1 regular text & character part2 MYTEXT3 bold or underlined text & character partA bold or underlined text & character partB MYTEXT4 bold or underlined text & character partA bold or underlined text & character partBnumber of regular or bold text part can be one or more. |
#4
|
|||
|
|||
Another way to describe my problem
Input paragraph (that I will select): ===================== n. string1 stringA string2 stringB Result (that I want the selected paragraph to be converted to) ============= MYTEXT1 1. MYTEXT2 string1 string2 MYTEXT3 stringA stringB MYTEXT4 stringA stringB where n is nay integer MYTEXT are hardcoded sentences or paragraph string1 & string2 are normal (such as un-bold) paragraph stringA & stringB are bold or formatted paragraph in the input but presented unformatted in the output result Thanks! |
#5
|
||||
|
||||
Try:
Code:
Sub Demo() Application.ScreenUpdating = False Dim RngFnd As Range, RngTmp As Range, StrTxt(), StrTmp1 As String, StrTmp2 As String StrTxt() = Array("MYTEXT1 ", "MYTEXT2 ", "MYTEXT3", "MYTEXT4") With ActiveDocument.Range With .Find .ClearFormatting .Replacement.ClearFormatting .Text = "[^13][0-9]{1,}.[!^13]{1,}" .Replacement.Text = "" .Forward = True .Wrap = wdFindStop .Format = False .MatchWildcards = True .Execute End With Do While .Find.Found If .Font.Bold = 9999999 Or .Font.Italic = 9999999 Then .Start = .Start + 1 .InsertBefore StrTxt(0) .Start = .Start + Len(StrTxt(0)) .Start = .Start + InStr(.Text, ". ") + 1 Set RngFnd = .Duplicate: Set RngTmp = .Duplicate StrTmp1 = "": StrTmp2 = "" RngTmp.Collapse wdCollapseStart With .Duplicate With .Find .ClearFormatting .Format = True .Font.Bold = False .Font.Italic = False .Wrap = wdFindStop .Execute End With Do While .Find.Found If Not .InRange(RngFnd) Then RngTmp.End = RngFnd.End With RngTmp If .Font.Bold = True Or .Font.Italic = True Then StrTmp2 = StrTmp2 & Trim(.Text) & " " End With Exit Do End If StrTmp1 = StrTmp1 & Trim(.Text) & " " If .Duplicate.Start > RngTmp.End Then RngTmp.End = .Duplicate.Start StrTmp2 = StrTmp2 & Trim(RngTmp.Text) & " " RngTmp.Start = .Duplicate.End .Collapse wdCollapseEnd .Find.Execute Loop End With .Text = StrTxt(1) & StrTmp1 & StrTxt(2) & StrTmp2 & StrTxt(3) .Font.Bold = False .Font.Italic = False End If .Collapse wdCollapseEnd .Find.Execute Loop End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#6
|
|||
|
|||
Thank you so much Paul. The code is exactly what I was looking for.
If I want to use it only for selected text what changes do I need to make? I have tried putting Selection.Find, but does not seems to work. |
#7
|
||||
|
||||
To do that, replace:
With ActiveDocument.Range with: Dim RngFnd As Range Set RngMain = Selection.Range With Selection.Range and insert: If Not .InRange(RngMain) Then Exit Sub before: If .Font.Bold = 9999999 Or .Font.Italic = 9999999 Then
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#8
|
|||
|
|||
You are really active, thank you so much I will try that
|
#9
|
|||
|
|||
Another question, Do you recommend any good book that might have longer code explained.
I just read the following, which have only little code and sort of introductory Mastering VBA for Microsoft Office 2013 |
#10
|
||||
|
||||
I'm not in a position to recommend any books on advanced VBA - my VBA knowledge is almost entirely self-taught.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Entering new text in different color in an existing document | davidbish79 | Word | 4 | 03-03-2015 12:58 AM |
how to add text to a document without existing text moving around? | Athalwolf | Word | 7 | 12-16-2014 05:16 PM |
Text format different than existing one | Manrak | Word | 1 | 08-11-2013 04:51 PM |
Macro for tagging and rearranging selected text for revision | caotico | Word VBA | 0 | 03-28-2012 06:35 PM |
Formatting existing text | Kaila | Word | 0 | 09-03-2011 05:40 PM |