Delete pages
Please would it be possible for you to help me I have blank pages from a mailmerge that I can not delete it’s a three page merge and I want to delete the last page (page4).
Sub DeleteLastPage()
Dim lngCharacters As Long
Dim r As Range
With ActiveDocument
lngCharacters = .GoTo(wdGoToPage, wdGoToLast).Start
Set r = .Range(lngCharacters - 1, .Range.End)
r.Delete
End With
End Sub
This macro above works when I run it in an individual file, however I can’t get it to run on a folder of word files in a folder, when I ask it to run using the following.
Sub UpdateDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, strDocNm As String, wdDoc As Document
strDocNm = ActiveDocument.FullName: strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.docx", vbNormal)
While strFile <> ""
If strFolder & "" & strFile <> strDocNm Then
Set wdDoc = Documents.Open(FileName:=strFolder & "" & strFile, AddToRecentFiles:=False, Visible:=False)
With wdDoc
'Call your other macro or insert its code here
.Close SaveChanges:=True
End With
End If
strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder( 0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
Many thanks
|