[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:confused:
Re: VBA - Deleting last page in document
Even this doesn't work.
Code:
Selection.EndKey Unit:=wdStory
Selection.Bookmarks("\Page").Range.Delete
Any ideas?
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
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!
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
Re: VBA - Deleting last page in document
you can try like
vb Code:
With ActiveDocument
Set r = .GoTo(wdGoToPage, wdGoToLast)
Set r = .Range(r.Start - 1, .Characters.Count)
r.Delete
End With
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
Re: VBA - Deleting last page in document
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
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
Re: VBA - Deleting last page in document
update:
Copy and paste in now works but still says out of range.
Damn thing.
Re: VBA - Deleting last page in document
Quote:
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
1 Attachment(s)
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
Re: VBA - Deleting last page in document
i changed the code a little and tested with your document
vb Code:
With ActiveDocument
strt = .GoTo(wdGoToPage, wdGoToLast).Start
Set r = .Range(strt - 1, .Range.End)
r.Delete
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
Re: VBA - Deleting last page in document
excellent, works a treat. Thanks so for this
Martin