|
#1
|
|||
|
|||
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 |
#2
|
|||
|
|||
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. |
#3
|
||||
|
||||
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 |
#4
|
|||
|
|||
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 |
#5
|
|||
|
|||
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?
|
#6
|
||||
|
||||
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 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 |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
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 |
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 |
MS Word, page goes to next page when entering data on previous page | munna94 | Word | 2 | 12-30-2010 08:12 AM |