Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #16  
Old 08-18-2020, 06:01 PM
Guessed's Avatar
Guessed Guessed is offline Would these macros be possible to make? Windows 10 Would these macros be possible to make? Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

If you don't think it is important enough to provide realistic content and show the hidden formatting then I will just give you a macro that works with the content we assume you have provided.



Try this on a copy of your example document. If it works, try it on a copy of your real document.
Code:
Sub SortParasBySize()
  Dim aRng As Range, aTable As Table, aRow As Row

  ActiveDocument.Range.ConvertToTable Separator:=wdSeparateByParagraphs, NumColumns:=1
  Set aTable = ActiveDocument.Tables(1)
  aTable.Columns.Add BeforeColumn:=aTable.Columns(1)
  For Each aRow In aTable.Rows
    aRow.Cells(1).Range.Text = Len(aRow.Cells(2).Range.Text)
  Next aRow
  aTable.Rows.Add BeforeRow:=aTable.Rows(1)
  aTable.SortDescending
  aTable.Columns(1).Delete
  aTable.Rows(1).Delete
  aTable.ConvertToText
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #17  
Old 08-18-2020, 07:43 PM
grumblid grumblid is offline Would these macros be possible to make? Windows 7 64bit Would these macros be possible to make? Office 2003
Novice
Would these macros be possible to make?
 
Join Date: Jul 2016
Posts: 23
grumblid is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
If you don't think it is important enough to provide realistic content and show the hidden formatting then I will just give you a macro that works with the content we assume you have provided.

Try this on a copy of your example document. If it works, try it on a copy of your real document.
Code:
Sub SortParasBySize()
  Dim aRng As Range, aTable As Table, aRow As Row

  ActiveDocument.Range.ConvertToTable Separator:=wdSeparateByParagraphs, NumColumns:=1
  Set aTable = ActiveDocument.Tables(1)
  aTable.Columns.Add BeforeColumn:=aTable.Columns(1)
  For Each aRow In aTable.Rows
    aRow.Cells(1).Range.Text = Len(aRow.Cells(2).Range.Text)
  Next aRow
  aTable.Rows.Add BeforeRow:=aTable.Rows(1)
  aTable.SortDescending
  aTable.Columns(1).Delete
  aTable.Rows(1).Delete
  aTable.ConvertToText
End Sub
Thanks~ So this almost works! I tried it with a 400-page document and Charles was right about it taking a long time to load. After around 20 minutes I just closed it because I had a feeling it wasn't going anywhere.

But believe it or not, though it still takes a little while to load (2 or 3 minutes, possibly a little more), it was able to work on a 100 page document.

The only problem is while it does organize everything biggest to smallest, everything is clumped together without the vertical spaces each of the bodies of text previously had.

Also if there was a body of text that had a list of things:
- Like
- This
- Kind
- Of
- List,

those individual words were at the very bottom of the document instead of remaining an intact list (staying with the 6-row entries since that example list has 6 rows). But that's not that big of a deal. I just thought I'd let you know :]

If we could solve the spacing issue, I'd say it would pretty much be the macro I was dreaming of, haha~
Reply With Quote
  #18  
Old 08-18-2020, 08:42 PM
Guessed's Avatar
Guessed Guessed is offline Would these macros be possible to make? Windows 10 Would these macros be possible to make? Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

The code I provided assumes your sample separates content by paragraph marks. As you have just discovered, your provided sample does but your REAL document doesn't.

I'm happy to revisit the code only if you provide a realistic sample (in Word format) so that I can see what you are actually dealing with. Although you are under the impression that screen captures gives me an accurate idea on what the underlying content structure is - I can assure you it doesn't. The sample needs to include examples of your bulleted list elements which should be included with the parent paragraph.

The spacing issue is easy enough to solve but the 'separation of related things' issue needs to be addressed after looking at structure of your docs.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #19  
Old 08-18-2020, 09:00 PM
grumblid grumblid is offline Would these macros be possible to make? Windows 7 64bit Would these macros be possible to make? Office 2003
Novice
Would these macros be possible to make?
 
Join Date: Jul 2016
Posts: 23
grumblid is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
The code I provided assumes your sample separates content by paragraph marks. As you have just discovered, your provided sample does but your REAL document doesn't.

I'm happy to revisit the code only if you provide a realistic sample (in Word format) so that I can see what you are actually dealing with. Although you are under the impression that screen captures gives me an accurate idea on what the underlying content structure is - I can assure you it doesn't. The sample needs to include examples of your bulleted list elements which should be included with the parent paragraph.

The spacing issue is easy enough to solve but the 'separation of related things' issue needs to be addressed after looking at structure of your docs.
Thank you for your patience and your help. I've attached a document that you can use. It contains a nice variety of text bodies as well as a list so we can figure that one out as well if you want to. I'm just grateful you're helping me at all. This site has always been great for help~

Also don't worry about it being an example document; if the macro works for this one it'll work for the ones I work with. Please understand those are private :]
Attached Files
File Type: doc ExampleForMacroTest.doc (27.0 KB, 6 views)
Reply With Quote
  #20  
