Force Save as PDF

Rob_010101

Board Regular
Joined
Jul 24, 2017
Messages
198
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I know this is not necessarily Excel-related, however, it is going to be VBA related.

I have created a return-to-work form in word, which is sent to managers to complete return-to-work interviews for sick employees.

The document is restricted and only form fields can be completed. However, what I would like to do is force the user to save as a PDF, at the 'Save As' screen, so that they can ONLY save it as a PDF.

Basically, this will hopefully stop the completed form being accidentally overwritten later.

Is this even possible?

Kind Regards
Chris
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
There are a couple of ways to do this...if you're only trying to keep the form from being overwritten, save it as an .xltm
When the user opens the .xltm (template), Excel will add a character to the filename to ensure it doesn't overwrite the template.


If you do want to save as .PDF, you can use the Workbook_beforesave event. When the user clicks the save button, it will prompt them to save as PDF with no other options. For example, try this code in the thisWorkbook module:
VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim fName As String
If SaveAsUI <> False Then
    Cancel = True
    fName = Application.GetSaveAsFilename(, "PDF (*.PDF), *.PDF", , "Save As PDF file")
    If fName <> "False" Then
      Application.EnableEvents = False
      ActiveWorkbook.SaveAs Filename:=fName, FileFormat:=xlTypePDF
      Application.EnableEvents = True
    Else
      MsgBox "Cancelled.  File not saved."
      Cancel = True
      Exit Sub
    End If
End If
End Sub
 
Upvote 0
Solution
There are a couple of ways to do this...if you're only trying to keep the form from being overwritten, save it as an .xltm
When the user opens the .xltm (template), Excel will add a character to the filename to ensure it doesn't overwrite the template.


If you do want to save as .PDF, you can use the Workbook_beforesave event. When the user clicks the save button, it will prompt them to save as PDF with no other options. For example, try this code in the thisWorkbook module:
VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim fName As String
If SaveAsUI <> False Then
    Cancel = True
    fName = Application.GetSaveAsFilename(, "PDF (*.PDF), *.PDF", , "Save As PDF file")
    If fName <> "False" Then
      Application.EnableEvents = False
      ActiveWorkbook.SaveAs Filename:=fName, FileFormat:=xlTypePDF
      Application.EnableEvents = True
    Else
      MsgBox "Cancelled.  File not saved."
      Cancel = True
      Exit Sub
    End If
End If
End Sub
Hello,

I'm not too fussed about the template being overwritten, as the word document is downloaded from SharePoint by the user.

I've added your code but it's not working for me. Can I check that I'm adding it in the correct place?

Developer > Visual Basic

1664575177284.png


When 'Save As' it gives the drop-down list and all the file types.
When 'Save' it overwrites the current document in the .docm format.

Thanks
 
Upvote 0

Forum statistics

Threads
1,223,702
Messages
6,173,961
Members
452,539
Latest member
delvey

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top