VBA for MS Word

davidku

New Member
Joined
Apr 24, 2012
Messages
12
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.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
You can use something like:
Code:
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
 
Upvote 0
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.
 
Upvote 0
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.
 
Upvote 0
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.

Code:
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.
 
Upvote 0
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 ...
 
Upvote 0
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 ?

Code:
    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
 
Upvote 0
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 :)
 
Upvote 0
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
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,213,561
Messages
6,114,316
Members
448,564
Latest member
ED38

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top