Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-31-2016, 06:11 PM
rok123 rok123 is offline Inserting and rearranging text by inspecting existing text Windows 7 64bit Inserting and rearranging text by inspecting existing text Office 2013
Novice
Inserting and rearranging text by inspecting existing text
 
Join Date: Jan 2016
Posts: 8
rok123 is on a distinguished road
Post 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
Reply With Quote
  #2  
Old 01-31-2016, 08:04 PM
macropod's Avatar
macropod macropod is offline Inserting and rearranging text by inspecting existing text Windows 7 64bit Inserting and rearranging text by inspecting existing text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

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]
Reply With Quote
  #3  
Old 02-02-2016, 01:26 PM
rok123 rok123 is offline Inserting and rearranging text by inspecting existing text Windows 7 64bit Inserting and rearranging text by inspecting existing text Office 2013
Novice
Inserting and rearranging text by inspecting existing text
 
Join Date: Jan 2016
Posts: 8
rok123 is on a distinguished road
Default

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.
Reply With Quote
  #4  
Old 02-02-2016, 01:34 PM
rok123 rok123 is offline Inserting and rearranging text by inspecting existing text Windows 7 64bit Inserting and rearranging text by inspecting existing text Office 2013
Novice
Inserting and rearranging text by inspecting existing text
 
Join Date: Jan 2016
Posts: 8
rok123 is on a distinguished road
Default

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!
Reply With Quote
  #5  
Old 02-02-2016, 06:03 PM
macropod's Avatar
macropod macropod is offline Inserting and rearranging text by inspecting existing text Windows 7 64bit Inserting and rearranging text by inspecting existing text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

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]
Reply With Quote
  #6  
Old 02-05-2016, 05:40 PM
rok123 rok123 is offline Inserting and rearranging text by inspecting existing text Windows 7 64bit Inserting and rearranging text by inspecting existing text Office 2013
Novice
Inserting and rearranging text by inspecting existing text
 
Join Date: Jan 2016
Posts: 8
rok123 is on a distinguished road
Default

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.
Reply With Quote
  #7  
Old 02-05-2016, 05:53 PM
macropod's Avatar
macropod macropod is offline Inserting and rearranging text by inspecting existing text Windows 7 64bit Inserting and rearranging text by inspecting existing text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

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]
Reply With Quote
  #8  
Old 02-05-2016, 05:58 PM
rok123 rok123 is offline Inserting and rearranging text by inspecting existing text Windows 7 64bit Inserting and rearranging text by inspecting existing text Office 2013
Novice
Inserting and rearranging text by inspecting existing text
 
Join Date: Jan 2016
Posts: 8
rok123 is on a distinguished road
Default

You are really active, thank you so much I will try that
Reply With Quote
  #9  
Old 02-05-2016, 06:02 PM
rok123 rok123 is offline Inserting and rearranging text by inspecting existing text Windows 7 64bit Inserting and rearranging text by inspecting existing text Office 2013
Novice
Inserting and rearranging text by inspecting existing text
 
Join Date: Jan 2016
Posts: 8
rok123 is on a distinguished road
Default

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



Reply With Quote
  #10  
Old 02-05-2016, 07:31 PM
macropod's Avatar
macropod macropod is offline Inserting and rearranging text by inspecting existing text Windows 7 64bit Inserting and rearranging text by inspecting existing text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
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

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]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting and rearranging text by inspecting existing text 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
Inserting and rearranging text by inspecting existing text 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

Other Forums: Access Forums

All times are GMT -7. The time now is 07:20 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