Link to home
Start Free TrialLog in
Avatar of WonHop
WonHopFlag for United States of America

asked on

MS Word. Capture the Table Title

This is question 3 of 3 that I am asking.   Microsoft Word 2016 VBA.  I am using fake data because I can't put company data out here.

I will be searching for "TBDs".  If it is found in a table, I need to capture the Title of the Table in a variable.
I already have code to let me know that it is in a table.
In the attached file, I need to capture "TABLE C-1  TO BE DETERMINED ITEMS" in a variable.

Thanks
WonHop
EE_Sample_01.docx
Avatar of irudyk
irudyk
Flag of Canada image

If the variable is strTableTitle then the following should work:

With Selection
    .Tables(1).Cell(1,1).Select
    .Previous(wdParagraph,1).Select
    .MoveLeft wdcharacter,1,wdExtend
    strTableTitle = .Text
End With

Open in new window

Avatar of WonHop

ASKER

Hello irudyk.  My question 2 and 3 are getting the same 5981 error message.  I have attached a photo of the error message.

 .Tables(1).Cell(1,1).Select

The thing is, there could be over 40 table in the document that is over 125 pages.  That is why I need to be able to identify that particular table so that I get the correct data from the correct table.

Thanks
WonHop
Error_Message.JPG
Okay, you mentioned in your question that your code is finding the table. If so, the your selection is within a table. So,

With Selection
    .Tables(1).Cell(1,1).Select 'this line will select the first cell of the table you are in

If you are getting a message, I am assuming that your selection is not in a table to start with.

Selection.Tables(1) allows you to reference the current table that your selection is in. You only need an index value if you refer to the table using ActiveDocument.Table(x) where x is the index value.

So, can you confirm that your routine is putting your selection within a table prior to the code I provided is run?
Avatar of WonHop

ASKER

You are correct.  The code below tells me that it is in a Table, but it does not actually select the Table or cell.  How do I get it to select the table/cell?  When I manually selected the table, the code worked.

If FindRange.Information(Word.WdInformation.wdWithInTable) = True Then  
      Do Stuff
End if
Interesting, when I try and run the first line of code I get an error. Is it part of a Find routine?
I believe FindRange is a variable, correct?
I've tried a few permutations of being in a table and selecting the entire table. Either way if I'm in a table the code I posted works for me.

Which line in the code does it error out on?
Avatar of WonHop

ASKER

Yes it is.  I run this out of MS Access.  Lots of code.  I am using code from a previous application, but it is using the same documents to do a different job.  The other code looked for stuff and it captured the footer page numbers.  See partial code  below.  I added that to a MS Access Table.   The code found the data and let me know if it was in a table or not.  

FindRange ' TBD"
Do While FindRange.Find.Execute() = True
        pdPage = FindRange.Information(wdActiveEndPageNumber)
        Selection.Goto wdGoToPage, wdGoToAbsolute, pdPage
        pageNumberText = FindRange.Sections(1).Footers(WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Text

        If FindRange.Information(Word.WdInformation.wdWithInTable) = True Then  
             Do Stuff
         End if

Loop
Avatar of WonHop

ASKER

The error happens here.
Tables(1).Cell(1,1).Select
Okay, are you using the With Selection block?

Tables(1).Cell(1,1).Select would try to select the first cell of the first table in the document

You are also missing the period at the start.

Let's remove that possibility. Try using the following:

Selection.Tables(1).Cell(1,1).Select
Selection.Previous(wdParagraph,1).Select
Selection.MoveLeft wdcharacter,1,wdExtend
strTableTitle = Selection.Text

Open in new window

Avatar of WonHop

ASKER

Same error message in the same spot.   It is not selecting / setting focus on the Table
Selection.Tables(1).Cell(1, 1).Select

That was part of my original question.  How do we know that Table is index (1) or index (30)?
I believe the reason is because I’m not referencing your range object as you are doing this through Access.  I’ll have to look at my coding to try and fix that.
Try replacing Selection with your variable FindRange
Avatar of WonHop

ASKER

It works great in the small file where it is Table 1.  Not in the big file.  It did find it in the big file, but for the Title, it chose the data in the cell to the Left.
450 W  --   422 W <TBD 5-1>
it chose "450 W" as the Title on page 47 which is the correct place in the big file.
 
So, now that it is at least getting data that is found from the correct table, how do we get the index of that table
But this code does not work either way.
FindRange.MoveLeft wdCharacter, 1, wdExtend or Selection.MoveLeft wdCharacter, 1, wdExtend
Replace wdCharacter with the number 1
Replace wdExtend with the number 1
Like so,
FindRange.MoveLeft 1, 1, 1
Also wdParagraph should be replaced with the number 4
Avatar of WonHop

ASKER

Exact same results.
Also. MoveLeft is not a method of FindRange.  That is the error message.
How is FindRange defined? I presume FindRange is a user defined function. How is it defined? Does it return a Selection object or a Range object or some other type of object. Without knowing that, it’s difficult to determine why my code is resulting in an error.
Avatar of WonHop

ASKER

Sorry for the delay.  I was off on Monday.

    Dim FindRange As Word.Range
    Set FindRange = ActiveDocument.Range
ASKER CERTIFIED SOLUTION
Avatar of irudyk
irudyk
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WonHop

ASKER

Great.  That code works great.  But I have two other questions for this process.  If you prefer for me to ask in another question, I will be more than happy to do that because you have been a great help.

The other things I need are
    How to capture the current cell location, Column and Row?
    After it moves to Cell 1,1, how do I select the original cell?
See below.

        If FindRange.Information(Word.WdInformation.wdWithInTable) = True Then
               
               Get current location
                strColumn = Current Column
                strRow = Current Row

            FindRange.Tables(1).Cell(1, 1).Select
            Selection.Previous(wdParagraph, 1).Select
            Selection.MoveLeft wdCharacter, 1, wdExtend
            strTableTitle = Selection.Text
     
                  Go back to original location.
                  FindRange.Tables(1).Cell(strColumn , strRow ).Select

        End If

The reason I need to go back to the original location is, the code is looping and finding all of the TBDs.  I don't want it to keep finding the same one all the time.

Thanks
Nate
Okay, when I ran it, I presumed that that the .FInd was being done of the FindRange object. If so, then you won't keep finding the same text, because the find is being performed on an object that is outside of whatever you are actually selecting within the document itself

However, if you want the current cell attributes, you can get them as follows:

strColumn = FindRange.Information(wdStartOfRangeColumnNumber)
strRow = FindRange.Information(wdStartOfRangeRowNumber)

Open in new window

Avatar of WonHop

ASKER

Your were correct.  The code does loop thru the document and the other tables with the code code below.  I was able to verify while stepping thru it with the strColumn  and strRow code.  It showed that it was moving to the next cell.


        If FindRange.Information(Word.WdInformation.wdWithInTable) = True Then
            strColumn = FindRange.Information(wdStartOfRangeColumnNumber)
            strRow = FindRange.Information(wdStartOfRangeRowNumber)
            FindRange.Tables(1).Cell(1, 1).Select
            Selection.Previous(wdParagraph, 1).Select
            Selection.MoveLeft wdCharacter, 1, wdExtend
            strTableTitle = Selection.Text
        End If

Please stand by over the next few weeks.  This is just the start of this project and I am sure I will have many more questions.  
Part of which will be finding a certain cell and doing Rev Tracking in that cell.  But, that is down the road.  I will need to update the first part of this code before I move on.

Thank you very much.
:0)
WonHop