Solved

How to find if a word/phrase NOT FOUND in a table

Posted on 2007-03-29
6
464 Views
Last Modified: 2013-11-25
Hi experts!

This is part of a macro that I wrote which searches through a number of Word tables for a company/brand and if it finds it IN THE FIRST COLUMN to copy that row to another Word document. It works great as long as all the companies/brands are in every table but if one is missing, it just jumps to the next table. This is bad since I then go back and copy the title of the table and paste it into the first row and so if the company/phrase isn't found, the labels no longer match the data. I need a way so that if it doesn't find that company/brand in that table, IN THE FIRST COLUMN, it will jump to the other Word document and put in the phrase "COMPANY NOT FOUND!" Thanks in advance. You guys are amazing!

------------------------------------------------------------------

dim x as integer
dim WhatColumn as integer
dim InTable as integer

for x=1 to NumberOfTables

    Selection.Find.ClearFormatting

    With Selection.Find
        .Text = "XYZ Company"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = True
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With

    Selection.Find.Execute

' ---------------------------------------------------------------------------------------------------
' Code to see what column the macro is in and if it is not in the first column of a table, ignore it
' ---------------------------------------------------------------------------------------------------

    WhatColumn = Selection.Information(wdStartOfRangeColumnNumber)
    InTable = Selection.Information(wdWithInTable)

    If WhatColumn > 1 Or InTable = False Then
        x = x - 1
        GoTo TheNextOne1
    End If

    Selection.SelectRow
    Selection.Copy

' Jump to other Word Document and paste the row in. If the company/phrase not found in that table, create a blank row and paste "COMPANY NOT FOUND!" in first column of this blank row.

    Switch_Windows
    Selection.Paste
    Switch_Windows
    Selection.Find.Execute

TheNextOne1:
next x

'----------------------------------------------------------

Sub Switch_Windows()

z = ActiveWindow.Index

If z = 1 Then
    ActiveWindow.Next.Activate
    z = ActiveWindow.Index
Else
    ActiveWindow.Previous.Activate
    z = ActiveWindow.Index
End If

End Sub
0
Comment
Question by:Eddie_Aeffect
  • 3
  • 3
6 Comments
 
LVL 17

Expert Comment

by:ramrom
ID: 18840986
Following assumes you are looking for exactly one company, as you do in your code above
                        
for x=1 to NumberOfTables
    activedocument.tables(x).Columns(1).Select
    Selection.Find.ClearFormatting

    With Selection.Find
        .Text = "XYZ Company"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = True
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With

    if selection.find.execute then
      ' process the item
    else
      ' process absense of item
    end if
  end with
next
0
 

Author Comment

by:Eddie_Aeffect
ID: 18844356
Hi ramrom!

I ran your code and it tells me ERROR 5992 - CANNOT ACCESS INDIVIDUAL COLUMNS IN THIS COLLECTION BECAUSE THE TABLE HAS MIXED CELL WIDTHS. I do have merged cells where I label columns (i.e., GENDER for Male and Female). The frustrating thing is that the cells in the first column are never merged.
0
 
LVL 17

Accepted Solution

by:
ramrom earned 400 total points
ID: 18846150
Let's not use find. There does not seem to be a way to constrain it to the selection! So try this:

Sub test()
NumberOfTables = 2 ' I added this for my test case
For x = 1 To NumberOfTables
    Found = False
    With ActiveDocument.Tables(x)
        For r = 2 To .Rows.Count ' skip header row
            If InStr(.Rows(r).Cells(1).Range.Text, "XYZ Company") Then
              ' process the item
              Found = True
              Exit For
            End If
        Next
        If Not Found Then
            ' process absense of item
        End If
    End With
Next
End Sub
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:Eddie_Aeffect
ID: 18853743
Hi ramrom!

Don't know how long I can keep this open before it becomes rude so I think I'll close it now. Your answer didn't quite work because it was looping through the tables without actually landing in the tables so I am using:

For x = 1 To MaximumTables
     Selection.GoTo What:=wdGoToTable, Which:=wdGoToFirst, Count:=x, Name:=""
 . . .
 . . .
next x

And playing around with the code from there. I did learn some stuff from your code examples and I thank you for that!

Eddie




0
 
LVL 17

Expert Comment

by:ramrom
ID: 18855466
I'm not sure why you say "without actually landing in the tables".

ActiveDocument.Tables(x).Rows(r).Cells(1).Range.Text refers to the text of column 1 of row r of table x.

If instr() returns true then ActiveDocument.Tables(x).Rows(r) gives you the entire row.

What more did youwant?
0
 

Author Comment

by:Eddie_Aeffect
ID: 18858725
I ran the InTable function and it was telling me that it was not in a table. My fault. I actually ended up rewriting my code to store the values into an array and then creating a table in my second document and filling it. No need to keep switching between documents if I don't have to. thanks for all the help!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now