Old 08-18-2020, 10:26 PM
Guessed's Avatar
Guessed Guessed is offline Would these macros be possible to make? Windows 10 Would these macros be possible to make? Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Based on that sample doc, this code appears to work
Code:
Sub SortParasBySize()
  Dim aRng As Range, aTable As Table, aRow As Row
  ActiveWindow.View = wdNormalView
  With ActiveDocument.Range.Find  'Do Find and Replace for separators
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "^p"
    .Replacement.Text = "zx"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute Replace:=wdReplaceAll
    .Text = "zxzx"
    .Replacement.Text = "^p"
    .Execute Replace:=wdReplaceAll
    ActiveDocument.Range.ConvertToTable Separator:=wdSeparateByParagraphs, NumColumns:=1
    .Text = "zx"
    .Replacement.Text = "^p"
    .Execute Replace:=wdReplaceAll
  End With
  Set aTable = ActiveDocument.Tables(1)
  aTable.Columns.Add BeforeColumn:=aTable.Columns(1)
  For Each aRow In aTable.Rows
    aRow.Cells(1).Range.Text = Len(aRow.Cells(2).Range.Text)
  Next aRow
  aTable.Rows.Add BeforeRow:=aTable.Rows(1)
  aTable.SortDescending
  aTable.Columns(1).Delete
  aTable.Columns.Add    'insert empty column to reinstate extra paras between sections
  aTable.Rows(1).Delete
  aTable.ConvertToText Separator:=wdSeparateByParagraphs
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #21  
Old 08-19-2020, 01:06 PM
grumblid grumblid is offline Would these macros be possible to make? Windows 7 64bit Would these macros be possible to make? Office 2003
Novice
Would these macros be possible to make?
 
Join Date: Jul 2016
Posts: 23
grumblid is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
Based on that sample doc, this code appears to work
Code:
Sub SortParasBySize()
  Dim aRng As Range, aTable As Table, aRow As Row
  ActiveWindow.View = wdNormalView
  With ActiveDocument.Range.Find  'Do Find and Replace for separators
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "^p"
    .Replacement.Text = "zx"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute Replace:=wdReplaceAll
    .Text = "zxzx"
    .Replacement.Text = "^p"
    .Execute Replace:=wdReplaceAll
    ActiveDocument.Range.ConvertToTable Separator:=wdSeparateByParagraphs, NumColumns:=1
    .Text = "zx"
    .Replacement.Text = "^p"
    .Execute Replace:=wdReplaceAll
  End With
  Set aTable = ActiveDocument.Tables(1)
  aTable.Columns.Add BeforeColumn:=aTable.Columns(1)
  For Each aRow In aTable.Rows
    aRow.Cells(1).Range.Text = Len(aRow.Cells(2).Range.Text)
  Next aRow
  aTable.Rows.Add BeforeRow:=aTable.Rows(1)
  aTable.SortDescending
  aTable.Columns(1).Delete
  aTable.Columns.Add    'insert empty column to reinstate extra paras between sections
  aTable.Rows(1).Delete
  aTable.ConvertToText Separator:=wdSeparateByParagraphs
End Sub
It works! owo

Wow, you're awesome dude. Thank you so much for taking the time to make such a useful tool. And sorry it took me so long to provide you a document so you could test it properly :]

The only unexpected thing it does is it changes the view from the Print Layout View (which I usually use) to the Normal View, seen at the very bottom left of the screen. Which isn't a big deal at all; I can just click Print Layout View again and it's back~
Reply With Quote
  #22  
Old 08-19-2020, 02:46 PM
Guessed's Avatar
Guessed Guessed is offline Would these macros be possible to make? Windows 10 Would these macros be possible to make? Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

I changed the view in an attempt to speed up the macro because you said the earlier one was very slow. I didn't do any speed tests because I didn't have a big file to test on but in theory, this should help the macro run faster because it reduces the repagination load.

You can disable that line up at the top of the macro. Or add a line to return the view the page view at the end of the macro.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #23  
Old 08-19-2020, 03:55 PM
grumblid grumblid is offline Would these macros be possible to make? Windows 7 64bit Would these macros be possible to make? Office 2003
Novice
Would these macros be possible to make?
 
Join Date: Jul 2016
Posts: 23
grumblid is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
I changed the view in an attempt to speed up the macro because you said the earlier one was very slow. I didn't do any speed tests because I didn't have a big file to test on but in theory, this should help the macro run faster because it reduces the repagination load.

You can disable that line up at the top of the macro. Or add a line to return the view the page view at the end of the macro.
Ah okay~ Well if it's faster I'm all the happier :]

Thanks again and I'll let you guys know if I notice anything else with the two macros made here~
Reply With Quote
Reply

Tags
help me, macros, ms word

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Would these macros be possible to make? How to automatically enable the macros upon opening a file with macros? laurieli Office 7 01-17-2016 08:56 AM
Can you make templates dynamically update macros/etc. from Normal.DOT? New Daddy Word 2 11-18-2013 09:07 AM
Would these macros be possible to make? VBA macros tays01s Word VBA 6 08-03-2011 09:42 PM
Macros nore Outlook 0 06-01-2011 04:39 PM
Would these macros be possible to make? Macros Desertwrangler Word VBA 6 06-25-2010 07:06 AM

Other Forums: Access Forums

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