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
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
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
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
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
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