JohnnyBravo
02-28-2019, 12:15 PM
I'm running Word 2016 version and I'm having an issue with a VBA routine I found on line. All i'm trying to do is to save as pdf. The routine works fine if the word doc is brand new and it hasn't been saving before. The routine checks to see if the document has been saved before, let's call it "Sales Proposal 1" and if it has, it prompts the user with 3 choices: "Sales 1 proposal" is already on your PC, do you wish to overwrite it? YES, NO, Cancel.
The error message is generated whenever I click on 'NO' it gives the following message:
invalid procedure call or argument
It's seems to be having an issue with the 'SaveAs2' part towards the bottom of the script below in the functions section. (The bold & underline is my emphasis only so you can spot it quicker).
Any assistance would be greatly appreciated. Thanks.
Here is the VBA routine:
=========================================================================== ================================
Sub Word_ExportPDF()
' PURPOSE: Generate A PDF Document From Current Word Document
' NOTES: PDF Will Be Saved To Same Folder As Word Document File
' SOURCE: www.TheSpreadsheetGuru.com/the-code-vault (http://www.TheSpreadsheetGuru.com/the-code-vault)
Dim CurrentFolder As String
Dim FileName As String
Dim myPath As String
Dim UniqueName As Boolean
UniqueName = False
' Store Information About Word File
myPath = ActiveDocument.FullName
CurrentFolder = ActiveDocument.Path & ""
FileName = Mid(myPath, InStrRev(myPath, "") + 1, _
InStrRev(myPath, ".") - InStrRev(myPath, "") - 1)
' Does File Already Exist?
Do While UniqueName = False
DirFile = CurrentFolder & FileName & ".pdf"
If Len(Dir(DirFile)) <> 0 Then
UserAnswer = MsgBox("Filename Already Exists! Click " & _
"[Yes] to override. Click [No] to Rename.", vbYesNoCancel).
If UserAnswer = vbYes Then
UniqueName = True
ElseIf UserAnswer = vbNo Then
Do
' Retrieve New File Name
FileName = InputBox("Provide New File Name " & _
"(will ask again if you provide an invalid file name)", _
"Enter File Name", FileName)
' Exit if User Wants To
If FileName = "False" Or FileName = "" Then Exit Sub
Loop While ValidFileName(FileName) = False
Else
Exit Sub 'Cancel
End If
Else
UniqueName = True
End If
Loop.
' Save As PDF Document
On Error GoTo ProblemSaving
ActiveDocument.ExportAsFixedFormat _
OutputFileName:=CurrentFolder & FileName & ".pdf", _
ExportFormat:=wdExportFormatPDF
On Error GoTo 0
' Confirm Save To User
With ActiveDocument
FolderName = Mid(.Path, InStrRev(.Path, "") + 1, Len(.Path) - InStrRev(.Path, ""))
End With
MsgBox "PDF has now been saved in the Folder: " & FolderName
Exit Sub
' Error Handlers
ProblemSaving:
MsgBox "There was a problem saving your PDF. This is most commonly caused" & by the original PDF file already being open."
Exit Sub
End Sub
Function ValidFileName(FileName As String) As Boolean
' PURPOSE: Determine If A Given Word Document File Name Is Valid
' SOURCE: www.TheSpreadsheetGuru.com/the-code-vault (http://www.TheSpreadsheetGuru.com/the-code-vault)
Dim TempPath As String
Dim doc As Document
' Determine Folder Where Temporary Files Are Stored
TempPath = Environ("TEMP")
' Create a Temporary XLS file (XLS in case there are macros)
On Error GoTo InvalidFileName
Set doc = ActiveDocument.SaveAs2(ActiveDocument.TempPath & "" & FileName & ".doc", wdFormatDocument)
On Error Resume Next
' Delete Temp File
Kill doc.FullName
' File Name is Valid
ValidFileName = True
Exit Function
' ERROR HANDLERS
InvalidFileName:
' File Name is Invalid
ValidFileName = False
End Function
The error message is generated whenever I click on 'NO' it gives the following message:
invalid procedure call or argument
It's seems to be having an issue with the 'SaveAs2' part towards the bottom of the script below in the functions section. (The bold & underline is my emphasis only so you can spot it quicker).
Any assistance would be greatly appreciated. Thanks.
Here is the VBA routine:
=========================================================================== ================================
Sub Word_ExportPDF()
' PURPOSE: Generate A PDF Document From Current Word Document
' NOTES: PDF Will Be Saved To Same Folder As Word Document File
' SOURCE: www.TheSpreadsheetGuru.com/the-code-vault (http://www.TheSpreadsheetGuru.com/the-code-vault)
Dim CurrentFolder As String
Dim FileName As String
Dim myPath As String
Dim UniqueName As Boolean
UniqueName = False
' Store Information About Word File
myPath = ActiveDocument.FullName
CurrentFolder = ActiveDocument.Path & ""
FileName = Mid(myPath, InStrRev(myPath, "") + 1, _
InStrRev(myPath, ".") - InStrRev(myPath, "") - 1)
' Does File Already Exist?
Do While UniqueName = False
DirFile = CurrentFolder & FileName & ".pdf"
If Len(Dir(DirFile)) <> 0 Then
UserAnswer = MsgBox("Filename Already Exists! Click " & _
"[Yes] to override. Click [No] to Rename.", vbYesNoCancel).
If UserAnswer = vbYes Then
UniqueName = True
ElseIf UserAnswer = vbNo Then
Do
' Retrieve New File Name
FileName = InputBox("Provide New File Name " & _
"(will ask again if you provide an invalid file name)", _
"Enter File Name", FileName)
' Exit if User Wants To
If FileName = "False" Or FileName = "" Then Exit Sub
Loop While ValidFileName(FileName) = False
Else
Exit Sub 'Cancel
End If
Else
UniqueName = True
End If
Loop.
' Save As PDF Document
On Error GoTo ProblemSaving
ActiveDocument.ExportAsFixedFormat _
OutputFileName:=CurrentFolder & FileName & ".pdf", _
ExportFormat:=wdExportFormatPDF
On Error GoTo 0
' Confirm Save To User
With ActiveDocument
FolderName = Mid(.Path, InStrRev(.Path, "") + 1, Len(.Path) - InStrRev(.Path, ""))
End With
MsgBox "PDF has now been saved in the Folder: " & FolderName
Exit Sub
' Error Handlers
ProblemSaving:
MsgBox "There was a problem saving your PDF. This is most commonly caused" & by the original PDF file already being open."
Exit Sub
End Sub
Function ValidFileName(FileName As String) As Boolean
' PURPOSE: Determine If A Given Word Document File Name Is Valid
' SOURCE: www.TheSpreadsheetGuru.com/the-code-vault (http://www.TheSpreadsheetGuru.com/the-code-vault)
Dim TempPath As String
Dim doc As Document
' Determine Folder Where Temporary Files Are Stored
TempPath = Environ("TEMP")
' Create a Temporary XLS file (XLS in case there are macros)
On Error GoTo InvalidFileName
Set doc = ActiveDocument.SaveAs2(ActiveDocument.TempPath & "" & FileName & ".doc", wdFormatDocument)
On Error Resume Next
' Delete Temp File
Kill doc.FullName
' File Name is Valid
ValidFileName = True
Exit Function
' ERROR HANDLERS
InvalidFileName:
' File Name is Invalid
ValidFileName = False
End Function