Moving around and selecting text in word

User avatar
pmatz
StarLounger
Posts: 71
Joined: 20 Feb 2010, 10:31
Location: UK

Moving around and selecting text in word

Post by pmatz »

Hi,

I have about 100 forms to sort through and get various information from, they are all of the same template.

Being a fair VBA-er, I am developing a sub to step through each form in the folder and save the result in a new doc for the entire batch of templates.

I am using the following code to get my 1st string, which is fairly straightforward:

Code: Select all

Selection.GoTo wdGoToHeading, wdGoToAbsolute, 3
Selection.GoTo wdGoToTable, wdGoToNext
Selection.Move wdCell, 2
Selection.SelectCell
str1 = Selection
str1 = Replace(str1, "_", "")
str1 = Replace(str1, "(Yes/No)", "")
str1 = Left(str1, Len(str1) - 2)
However on the second string i need, the text in the last paragraph of a cell in a table is what I need. I am having trouble getting that! So far I have:

Code: Select all

'SysImpact
Selection.GoTo wdGoToHeading, wdGoToAbsolute, 4
Selection.GoTo wdGoToTable, wdGoToNext
Selection.Move wdCell, 1
which gets to to the right cell, but then how to select the last paragraph and return as a string is flummoxed me! In the cell there is typically 1-2 paragraps of text, an image, then a last paragraph.

I know it must be simple.... :hairout:

Thanks
thanks, Paul.

User avatar
HansV
Administrator
Posts: 78416
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Moving around and selecting text in word

Post by HansV »

Try something like this:

Code: Select all

With Selection.Cells(1).Range
  str1 = .Paragraphs(.Paragraphs.Count).Text
End With
str1 = Left(str1, Len(str1) - 2)
The last line removes the last two characters from the text of the paragraph (the paragraph marker and the cell end marker)
Best wishes,
Hans

User avatar
pmatz
StarLounger
Posts: 71
Joined: 20 Feb 2010, 10:31
Location: UK

Re: Moving around and selecting text in word

Post by pmatz »

Perfect - thanks Hans :)

I can't use the .Text in Office 2002, but that works fine.
thanks, Paul.

User avatar
HansV
Administrator
Posts: 78416
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Moving around and selecting text in word

Post by HansV »

Oops, it should have been

str1 = .Paragraphs(.Paragraphs.Count).Range.Text
Best wishes,
Hans