#1
|
|||
|
|||
Macro to find a word in first row of table and then perform two macros
Hey all,
I have a word document full of tables and each table's first row has the word "Question" in it. I would like to have a macro which goes through the document finding each instance of the first row "Question" in each table and then run two macros which I already have written called "Caption" and "Table1", in that order. Is this doable? Thank you!! |
#2
|
||||
|
||||
I assume your macros are reliant on the selection object being in the first cell before running.
Code:
Sub AAA() Dim aTbl As Table For Each aTbl In ActiveDocument.Tables aTbl.Range.Cells(1).Range.Select Caption Table1 Next aTbl End Sub Last edited by Guessed; 01-28-2015 at 07:28 PM. Reason: PS. It is not a great idea to have a macro called Caption since this is likely to a reserved word that has a meaning in VBA |
#3
|
|||
|
|||
Worked Perfectly!
Thanks so much for your help and the advice in naming macros! The macro you supplied worked perfectly. I was even able to add a section to it so that it would only run on selected tables and also remove the extra paragraph marks between the tables. This will be a huge time saver. Thanks again!
|
#4
|
|||
|
|||
Quote:
|
#5
|
|||
|
|||
Quote:
Sub CaptionPort() ' ' Use this macro to replace captions and tables for portrait orientation With Selection.Tables Dim aTbl As Table For Each aTbl In Selection.Tables aTbl.Range.Cells(1).Range.Select Caption Table1 Next aTbl End With End Sub After the macros run there are two paragraphs between my tables and I'd like only one. The problem I'm running into is that the original selection I've highlighted is lost as my table1 macro is run on each table. Is there a way to save my original selection from the start and recall it after the two macros have run to then run my find and replace macro? Thanks!! Sub ParagraphCleanup() ' ' ParagraphCleanup Macro ' ' Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find .Text = "^p^p" .Replacement.Text = "^p" .Forward = True .Wrap = wdFindAsk .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Replace:=wdReplaceAll End Sub |
#6
|
||||
|
||||
Yes, it is quite simple to set a range and then do the find on the range rather than the selection. All your code could be a lot cleaner if we didn't need to rely on the selection changing to do the work. This is why ranges preferred to selections in most vba code.
Can we see what you are doing in the Caption and Table1 macros so those can be converted to use ranges?
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
Tags |
macro find and replace |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Word VBA Find Table Text Shading Colour and replace with another | QA_Compliance_Advisor | Word VBA | 10 | 09-19-2014 08:36 AM |
Word VBA Macro to Find and Replace based on the Alt Text of an Image | bennymc | Word VBA | 1 | 01-27-2014 04:23 PM |
How Find Out What Keys Trigger My Macros? | peytontodd | Word | 1 | 10-28-2013 09:39 AM |
Find - Replace Macro using a table list | mdw | Word | 0 | 08-01-2013 04:36 PM |
Macro that can find phrase and then find another and copy | jperez84 | Word VBA | 10 | 09-19-2012 04:48 PM |