Consulting

Results 1 to 11 of 11

Thread: Delete a page

  1. #1
    VBAX Regular
    Joined
    Sep 2007
    Posts
    21
    Location

    Delete a page

    I am working on a macro to run through a word document and find pages with the title "service team". The program that puts together the document is inserting multiples of this page. (I know the easy thing would be to fix the program, however they have no intentions of fixing this problem. ) I need to keep the first and get rid of all others. Each page is separated by a section break, next page.

    Below is what I have so far in my VBA but I need a little assistance. I have it finding the second instance and deleting out 33 lines in the word document, but what would be really neat is if it could delete out everything from the title to the section break, including the section break.

    [vba]Sub Macro2()
    Selection.HomeKey unit:=wdStory
    Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="3"
    Selection.Find.ClearFormatting
    With Selection.Find
    .Text = "Service Team"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.Find.Execute
    If Selection.Find.Found Then
    Selection.MoveDown unit:=wdParagraph, Count:=33, Extend:=wdExtend
    Selection.Delete unit:=wdCharacter, Count:=1
    Else
    Selection.HomeKey unit:=wdStory
    End If
    End Sub
    [/vba]

    I know that I need to set up the loop to run through and find any of the other occurrances as well.

    Thanks for your help with this.

    Jake

  2. #2
    VBAX Regular
    Joined
    Sep 2007
    Posts
    21
    Location
    If I had only waited until the next question was asked after mine.

    I just need to replace the lines

    Selection.MoveDown unit:=wdParagraph, Count:=33, Extend:=wdExtend
    Selection.Delete unit:=wdCharacter, Count:=1

    with

    ActiveDocument.Bookmarks("\page").Range.Select
    Selection.Delete unit:=wdCharacter, Count:=3

    Thank you for your help.

    Jake

  3. #3
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Well done for picking that up. I wish everybody was so observant!

    However, I don't fully understand how what you suggest is a solution to the problem you posed.

    Probably just me not really reading the code - Gerry will come along shortly and read it properly and tell me, I'm sure
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  4. #4
    VBAX Regular
    Joined
    Sep 2007
    Posts
    21
    Location
    Tony,

    The line

    ActiveDocument.Bookmarks("\page").Range.Select

    selects the current page, then I can deleted it. I tried it and it worked for what I needed for now. I will be back if I need more help with it though.

    Thanks for offering a hand.

    Jake

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I don't get it either.

    Using Selection is not great, or efficient. Hard to suggest anything as I don't know what:

    "find pages with the title "service team"

    actually means. Title? The document is not derscribed well enough. I think it means removing everything afte the first one is found...but I am not sure.

    Moving the Selection 33 times is very inefficient.

    Sorry Tony, I have no idea why deleting 3 characters works.

  6. #6
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Quote Originally Posted by Gerry
    Sorry Tony, I have no idea why deleting 3 characters works.
    It's a feature!
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Yeeessss.

  8. #8
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    I had a very quick look at what happens and deleting 1 character deleted the selection - except that it left a paragraph mark behind (my test page probably ended in a paragraph mark but I can't swear to that) - deleting 2 characters deleted the selected page and the paragraph mark - and deleting 3 characters also deleted the first character of the following page.

    It seems reasonable that the selection should be treated as one character - no, reasonable is not the correct word, believable - but I don't understand all of it and am going to look a bit more closely.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  9. #9
    VBAX Regular
    Joined
    Sep 2007
    Posts
    21
    Location
    Quote Originally Posted by fumei
    I don't get it either.

    Using Selection is not great, or efficient. Hard to suggest anything as I don't know what:

    "find pages with the title "service team"

    actually means. Title? The document is not derscribed well enough. I think it means removing everything afte the first one is found...but I am not sure.

    Moving the Selection 33 times is very inefficient.

    Sorry Tony, I have no idea why deleting 3 characters works.
    Sorry guys I just got back into the office.

    Let me start off first by saing sorry to Gerry that I didn't post the document for you to see, or an explanation that lived up to your understanding. I am a lot like the others that you rip into repetitively on this site for not posting the questions the way you would like. I did my best to describe it however. Let me also start off by saying that I have no training at all in VBA, I am trying to learn all of this on my own without any courses or books to read. I study the code that the recorder makes in word, and posts on sites like this, I have figured out some tips to make the code shorter, and run quicker. I have only been at this, well, since my first post on this site. (Maybe a few months ago). I also have no formal training in word/excel so from time to time I figuire something out.

    Tony was correct when he mentioned that there is an empty paragraph mark that needs to be deleted that is left behind and that there is also a page break/section break that needs to come out as well or else there would be a blank page that a user would need to delete out. So basically there needs to be 3 character deletions. I knew deleting 33 times was inefficient but for the moment to get something together to post and test with it worked fine.

    The title is just a phrase that appears first on that page in bold lettering. Just like my post said; sometimes it is that simple. I only need one occurrence of this page in the document, so the next 2-8 times I need to remove those page.

    What I have for a final works perfectly in testing, now that I have my loop all set to run through the remainder of the document looking for these pages and deleting them out.

    Thank you to the two of you for being so proficient and helpful. I will try to do a better job in the future of explaining what the document looks like and what is needed.

    Thanks,

    Jake
    Last edited by bigjaker; 11-12-2007 at 07:29 AM.

  10. #10
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I am a lot like the others that you rip into repetitively on this site for not posting the questions the way you would like.
    That is not fair at all. It may be your opinion, which you are of course entitled to.

    It is not a question of "what I like". It is a question regarding something that has NOT been stated.

    "The title is just a phrase that appears first on that page in bold lettering. Just like my post said; sometimes it is that simple. "

    Except....you never said that! Yes, it would be "simple", if you had actually stated that, but you did not. I would even been able to figure it out if you had some testing parameter on bold...but you do not have that either. You never stated that it was text that was bolded. Never.

    Read your own posts. You never said that.

    If you feel it is me "ripping" into you by asking, I am sorry about that. I ask questions when I do not know the answer.

    Let me repeat it. You NEVER stated it was the first text that appeared as bolded. Never.

    How was I supposed to know that, if you never say it? Here is EXACTLY what you wrote:

    "run through a word document and find pages with the title "service team". "

    Do you see any mention of bolded text? Hmmmm? It is ripping into, asking what that means? Word has no idea (nor do I) what "title" means, unless it is an actual Title Field.

    If you had written:

    "run through a word document and find pages starting with bolded text "service team". ....hey, then guess what? I would not have asked. I would not have "ripped" into. I would know what the full search criteria was...but you did NOT state that. So I asked.

    You write as if I was somehow magically supposed to know that without asking.

    Fine. Sorry I asked. I won't bother again. Please accept my apologies for you feeling I "ripped" into you. I will read your posts again after I find my mind-reading device, which I seemed to have misplaced.

    Oh, and just to do some more "ripping" as it seems that I am considered to be some kind of @$$h&^e, IF the first two words of each Section is "Service Team" - something by the way you also do not state (that Service Team are the first words - then you could do something like:[vba]Dim aWord
    Dim aSection As Section
    For Each aSection In ActiveDocument.Sections
    If Trim(aSection.Range.Words(1)) = "Service" And _
    Trim(aSection.Range.Words(2)) = "Team" Then
    ' do stuff with the Section
    End If
    Next
    [/vba]That would check each Section to see if the first two words are "Service Team", and if they are...do something.

    I have no formal training either. I have learned by trying stuff, and, like you, asking questions. I also learned that I got better answers when I was as precise and clear as I could be. What helped me - both by my own work and study AND when I have asked my own questions - was to be very very clear and precise. Making zero assumptions that anyone I am asking questions of knows anything about my situation unless I have actually stated it.

    I have not intended, or EVER intended, to "rip" anyone.

    Seriously, I am only trying to help. I know that I am not the most diplomatic person, and I agree I can ask better. I admit that, and I am working on it. Until then, I am sure that others can help you with any further questions you may have. I would not like you to feel that you are being "ripped" into. Again, my apologies for that.

    All the best.

  11. #11
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Oh, and finally, because I guess I really am an @$$h&^e....

    You wrote:

    "what would be really neat is if it could delete out everything from the title to the section break, including the section break."

    Try this. It may need some fussing, as...ummmm, I do not know (as I still can't find my mind-reading device), precisely how your Section breaks are configured - oh, things like headers and footers etc. etc.:
    [vba]Dim aWord
    Dim aSection As Section
    For Each aSection In ActiveDocument.Sections
    If Trim(aSection.Range.Words(1)) = "Service" And _
    Trim(aSection.Range.Words(2)) = "Team" Then
    aSection.Range.Delete
    End If
    Next[/vba]This does precisely that. It removes everything from the - ahem, "title" - to the Section break, including the Section break. Assuming of course that the first two words ARE "Service Team", which is...ummm, an assumption, since that is not actually stated, although I guess I should know that somehow.

Posting Permissions

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