Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-23-2020, 09:12 AM
kalton kalton is offline Windows 10 Office 2016
Novice
Delete last page of .docx and then save as .pdf for all the files in a folder
 
Join Date: Jan 2020
Posts: 1
kalton is on a distinguished road
Default Delete last page of .docx and then save as .pdf for all the files in a folder


Currently, I am trying to write something in word VBA to delete the last page for a document and then save it as a pdf before moving onto the next one. The macro runs for about ~15 documents before giving the error runtime error 5904: cannot edit range. This is a little concerning as I have ~350 documents that need to have this done to them.

Does anyone have any ideas on what to do?

Code:
Sub ConvertWordsToPdfs()

    Dim directory As String
    directory = "C:\Users\kalton\Documents\TEST" ' The starting directory

    Dim fso, folder, files
    Set fso = CreateObject("Scripting.FileSystemObject")

    Set folder = fso.GetFolder(directory)
    Set files = folder.files

    For Each File In files

        Dim newName As String
        newName = Replace(File.Path, ".docx", ".pdf")

        Documents.Open FileName:=File.Path, _
            ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
            PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
            WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
            wdOpenFormatAuto, XMLTransform:=""

            DeleteLastPage

        ActiveDocument.ExportAsFixedFormat OutputFileName:=newName, _
            ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
            wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
            Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
            CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
            BitmapMissingFonts:=True, UseISO19005_1:=False
        ActiveDocument.Close savechanges:=False

    Next

End Sub

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

Last edited by macropod; 01-23-2020 at 09:19 PM. Reason: Added code tags
Reply With Quote
  #2  
Old 01-23-2020, 09:15 PM
macropod's Avatar
macropod macropod is offline Delete last page of .docx and then save as .pdf for all the files in a folder Windows 7 64bit Delete last page of .docx and then save as .pdf for all the files in a folder Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Cross-posted at: ms word - Creating a loop for opening documents, executing macros and then close - Stack Overflow
For cross-posting etiquette, please read: Excelguru Help Site - A message to forum cross posters
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 01-23-2020, 10:05 PM
Guessed's Avatar
Guessed Guessed is offline Delete last page of .docx and then save as .pdf for all the files in a folder Windows 10 Delete last page of .docx and then save as .pdf for all the files in a folder Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

To me, the requirement to remove the last page is a bit complicated.

If the last page is preceded by a hard page break for instance, deleting the last page would retain the last page but clear the content from it (other than an empty paragraph). The same thing would happen if the preceding page ended with a table right at the bottom of the page since a paragraph mark needs to be the final thing in the file. If the last page happened to include a section break, the page setup/headers/footers on preceding pages would potentially change.

You could potentially avoid these issues by exporting and specifying the page range to exclude the last page.

And then you have the issue of what to do with a single page document...
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete last page of .docx and then save as .pdf for all the files in a folder Web Page, Filtered. 2010. Save as .docx JonJacobs Word 3 12-14-2018 10:38 AM
Looping through a folder of PDF files and saving them as docx ballpoint Word VBA 5 11-22-2018 05:18 AM
Delete last page of .docx and then save as .pdf for all the files in a folder Run Code on all files and save files as .docx Plokimu77 Word VBA 4 06-05-2016 04:41 PM
Delete last page of .docx and then save as .pdf for all the files in a folder Macro to change all text color to black in all docx files in a selected folder joewoods Word VBA 13 05-16-2016 06:29 PM
Macro to change/convert/delete txt files in folder+subfolders NoS Word VBA 4 03-03-2016 12:10 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:00 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft