Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-12-2018, 02:32 AM
benbob benbob is offline Convert particular blocks of text within a word document to tables using macro Windows 10 Convert particular blocks of text within a word document to tables using macro Office 2016
Novice
Convert particular blocks of text within a word document to tables using macro
 
Join Date: Jul 2018
Posts: 5
benbob is on a distinguished road
Default Convert particular blocks of text within a word document to tables using macro

I want reformat a large word document (1000 pages) so that, wherever in the document the blue text ( as shown in the attachment) is discovered it converts it to a table



These blocks of blue text are in the same order within the document, but can appear on each page and then not for a few pages maybe even 2 on a page.
Ideally the text color should remain as the original, in the document before reformatting. The font should be as the table exampleThe items (data) next to the blue text differs and should be the second column of the table. The table will be 2 columns and 7 rows.

I created a macro to convert to table, but only works if you select the text... also the table it created didn't move all the data to the second column, but kept it in column one.
I would really appreciate your help - Thank you in advance
Attached Files
File Type: docx text-table.docx (15.5 KB, 21 views)
Reply With Quote
  #2  
Old 07-12-2018, 04:59 AM
gmayor's Avatar
gmayor gmayor is offline Convert particular blocks of text within a word document to tables using macro Windows 10 Convert particular blocks of text within a word document to tables using macro Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The short answer is that using your document as an example, what you ask for is not possible. You have only shown one block of text with no indication of how to determine where the block starts and ends.

You referred to 'blue' texts, which start some lines but not others, and while it would be possible to create a macro to process the one block of text, I would imagine the other blocks would be different.

In order to put any text into a table using a macro, you would have to tell the macro where the text range to go in the table starts and stops, and then how to determine which pieces of text go in which cell.

The amount of error handling just to produce your example, when the start and end results are known, is significant. When working in the dark, imposible.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 07-12-2018, 05:59 AM
benbob benbob is offline Convert particular blocks of text within a word document to tables using macro Windows 10 Convert particular blocks of text within a word document to tables using macro Office 2016
Novice
Convert particular blocks of text within a word document to tables using macro
 
Join Date: Jul 2018
Posts: 5
benbob is on a distinguished road
Default

Thanks for replying gmayor...

That upsetting this is a significant piece of work... I will try and answer your clarification questions:

The block of text starts with 'command' and ends 'help'. The blue text is constant but the results will change each time.The block of text is always followed by another table.

The 'example' and 'note' results have been placed underneath (annoyingly) but would also need to move into column 2

Column one would have the blue text and column two the results.

I wonder how else i could mark the text, maybe by format or style?

I have tried to write a macro but i was not getting anywhere, (i have next to no knowledge of VBA) Thanks again for your reply, does what i have added above still result in it being significant task? Could i make alterations to the text to make the creation of the macro easier?
Reply With Quote
  #4  
Old 07-12-2018, 07:36 AM
macropod's Avatar
macropod macropod is offline Convert particular blocks of text within a word document to tables using macro Windows 7 64bit Convert particular blocks of text within a word document to tables using macro Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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 might try something along the lines of the following macro:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range
With ActiveDocument
  With .Range
    With .Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = ":[^s]{1,}"
      .Replacement.Text = ":^t"
      .Forward = True
      .Format = False
      .MatchWildcards = True
      .Wrap = wdFindContinue
      .Execute Replace:=wdReplaceAll
      .Wrap = wdFindStop
      .Text = "Command:*Help:*^13"
      .Execute
    End With
    Do While .Find.Found
      Set Rng = .Duplicate
      With .Duplicate
        With .Find
          .Forward = True
          .Format = False
          .MatchWildcards = True
          .Text = "[^13]{2,}"
          .Replacement.Text = "^p"
          .Execute Replace:=wdReplaceAll
          .Text = ":[^t ]@^13"
          .Replacement.Text = ":^t"
          .Execute Replace:=wdReplaceAll
          .Text = "^t*^13[!^t]@^13"
          .Execute
        End With
        Do While .Find.Found
          If Not .InRange(Rng) Then Exit Do
          With .Paragraphs.Last.Range
            .InsertBefore vbTab
          End With
          .End = .Paragraphs.Last.Range.Start
          .Collapse wdCollapseEnd
          .Find.Execute
        Loop
        With Rng
          With .Find
            .Forward = True
            .Format = False
            .MatchWildcards = True
            .Text = "^13^t"
            .Replacement.Text = "¶"
            .Execute Replace:=wdReplaceAll
          End With
          .ConvertToTable Separator:=vbTab, NumColumns:=2, AutoFitBehavior:=wdAutoFitContent
          .Tables(1).Style = "Table Grid"
          With .Find
            .Text = "¶"
            .Replacement.Text = "^p"
            .Execute Replace:=wdReplaceAll
          End With
        End With
      End With
      .Collapse wdCollapseEnd
      .Find.Execute
    Loop
  End With
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 07-15-2018, 01:34 AM
benbob benbob is offline Convert particular blocks of text within a word document to tables using macro Windows 10 Convert particular blocks of text within a word document to tables using macro Office 2016
Novice
Convert particular blocks of text within a word document to tables using macro
 
Join Date: Jul 2018
Posts: 5
benbob is on a distinguished road
Default

Thanks Macropod, works perfectly.

I have added a macro to make sure the fonts are as i want them

The part i cant reverse engineer from the script was the top row, command:, the text after the description of the command didnt move to the second row. (everything else did)

What do i change to capture the text after Command: as well ( the command part is written 'command: ' (a space after the colon) (without quotes) whereas the other elements have ° (a nonbreaking space)

Thanks for your help... i havent tested it on the 1000 pager yet, but on 4 pages works like a dream
Reply With Quote
  #6  
Old 07-15-2018, 03:20 AM
macropod's Avatar
macropod macropod is offline Convert particular blocks of text within a word document to tables using macro Windows 7 64bit Convert particular blocks of text within a word document to tables using macro Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

Your attachment has 'Command:' and 'Some text' on the same line, so it's unreasonable to expect the macro coded to deal with the document as supplied to put them on different rows when you didn't say that's what you wanted. Furthermore, your attachment has a non-breaking space between 'Command:' and 'Some text', just like the others, not the standard space you're now saying it has. To handle both scenarios, you could change:
.Text = ":[^s]{1,}"
to:
.Text = ":[ ^s]{1,}"
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Tags
vba macro, word 2016

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Referencing text blocks from one document in several chrisr34000 Word 2 07-05-2017 03:17 PM
Convert particular blocks of text within a word document to tables using macro Convert all tables to text but ... bnyamin Word VBA 6 05-25-2017 12:15 AM
Convert particular blocks of text within a word document to tables using macro Convert Word Tables to PDF tcoggins Word Tables 2 06-29-2016 10:28 AM
Convert particular blocks of text within a word document to tables using macro Macro Needed To Convert Text in Word to Plain Text and Back to Word rsrasc Word VBA 5 12-18-2015 07:13 AM
Convert particular blocks of text within a word document to tables using macro convert text to tables oraenthu Word 8 03-17-2015 10:22 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:07 AM.


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