Finding text (URGENT)

C

Cabong

Hiya all,

I've been trying to find specific text within a table and return it'
location so I can edit it.
Also I'm trying to access a specific cell in the table, no luck ther
either.

So my question is, how do you return the position of "found" text in
table and how do you access it (to modify it) after?

the command MyTable.cells(1).range.text seems to be giving me ever
single entry in the table.

Can someone help me??

Than
 
P

Peter Hewett

Hi Cabong

This code will do a find operation on the first table - table 1. If you want to use
another table in your document change the statement ".Tables(1)":

Public Sub FindInTable()
Dim rngFind As Word.Range
Dim rngToUse As Word.Range

' Search the first table in the document
Set rngFind = ActiveDocument.Tables(1).Range

' Setup for the search
With rngFind.Find
.ClearFormatting
.Text = "Text to be found"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False

' Find the required text
Do While .Execute
Set rngToUse = rngFind.Duplicate

' Use the rngTo use to do what you want
Loop
End With
End Sub

The line of code:
MsgBox MyTable.cells(1).range.text

is invalid an wont even execute so I don't know how it can possibly return every single
entry in the table. The problem is that table do not have a Cells property they have a
Cell method. The Cell method has 2 mandatory arguments (Row, Column) not 1. A Table
object has Rows and Columns properties. Both of these objects have a Cells property that
accepts 1 argument.

To reference the first cell in a table you can use something like this:
MsgBox ActiveDocument.Tables(1).Cell(1,1).Range.Text

however you need to be aware that when you return the contents of a cell in this manner
you also get a Paragraph character (Chr(13)) and an end of cell character (Chr(7)). So to
work with the text you need to strip these two characters, something like this:

Dim strCell As String
ActiveDocument.Tables(1).Cell(1,1).Range.Text
strCell = Left$(strCell, Len(strCell)-2)

HTH + Cheers - Peter
 
C

Cabong

Thanx Peter, this has been very helpfull.

Yet I have another question:

I have to pull out from the header a date enterred. Luckly it is th
last entry in the header. Do you know how I can do this?

TI
 
P

Peter Hewett

Hi Cabong

Headers/Footers are a lot trickier to work with than the Main text Story (the documents
body text). There are 3 different Header/Footer types and each sections of your document
has it's own set of Header/Footers. So you need to know what you're working with. Also
you need a way of identifying the date in the header. Putting the date in a bookmark
makes the whole process a no brainer - because the bookmark knows which Header/Section to
use. So if possible make sure that date is bookmarked. If not then you need to work out
how you can identify the date in the header. I don't know what you mean by <Luckily it is
the last entry in the header.> I'm sorry do you mean it's the very last text? As I said
before if you can bookmark the date the rest is moot.

HTH + Cheers - Peter
 
C

Cabong

Newsgroups: microsoft.public.word.vba.general
NNTP-Posting-Host: 69-56-172-122.theplanet.com 69.56.172.122
Path: number1.nntp.ash.giganews.com!internal1.nntp.ash.giganews.com!border2.nntp.ash.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
Lines: 1
Xref: number1.nntp.ash.giganews.com microsoft.public.word.vba.general:49611


No the date is not bookmarked. Actually the documents are not mine, I'
just trying to extract the data to put it into a flat db table.
Unfortunatly my limited knowledge of Word seems to be the prlbm.

I meant that the date is the last thing to appear on the right insid
of the header. I was wondering how I can extract (even all) the tex
using a VBA object.

I noticed that they're several types of header, but regardless, I don'
seem to get the syntax right to extract text from any of them.

would it be something like:

mydoc.header(thistype).range.text?


Thanx for any hel
 
C

Cabong

Newsgroups: microsoft.public.word.vba.general
NNTP-Posting-Host: 69-56-172-122.theplanet.com 69.56.172.122
Path: number1.nntp.ash.giganews.com!internal1.nntp.ash.giganews.com!border2.nntp.ash.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
Lines: 1
Xref: number1.nntp.ash.giganews.com microsoft.public.word.vba.general:49614


No the date is not bookmarked. Actually the documents are not mine, I'
just trying to extract the data to put it into a flat db table.
Unfortunatly my limited knowledge of Word seems to be the prlbm.

I meant that the date is the last thing to appear on the right insid
of the header. I was wondering how I can extract (even all) the tex
using a VBA object.

I noticed that they're several types of header, but regardless, I don'
seem to get the syntax right to extract text from any of them.

would it be something like:

mydoc.header(thistype).range.text?


Thanx for any hel
 
P

Peter Hewett

Hi Cabong

You need to use the Documents Sections collection as the Document object does not directly
support a Header(s) object. You can use something like this:

ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text
ActiveDocument.Sections(1).Headers(wdHeaderFooterEvenPages).Range.Text
ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range.Text

You'll need to experiment with which one as it depends upon the document structure. Also
you'll need to change the section number if the Header you require is not in Section 1.

HTH + Cheers - Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top