Results 1 to 15 of 15

Thread: [RESOLVED] VBA - Deleting last page in document

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    27

    Resolved [RESOLVED] VBA - Deleting last page in document

    Hi all,

    I have the following:

    Code:
          ActiveDocument.Repaginate
          totalpages = ActiveDocument.BuiltInDocumentProperties(wdPropertyPages)
        ActiveDocument.Range.GoTo(wdGoToPage, wdGoToAbsolute, totalpages).Bookmarks("\Page").Range.Delete
    I want this to delete the last page in the document.
    Is there any reason why it isn't?

    Thanks

    Martin

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    27

    Re: VBA - Deleting last page in document

    Even this doesn't work.
    Code:
    Selection.EndKey Unit:=wdStory
    Selection.Bookmarks("\Page").Range.Delete
    Any ideas?

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    27

    Re: VBA - Deleting last page in document

    I have sorted this:
    I used:

    Code:
    With ActiveDocument.Content.Find
    .Text = "^12"
    .Replacement.Text = ""
    .Execute Replace:=wdReplaceAll
    End With
    Thanks

  4. #4
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: VBA - Deleting last page in document

    looks like you had only "^12" on your last page!
    I'd check if that works for documents which have not!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    27

    Re: VBA - Deleting last page in document

    Hi,

    It probably wont work but for my test document it does.

    Is there a way to do it otherwise at all?
    Thanks
    Martin

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA - Deleting last page in document

    you can try like
    vb Code:
    1. With ActiveDocument
    2.     Set r = .GoTo(wdGoToPage, wdGoToLast)
    3.     Set r = .Range(r.Start - 1, .Characters.Count)
    4.     r.Delete
    5. End With
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    27

    Re: VBA - Deleting last page in document

    Hi,

    I used dim r as document

    but im getting a type mismatch on line 2

    Any reason for this?
    Thanks
    Martin

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA - Deleting last page in document

    r should be a range
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    27

    Re: VBA - Deleting last page in document

    Hi,
    thanks for the quick response.
    Now on teh next range it says out of range.
    r.start - 1 has the value of 1358
    .characters.count = 1308.

    wdgotolast = -1

    Is this right?

    Thanks for all your help on this.

    Martin

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    27

    Re: VBA - Deleting last page in document

    Just an update.
    If I use this line:

    Target.Range.PasteSpecial DataType:=wdPasteText

    then the 3rd page doesn't get created but the image doesn't come across - its driving me nuts

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    27

    Re: VBA - Deleting last page in document

    update:

    Copy and paste in now works but still says out of range.

    Damn thing.

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA - Deleting last page in document

    Now on teh next range it says out of range.
    r.start - 1 has the value of 1358
    .characters.count = 1308.
    post a sample document to demonstrate this problem, i tested with a document before posting
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    27

    Re: VBA - Deleting last page in document

    Hi,

    here is an example of the letter and also the code which copy's the pages out and then deletes the last page.

    Thanks for this.

    Martin

    Code:
    Sub findtext(control As IRibbonControl)
    
    Dim sText As String
    Dim sValues() As String
    Dim cuttext As String
    Dim cuttext2 As String
    Dim strdocname1 As String
    Dim type1 As String
    Dim Counter As Long, Source As Document, Target As Document
    Dim totalpages As String, totalpages2 As String
    Dim objRngPg As Document
    Dim r As Range
    
    
    Set Source = ActiveDocument
    Selection.HomeKey Unit:=wdStory
    pages = Source.BuiltInDocumentProperties(wdPropertyPages)
    Counter = 0
    
    While Counter < pages
        Counter = Counter + 1
        Source.Bookmarks("\Page").Range.Cut
    
        Set Target = Documents.Add
        'Target.Range.PasteSpecial DataType:=wdPasteText
    Target.Range.PasteAndFormat wdPasteDefault
    
    
    With ActiveDocument.Content.Find
    .Text = "^12"
    .Replacement.Text = ""
    .Execute Replace:=wdReplaceAll
    End With
    
    '    With ActiveDocument
    '        Set r = .GoTo(wdGoToPage, wdGoToLast)
    '        Set r = .Range(r.Start - 1, .Characters.Count)
    '        r.Delete
    '    End With
    
    ' Clear formatting from previous searches
    ' Application.Selection.ClearFormatting
    ' Find "Name: "
    Application.Selection.Find.Execute "Ref: "
    ' Select whole line
    Application.Selection.Expand wdLine
    ' Assign text to variable
    sText = Application.Selection.Text
    sText = Replace(sText, " ", "")
    type1 = Right(sText, 4)
    type2 = Left(type1, 3)
    
    cuttext = Left(sText, 14)
    cuttext2 = Right(cuttext, 10)
    
              totalpages2 = ActiveDocument.BuiltInDocumentProperties(wdPropertyPages)
              
            strdocname1 = "\\documotive\documotive$\Documotive AutoFiler\Drop\AR\" & type2 & " Letter" & "_" & cuttext2 & "_" & Format(Date, "dd-mm-yyyy") & ".doc"
    
     'Save file with new extension
        Target.SaveAs FileName:=strdocname1, ReadOnlyRecommended:=True, FileFormat:=wdFormatDocument
    Target.Close
    
    Wend
     
    End Sub
    Attached Files Attached Files

  14. #14
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA - Deleting last page in document

    i changed the code a little and tested with your document
    vb Code:
    1. With ActiveDocument
    2.         strt = .GoTo(wdGoToPage, wdGoToLast).Start
    3.         Set r = .Range(strt - 1, .Range.End)
    4.         r.Delete
    5.     End With
    -1 may not be required, but if not used, you may end up with an empty last page

    characters.count must not count all characters
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    27

    Re: VBA - Deleting last page in document

    excellent, works a treat. Thanks so for this
    Martin

Posting Permissions

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



Click Here to Expand Forum to Full Width