Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-14-2022, 12:40 AM
TheBigBoss TheBigBoss is offline Ensure macro only run for specific table or table in page 1 Windows 7 32bit Ensure macro only run for specific table or table in page 1 Office 2010 32bit
Advanced Beginner
Ensure macro only run for specific table or table in page 1
 
Join Date: Dec 2016
Posts: 56
TheBigBoss is on a distinguished road
Default Ensure macro only run for specific table or table in page 1

Hi there!

I have a basic macro that changes table properties (lines, width, etc.) of the first FOUR tables found in the document which form a masthead (see below)



It works great... but what about document not having the masthead, the macro will still run and mess up the four first tables found in the document.

I am using to Selection.Information(wdActiveEndPageNumber) to check that that tables are in page 1

However, I wonder if there is any better option.
I saw "ID" under "Table", can table be given an unique ID or name? Or any identifier?

Thanks
Reply With Quote
  #2  
Old 03-14-2022, 04:53 AM
gmayor's Avatar
gmayor gmayor is offline Ensure macro only run for specific table or table in page 1 Windows 10 Ensure macro only run for specific table or table in page 1 Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,138
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 ofgmayor has much to be proud of
Default

It is probably best to error trap each table to ensure that you are processing the correct tables e.g. as follows. I have assumed each table has six cells - change each as appropriate. However why do you need to change the tables? Wouldn't it be better to create your document from a template that already has the masthead correctly formatted?

Code:
Dim oTable As Table
Dim i As Integer
    If ActiveDocument.Tables.Count > 3 Then
        If ActiveDocument.Tables(1).Range.Cells.Count = 6 And _
           ActiveDocument.Tables(2).Range.Cells.Count = 6 And _
           ActiveDocument.Tables(3).Range.Cells.Count = 6 And _
           ActiveDocument.Tables(4).Range.Cells.Count = 6 And _
           ActiveDocument.Tables(4).Range.Information(wdActiveEndPageNumber) = 1 Then
            For i = 1 To 4
                Set oTable = ActiveDocument.Tables(i)
                'Do something with otable e.g.
                oTable.Cell(1, 1).Range.Text = "Test"
            Next i
        End If
    End If
__________________
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 03-14-2022, 05:16 AM
TheBigBoss TheBigBoss is offline Ensure macro only run for specific table or table in page 1 Windows 7 32bit Ensure macro only run for specific table or table in page 1 Office 2010 32bit
Advanced Beginner
Ensure macro only run for specific table or table in page 1
 
Join Date: Dec 2016
Posts: 56
TheBigBoss is on a distinguished road
Default

Thanks Gmayor for your reply.

Sure, I will have to error trap each tables.
Counting cells is a possibility... however, since the last table has a bookmark, I am thinking about selecting tables 1 to 4 as range and check if bookmark is there.


Why not creating a straightforward template?
Indeed. We get document from our clients so the macros we have are macros that will reset default - default masthead + default pagesetup + default stylesheet... while doing the default settings, macro will check for the type of document (category properties) and will load the few specificities.

I hope it is understable. Since most of our templates follow the default one, I decided to have macros (instead of hundreds of templates) and just a few templates on the side for the specific ones.

Thanks
Reply With Quote
  #4  
Old 03-14-2022, 05:26 AM
gmayor's Avatar
gmayor gmayor is offline Ensure macro only run for specific table or table in page 1 Windows 10 Ensure macro only run for specific table or table in page 1 Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,138
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 ofgmayor has much to be proud of
Default

If there's a bookmark in table 4, the code can be simplified e.g as below, where I have also provided different options for each of the four tables if required.
Code:
If ActiveDocument.Tables.Count > 3 Then
        If ActiveDocument.Tables(4).Range.Bookmarks.Exists("bmName") = True Then
            For i = 1 To 4
                Set oTable = ActiveDocument.Tables(i)
                'Do something with otable e.g.
                Select Case i
                    Case Is = 1
                        oTable.Cell(1, 1).Range.Text = "Test"
                    Case Is = 2
                        oTable.Cell(1, 1).Range.Text = "Test2"
                    Case Is = 3
                        oTable.Cell(1, 1).Range.Text = "Test3"
                    Case Is = 4
                        oTable.Cell(1, 1).Range.Text = "Test4"
                End Select
            Next i
        End If
    End If
__________________
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
  #5  
Old 03-14-2022, 06:03 AM
TheBigBoss TheBigBoss is offline Ensure macro only run for specific table or table in page 1 Windows 7 32bit Ensure macro only run for specific table or table in page 1 Office 2010 32bit
Advanced Beginner
Ensure macro only run for specific table or table in page 1
 
Join Date: Dec 2016
Posts: 56
TheBigBoss is on a distinguished road
Default

Thanks Graham for the code and answer.

As a general question, there is no way to have unique identifier to objects then? A bit like ID in HTML which is a unique ID for elements... Odd.
Reply With Quote
  #6  
Old 03-14-2022, 06:27 AM
gmayor's Avatar
gmayor gmayor is offline Ensure macro only run for specific table or table in page 1 Windows 10 Ensure macro only run for specific table or table in page 1 Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,138
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 ofgmayor has much to be proud of
Default

Activedocument.Tables(1) is uniquely the first table.
__________________
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
  #7  
Old 03-14-2022, 03:14 PM
Guessed's Avatar
Guessed Guessed is offline Ensure macro only run for specific table or table in page 1 Windows 10 Ensure macro only run for specific table or table in page 1 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,161
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

There is a Table.ID property but the help says it only works if the document is saved as html.

You can use Table.Title or Table.Descr to see the values entered via Table Properties > Alt Text dialog. This is usually how I identify particular tables.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #8  
Old 03-21-2022, 06:09 AM
TheBigBoss TheBigBoss is offline Ensure macro only run for specific table or table in page 1 Windows 7 32bit Ensure macro only run for specific table or table in page 1 Office 2010 32bit
Advanced Beginner
Ensure macro only run for specific table or table in page 1
 
Join Date: Dec 2016
Posts: 56
TheBigBoss is on a distinguished road
Default

Thanks Guessed and gmayor.

The Table.Title is a nice way around and does work.
However, after consideration, since I need to parse the table to do stuff, I have opted for gmayor option which is to verify conformity of the table (check cells).

I will mark it as solved. Thanks
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking for a macro for word count in one specific column of table, only rows with white background mobj Word VBA 2 10-17-2019 03:09 AM
Ensure macro only run for specific table or table in page 1 Macro to delete table rows based on the absence of a single specific keyword JellehFishh Word VBA 2 06-27-2019 08:23 AM
Linking Specific text fields in PP to specific cells in an Excel table GWRW1964 PowerPoint 0 02-26-2018 07:37 AM
Ensure macro only run for specific table or table in page 1 VBA Table – Search All Tables - Find & Replace Text in Table Cell With Specific Background Color jc491 Word VBA 8 09-30-2015 06:10 AM
Ensure macro only run for specific table or table in page 1 Save table sytle, NOT a specific table Lebber Word 9 02-01-2013 12:31 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:37 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft