I am using the following to find the table number for a word phrase in an MS Word Table. This allows me to determine a reference point to tell the code what cell or cells should be located to extract from word into an access table as in some files the table number is <x> and in other files, the data will be found in table number <y>.
This was sufficient until today when I started working with files that had a different format/table layout than what I originally coded for. Now need to know more than just the table number, need to know the Row and possibly the col so that my code can then locate the proper cell to extract from Word into an Access table. I was hoping that since the above code can find the word and identify the table, that it could also tell me the Row and Col number for the cell containing the found item. Not sure about the syntax. Most of the searches I've done keep bringing up excel examples for excel tables, so my search terms must not be right as it is taking the keywords out of context. I also tried prefixing my search with "MS Word" and it still didn't seem to give too many relevant results - or ones that I can make sense of. The ones I found talked about the selection in word and since I am doing from Access, to my knowledge, I'm not actually clicking anything in word for the cursor to find relative to where the selection is.
Code:
Function FindWordTable(WordDoc As Object, pFindTxt As String, Optional NoTrim As Boolean, Optional SearchForward As Boolean = True)
'Find the table number containing the search term
'https://stackoverflow.com/questions/67059729/vba-search-a-word-document-for-a-table-containing-a-specific-string-and-return
'and FindInWordDoc from DocControl.accdb for use as a called function
'20231024
Dim tno As Integer
With WordDoc
For tno = 1 To .tables.Count
With .tables(tno).Range.Find
.Text = pFindTxt '"Daily Report"
.Execute
If .found Then
FindWordTable = tno
Exit Function
End If
End With
Next
End With
End Function
This was sufficient until today when I started working with files that had a different format/table layout than what I originally coded for. Now need to know more than just the table number, need to know the Row and possibly the col so that my code can then locate the proper cell to extract from Word into an Access table. I was hoping that since the above code can find the word and identify the table, that it could also tell me the Row and Col number for the cell containing the found item. Not sure about the syntax. Most of the searches I've done keep bringing up excel examples for excel tables, so my search terms must not be right as it is taking the keywords out of context. I also tried prefixing my search with "MS Word" and it still didn't seem to give too many relevant results - or ones that I can make sense of. The ones I found talked about the selection in word and since I am doing from Access, to my knowledge, I'm not actually clicking anything in word for the cursor to find relative to where the selection is.