Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: Macros (2000)

  1. #1
    Lounger
    Join Date
    Jul 2005
    Location
    Benbrook, Texas, USA
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Macros (2000)

    Is there a way to write a macro that will delete every page in the document if the page doesn't have a specific word or phrase on it?

    I have a list of military members that I have to sort through to find the names of members of my unit. There are over 900 pages in the document and hitting control F, highlighting the page, copying, and pasting just takes too long and it is easy to miss pages. Unfortunately the members of my unit are not on successive pages, they are scatered all thorough the document. The name of the unit is NMCB 28. If the page has NMCB 28 on it, I need all the information from that particular page. Hope you can help.

    I thank you and your conuntry thanks you for your help.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macros (2000)

    Welcome to Woody's Lounge!

    Here is a macro that extracts the pages on which the text is found to a new document:

    Sub Extract()
    Dim colPages As New Collection
    Dim docSrc As Document
    Dim docTrg As Document
    Dim itm As Variant
    Dim rng As Range

    Selection.HomeKey Unit:=wdStory

    With Selection.Find
    .ClearFormatting
    ' The text to search for
    .Text = "NMCB 28"
    .MatchCase = False
    .MatchWholeWord = True
    .MatchWildcards = False
    Do While .Execute
    colPages.Add Item:=Selection.Information(wdActiveEndPageNumber)
    Loop
    End With

    If colPages.Count = 0 Then Exit Sub

    Set docSrc = ActiveDocument
    Set docTrg = Documents.Add
    docSrc.Activate
    Selection.HomeKey
    For Each itm In colPages
    Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=itm
    docSrc.Bookmarks("page").Range.Copy
    Set rng = docTrg.Content
    rng.Collapse Direction:=wdCollapseEnd
    rng.Paste
    Next itm

    docTrg.Activate
    End Sub

    BTW not all Loungers are US citizens <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

  3. #3
    Lounger
    Join Date
    Jul 2005
    Location
    Benbrook, Texas, USA
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Macros (2000)

    I didn't realize that. Sorry, the USNavy thanks you.

  4. #4
    Lounger
    Join Date
    Jul 2005
    Location
    Benbrook, Texas, USA
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Macros (2000)

    That works, but it messes up the pages that it keeps. How can I use the macro and keep the pages the same as they were? Apparently there are no page breaks on any of the pages so when it sorts the document, it puts the last line of the original page on a page by itself.

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macros (2000)

    I fear that it will be difficult to keep the pages as they are, since pages are not natural units in Word; a document is repaginated on the fly. You could insert a line

    rng.InsertParagraphAfter

    below the line

    rng.Collapse Direction:=wdCollapseEnd

  6. #6
    Lounger
    Join Date
    Jul 2005
    Location
    Benbrook, Texas, USA
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Macros (2000)

    That didn't work. Probably because there is a space in the original page prior to the last line. It also changes the page from landscape to portrait.

    Once I get this working, how do I make it a button that can be clicked, instead of having to go to macros --> run macro etc...?

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macros (2000)

    Try

    rng.InsertBreak wdPageBreak

    instead. You'll end up with a blank page at the beginning, you could add a test to avoid that, or remove it at the end of the macro.

    You can assign the macro to a custom toolbar button, or to a keyboard shortcut. For a button:
    - Select Tools | Customize...
    - Activate the Commands tab if necessary.
    - Select Macros in the list of Categories on the left.
    - Select Extract in the list of Commands on the right.
    - Drag it to a convenient location on a toolbar and drop it there.
    - Right-click the new button to set its properties (you can display text or an icon or both, set the caption, etc.)
    And for a keyboard shortcut:
    - Select Tools | Customize...
    - Click Keyboard.
    - Select Macros in the list of Categories on the left.
    - Select Extract in the list of Commands on the right.
    - Click in the 'Press new shortcut key' box.
    - Press the shortcut key, preferably one you don't use for other purposes.

  8. #8
    Lounger
    Join Date
    Jul 2005
    Location
    Benbrook, Texas, USA
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Macros (2000)

    Here is an example of one of the pages in question. They do have page breaks. It is the layout that is making the extra page. The top and bottom margins should be 0.3", the left and right margins should be 1", and it should be in landscape. When it extracts the information, it sets the page up with top and bottom margins with 1" top and bottom margins, left and right margins with 1.25", and in portrait.
    Attached Files Attached Files

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macros (2000)

    Try the following:
    - Make a copy of your document.
    - Remove all contents, then save it.
    - Change the line

    Set docTrg = Documents.Add

    to

    Set docTrg = Documents.Open("C:WordModel.doc")

    where C:WordModel.doc is the path+filename of the blank document you just created.

  10. #10
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Macros (2000)

    I wonder if this might not be easier if the code that searches the document simply set some character attribute for the Entire page when it finds a match (e.g. blue underline), you could then use a second Edit/Replace to replace all non blue-underline text in the document with Nothing, and then remove the underline to finish.

    StuartR

  11. #11
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macros (2000)

    Sounds good - feel free to write a macro <img src=/S/evilgrin.gif border=0 alt=evilgrin width=15 height=15>

  12. #12
    Lounger
    Join Date
    Jul 2005
    Location
    Benbrook, Texas, USA
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Macros (2000)

    Isn't there a way to add a couple of lines to the macro to tell it to set the margins and layout, of the new document, to the desired limits?

    I would like to make this as easy and painless as possible because I am going to pas this job on to the next new person that comes on board. It would be far easier to have them simply click a button and have the macro do all the work instead of them having to create a new document.

    I really appreciate your help. This is a great site. I have already told coworkers about it and I just joined a couple of hours ago.

    Thanks

  13. #13
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macros (2000)

    You can add these lines after creating or opening the target document:

    With docTrg.PageSetup
    .Orientation = wdOrientLandscape
    .LeftMargin = InchesToPoints(1)
    .RightMargin = InchesToPoints(1)
    .TopMargin = InchesToPoints(0.3)
    .BottomMargin = InchesToPoints(0.3)
    End Sub

  14. #14
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Macros (2000)

    Compare the two documents: are the margins different? Are the fonts of the relevant styles different? To make the pagination match, both should be the same.

    One could try to modify the original document, as long as it has a simple format. In particular, as long as it consists of only one section, all pages in the same orientation (portrait or landscape) and straightforward page numbering (no stops and starts). Try this:

    <code>Sub NukePagesWithoutFoundText()
    Dim strKeepPages As String
    With Selection
    .HomeKey Unit:=wdStory
    ' Store all pages that contain the found string
    With .Find
    .ClearFormatting
    ' The text to search for
    .Text = "NMCB 28"
    .MatchCase = False
    .MatchWholeWord = True
    .MatchWildcards = False
    Do While .Execute
    strKeepPages = strKeepPages & "|" & _
    CStr(Selection.Information(wdActiveEndPageNumber))
    Loop
    End With
    If strKeepPages = vbNullString Then Exit Sub
    strKeepPages = strKeepPages & "|"
    Debug.Print strKeepPages

    ' Delete all pages not in the keep list
    .EndKey wdStory
    Do
    If InStr(1, strKeepPages, "|" & _
    CStr(.Information(wdActiveEndPageNumber)) & "|") = 0 Then
    ' This page is not a keeper!
    ' Select the page using the page bookmark and delete
    Debug.Print "Deleting page: " & .Information(wdActiveEndPageNumber)
    .GoTo What:=wdGoToBookmark, Name:="page"
    .Delete
    End If
    ' Advance to previous page and try again, unless it is page 1 already
    If .Information(wdActiveEndPageNumber) = 1 Then Exit Do
    .GoTo What:=wdGoToPage, Which:=wdGoToPrevious
    Loop
    End With
    End Sub</code>

    Note that those Debug.Print statements are for testing on modest sized documents. The Immediate window cannot hold 900 lines of text (later lines will bump earlier lines into oblivion).

    As for how to run a macro more conveniently, you can use Tools>Customize..., Command tab, Macros, and drag the macro name to a toolbar or the menu bar to create a button.

  15. #15
    Lounger
    Join Date
    Jul 2005
    Location
    Benbrook, Texas, USA
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Macros (2000)

    HUH!? I don't think the next guy (or girl) will be able to remember all of this.

    Can't those lines go into the original macro? I would like to keep it simple for the person having to sort the information. If there is a way to put them into the Extract macro, I'd rather do it that way.

    Sorry to be such a pain.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •