Avatar of jologo1
jologo1

asked on 

How to find and replace all tables in a document with a text message "insert table here"?

To simplify editing, I would like to have a macro to search for all tables in a Word document and replace each table with three words of text e.g.,'insert table here.' This is easy to do with graphics without a macro. I found a macro at Microsoft-  http://support.microsoft.com/kb/212692 - that is supposed to find all tables in a document. It does that  but once each is found it doesn't allow you to do anything like insert text. (I get an 'hourglass' when I try to delete the table and insert text ...In fact, for me the macro's 'no' (exit) button doesn't work either.)

I'd appreciate it if I could get a modification of this macro or some other work around that enables me to search for all tables in a Word document and replace each table with three words of text e.g.,'insert table here.'  Thanks. Macro text is below and attached.

Sub FindTables()
      Dim iResponse As Integer
      Dim tTable As Table
      'If any tables exist, loop through each table in collection.
      For Each tTable In ActiveDocument.Tables
         tTable.Select
         iResponse = MsgBox("Table found. Find next?", 68)
         If response = vbNo Then Exit For 'User chose to leave search.
      Next
      MsgBox prompt:="Search Complete.", buttons:=vbInformation
   End Sub
Visual Basic ClassicMicrosoft Word

Avatar of undefined
Last Comment
jologo1
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

This code will delete the first table and then ask if the next is to be deleted.

Sub FindTables()
    Dim iResponse As Integer
    Dim tTable As Table
    Dim rng As Range
    'If any tables exist, loop through each table in collection.
    For Each tTable In ActiveDocument.Tables
        Set rng = tTable.Range
        tTable.Delete
        rng.Text = "Insert table here"
        iResponse = MsgBox("Table found. Find next?", 68)
        If iResponse = vbNo Then
            Exit For 'User chose to leave search.
        End If
    Next
    MsgBox prompt:="Search Complete.", Buttons:=vbInformation
End Sub
    

Open in new window

Avatar of jologo1
jologo1

ASKER

Thanks Graham.

It's halfway there and better in a way than the MS macro since the 'no' (exit) button actually does work. But It needs to insert a text message after deleting the found table for example  'insert table here.'

Preferably, it would offer the option 1) to find and replace every table in the document with the designated text message all at once, in addition2) to offering  the choice  to replace each table as found with the text message. Even better, after each completed action it would ask whether you wanted to find the next table and then provide a second choice with three options a).  replace the table with the designated text message b). edit the table (highlight it allowing the user to edit the table or type in a customized message before manually deleting it), or c) exit.

However, the very first option 1) would meet my immediate needs however.

Regards,
J.
 
Avatar of jologo1
jologo1

ASKER

I meant to attach these in my original post.
WD2000--Macro-to-Find-Tables-in-.pdf
button.jpg
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

The code actually does insert the text message as you asked in the question. Message boxes have limited options, but I'll see if the three-way choice can be implemented acceptably.
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of jologo1
jologo1

ASKER

Brilliant! This works a charm. Also, you were right. Your first macro  did replace  the table with the text (I  should have checked more carefully.) So your second macro is a bonus then - I'm very grateful. Both should be really useful in preparing my documents. Many thanks!
Visual Basic Classic
Visual Basic Classic

Visual Basic is Microsoft’s event-driven programming language and integrated development environment (IDE) for its Component Object Model (COM) programming model. It is relatively easy to learn and use because of its graphical development features and BASIC heritage. It has been replaced with VB.NET, and is very similar to VBA (Visual Basic for Applications), the programming language for the Microsoft Office product line.

165K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo