# VBA for MS Word



## davidku (Apr 25, 2012)

Hi,

I am trying to find the first and the last row in a PAGE of the entire document. Having been exploring the Object Browser and tested some codes. However, I am not able to get it right.

Is there a function to find the first / last row in a page OR I need to write my own ?

Hope experts here can shed some lights. Thanks.


----------



## Macropod (Apr 25, 2012)

You can use something like:

```
Dim MyRange As Range
Set MyRange = ActiveDocument.Range(0,0)
Set MyRange = MyRange.GoTo(What:=wdGoToPage, Name:="4")
Set MyRange = MyRange.GoTo(What:=wdGoToBookmark, Name:="\page")
MyRange.Collapse wdCollapseEnd
```


----------



## davidku (Apr 25, 2012)

Hi Macropod,

Thanks for your code but I am not too sure how to test it. I tried running step by step in debug mode but it did not show me anything or able to capture the last row.


----------



## Macropod (Apr 25, 2012)

Hi David,

With the code I posted, the line:
Set MyRange = MyRange.GoTo(What:=wdGoToBookmark, Name:="\page")
sets the range to the whole page. The line:
MyRange.Collapse wdCollapseEnd
then collapses the range to the last character on the page. You can use:
MyRange.Collapse wdCollapseStart
to collapse the range to the first character on the page. You can then use other techniques to extend the range to get the whole of whichever line you're after.


----------



## davidku (Apr 25, 2012)

Thanks ... this is pretty interesting. I expanded the code a bit to delete the last line. May not be fool proof but should be a good start.


```
Set MyRange = ActiveDocument.Range(0, 0)
Set MyRange = MyRange.GoTo(What:=wdGoToPage, Name:="6")
Set MyRange = MyRange.GoTo(What:=wdGoToBookmark, Name:="\page")
MyRange.Collapse wdCollapseEnd
MyRange.Select
Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
```

The Word document I have is converted from PDF to Word, so it inherits all the page number which is not part of the footer and I need to remove it to recreate the raw file. Kind of a challenge ...

I need to do a wordcount to make sure the character I removed should not more than 3 digit. So, I won't delete the wrong thing.

This is my first MS Word VBA ... seems a bit tougher than Excel.


----------



## Macropod (Apr 25, 2012)

So why not use Find/Replace to look for all 1-3 digit paragraphs and delete them? Seems a whole lot simpler than what you're trying to do now ...


----------



## davidku (Apr 26, 2012)

Good suggestion, but I can't find a pattern for start of the paragraph. I found start of a word and I just discovered that at the end of the page number, there's a special character which failed my word len check.

You have any suggestions to tweak the code to scan for start of the paragraph ?


```
With Selection.Find
        .Text = "<[0-9][0-9][0-9]??"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindAsk
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With
    Selection.Find.Execute
```


----------



## davidku (Apr 26, 2012)

Yup, hope some expert here can help.

VBA for Word is tougher than EXCEL and not much examples can be found. Probably, someone can write a guide and sell it


----------



## Macropod (Apr 26, 2012)

There is a _lot_ of material about for Word. If you look for it somewhere other than in an Excel-oriented forum.

For this task, you don't even need a macro. All you need is a _wildcard_ Find/Replace, where:
Find = ^13[0-9]{1,3}^13
Replace = ^p


----------



## davidku (Apr 26, 2012)

Hi Macropod,

Thanks for your help. I tried the search and replace earlier before heading to macro. Have been exploring the Special option under Find but not able to get it done.

I guess, I'll just manually remove those lines for now and will check out other Word help later.


----------



## davidku (Apr 25, 2012)

Hi,

I am trying to find the first and the last row in a PAGE of the entire document. Having been exploring the Object Browser and tested some codes. However, I am not able to get it right.

Is there a function to find the first / last row in a page OR I need to write my own ?

Hope experts here can shed some lights. Thanks.


----------



## Macropod (Apr 27, 2012)

Did you check the 'use wildacrds' option?


----------



## Macropod (Apr 27, 2012)

Cross-posted at: http://www.msofficeforums.com/word/12462-using-find-replace-vba.html
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184


----------



## davidku (Apr 27, 2012)

Yeah, I refer to the guide here and just hit Ctrl F 



> http://www.datamystic.com/textpipe/htmlhelp/replace menu/wordpattern.htm



Inputted <[0-9]{1,4}

You can look at the screen shot here.

http://tinypic.com/r/e7c8q9/6

I suspect there's is a special character at the end of the page number.


----------



## Macropod (Apr 27, 2012)

Without placing too fine a point on it:
1. Your find expression is not what I posted.
2. There is no evidence from your pic that you're doing anything more than a Find (Ctrl-F is for Find, Ctrl-H is for Find&Replace) - there is no 'Replace' expression visible; and
3. There is no evidence from your pic that you've set the 'use wildcards' option.

You can't realistically expect to get a meaningful result if you ignore the advice you've been given.


----------



## davidku (Apr 27, 2012)

Macropod,

Thanks for pointing out. Your Find/Replace works !

I just clicked on the "Use Wildcards". Silly me, I coded the expression and found nothing. Not sure what's wrong. It is just the option settings. No wonder, I input all the regular expression and it just don't work.

Sorry, I am just a newbie in MS Word and I appreciate your feedback a lot.
I learn a valuable lesson here. Thanks again.


----------



## davidku (Apr 27, 2012)

Let me add another post here so that it can be useful to others as well ..

Here are the interpretation for ^13 and ^p. 


```
http://support.microsoft.com/kb/214204
```

I started the wrong direction, but now, I'll grab a copy of Microsoft Words to explore in details. I don't want to remove the line number one by one for over 300 pages.


----------

