Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-25-2023, 04:59 PM
bblelli bblelli is offline Run a macro from page x to page Y Windows 10 Run a macro from page x to page Y Office 2021
Novice
Run a macro from page x to page Y
 
Join Date: Feb 2023
Posts: 22
bblelli is on a distinguished road
Question Run a macro from page x to page Y

Dears,



How can I run a specific VBA code to format some tables from a given page range?

My code is working perfectly, it's formatting the tables correctly, but now I would like to let the user determine the range of pages to apply and run the macro.
So, in this case, the macro won't format the entire document, the macro will only format the pages that are in the given page range.

For example, my document has 500 pages, and everypage has a table. But I would like to format the tables from pages 100 to 200, so, the macro will only format the tables from page 100 to 200. The macro will not format the tables from pages 201 to 500.

Thanks
Reply With Quote
  #2  
Old 02-26-2023, 12:31 PM
BrianHoard BrianHoard is offline Run a macro from page x to page Y Windows 10 Run a macro from page x to page Y Office 2019
Advanced Beginner
 
Join Date: Jul 2022
Location: Haymarket, VA USA
Posts: 85
BrianHoard is on a distinguished road
Default

Just a thought. What if you had the user enter a keyword where they wanted to start, and end. Then, let your script look for that to determine the range to work with? I know it may not be perfect, but I have used this sort of thing in the past as a quick way to get something done. For instance, if at the start of your desired range, the user entered some unique string like <<<FORMAT START HERE>>>, and a similar one at the end.
Some pseudo code to work with that might be...
Using a find, search for <<<FORMAT START HERE>>>.
Store the .Start of this range as a variable.

Do the same for <<<FORMAT END HERE>>>, getting the .End of that range.

Now, you have your .Start and .End stored so apply your formatting to that.
When finished, delete the formatting strings.

Just something to consider, as I am not sure there is a way to tell Word to act on pages, since the number pages can vary as you edit the document and things automatically flow from page to page.
Reply With Quote
  #3  
Old 02-26-2023, 04:52 PM
Guessed's Avatar
Guessed Guessed is offline Run a macro from page x to page Y Windows 10 Run a macro from page x to page Y Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,995
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

Using Pages to define a range is painful from a coding viewpoint. I would recommend you either use selection or a bookmark to define the chosen range.

From the user's viewpoint, what is their thought process in deciding what range the code has to run over? If you are going to prompt them for a start and finish page number, what are they going to need to do BEFORE running the macro in order to be able to answer that question? Can you simplify that decision making process by coding it without asking the users for page numbers?
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #4  
Old 02-27-2023, 05:19 AM
bblelli bblelli is offline Run a macro from page x to page Y Windows 10 Run a macro from page x to page Y Office 2021
Novice
Run a macro from page x to page Y
 
Join Date: Feb 2023
Posts: 22
bblelli is on a distinguished road
Default

Dears,

That's interesting... I really thought that you could easily work with pages in Microsoft Word, once it's as intuitive as working with cells in Microsoft Excel.

Luckly I've some sections...
Is is possible to work with sections?

For example, I have more than 20 sections in my document.
Is it possible to run my macro only for section 2 for example?

Thanks
Reply With Quote
  #5  
Old 02-27-2023, 07:12 AM
BrianHoard BrianHoard is offline Run a macro from page x to page Y Windows 10 Run a macro from page x to page Y Office 2019
Advanced Beginner
 
Join Date: Jul 2022
Location: Haymarket, VA USA
Posts: 85
BrianHoard is on a distinguished road
Default

Yes, now I believe it is possible to loop through sections. But I liked the direction Guessed was asking. What logic will the user use to determine what sections need formatted? It is very likely that is a good way to build the script. Can you explain what is the difference between what needs formatted and what doesn't? Or better yet, can you post a sample document with just a few pages, sections, and tables? A before and after table?
Reply With Quote
  #6  
Old 02-27-2023, 03:27 PM
Guessed's Avatar
Guessed Guessed is offline Run a macro from page x to page Y Windows 10 Run a macro from page x to page Y Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,995
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

Word and Excel's VBA is similar but both rely on fixed units for accuracy. Document:Section:Paragraph/Table are the equivalent of Workbook:Worksheet:Cell

Unfortunately, 'page' is not a fixed unit when you are interested in a specific region of content in Word. You can interrogate the content to find out what page something happens to be sitting on but you will get different results if something unrelated causes a change (eg font size changes).

Sections are something that we can code for easily.
Code:
  Dim aTbl As Table
  For Each aTbl In ActiveDocument.Sections(2).Range.Tables
    'code for table goes here
  Next aTbl
You can see this code is 'fixed' to a static section number. A more flexible solution that might work for you is to have the macro use the current selection to determine which section you wanted to process. Since a selection can span multiple sections your code needs to get specific eg.
Code:
  Dim aTbl As Table, aSect As Section
  Set aSect = Selection.Range.Sections.First
  For Each aTbl In aSect.Range.Tables
    'code for table goes here
    aTbl.Select
  Next aTbl
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Run a macro from page x to page Y inserting a page break after each 9th page? or skip print on the 10th page ? vender Mail Merge 2 05-27-2020 05:18 AM
Custom page size - Page color not covering entire page when converting to PDF icor1031 Word 9 12-09-2019 08:21 PM
Run a macro from page x to page Y Start page number on page 3, go to page not working properly MetroBOS Word 7 01-30-2016 11:31 PM
2 page document printing problem, text from page 1 in layout of page 2 when printed laurawether45 Word 1 08-02-2012 07:03 AM
Run a macro from page x to page Y MS Word, page goes to next page when entering data on previous page munna94 Word 2 12-30-2010 08:12 AM

Other Forums: Access Forums

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