Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-28-2015, 04:10 PM
hmsrose hmsrose is offline Macro to find a word in first row of table and then perform two macros Windows 7 32bit Macro to find a word in first row of table and then perform two macros Office 2010 32bit
Novice
Macro to find a word in first row of table and then perform two macros
 
Join Date: Jan 2015
Posts: 4
hmsrose is on a distinguished road
Question 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!!
Reply With Quote
  #2  
Old 01-28-2015, 07:26 PM
Guessed's Avatar
Guessed Guessed is offline Macro to find a word in first row of table and then perform two macros Windows 7 32bit Macro to find a word in first row of table and then perform two macros Office 2010 32bit
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,105
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 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
Reply With Quote
  #3  
Old 01-29-2015, 10:15 AM
hmsrose hmsrose is offline Macro to find a word in first row of table and then perform two macros Windows 7 32bit Macro to find a word in first row of table and then perform two macros Office 2010 32bit
Novice
Macro to find a word in first row of table and then perform two macros
 
Join Date: Jan 2015
Posts: 4
hmsrose is on a distinguished road
Default Worked Perfectly!

Quote:
Originally Posted by Guessed View Post
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
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!
Reply With Quote
  #4  
Old 01-29-2015, 11:54 AM
hmsrose hmsrose is offline Macro to find a word in first row of table and then perform two macros Windows 7 32bit Macro to find a word in first row of table and then perform two macros Office 2010 32bit
Novice
Macro to find a word in first row of table and then perform two macros
 
Join Date: Jan 2015
Posts: 4
hmsrose is on a distinguished road
Default

Quote:
Originally Posted by hmsrose View Post
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!
Well, I thought I had the paragraph part down but that may have been a fluke.
Reply With Quote
  #5  
Old 01-29-2015, 12:15 PM
hmsrose hmsrose is offline Macro to find a word in first row of table and then perform two macros Windows 7 32bit Macro to find a word in first row of table and then perform two macros Office 2010 32bit
Novice
Macro to find a word in first row of table and then perform two macros
 
Join Date: Jan 2015
Posts: 4
hmsrose is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
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
Well, I thought I had the paragraph part down but that may have been a fluke.

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
Reply With Quote
  #6  
Old 01-30-2015, 12:17 AM
Guessed's Avatar
Guessed Guessed is offline Macro to find a word in first row of table and then perform two macros Windows 7 32bit Macro to find a word in first row of table and then perform two macros Office 2010 32bit
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,105
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

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
Reply With Quote
Reply

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
Macro to find a word in first row of table and then perform two macros 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
Macro to find a word in first row of table and then perform two macros 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 to find a word in first row of table and then perform two macros Macro that can find phrase and then find another and copy jperez84 Word VBA 10 09-19-2012 04:48 PM

Other Forums: Access Forums

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