ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   User hits Cancel (https://www.excelbanter.com/excel-programming/430469-user-hits-cancel.html)

The Inspector[_2_]

User hits Cancel
 

Here is a function I am attempting to use. It seemed to work fine until I
attempted to account for the user hitting cancel when prompted to enter a
file name. I get a type mismatch error because the cancel button returns a
boolean instead of a string. How do I rectify this? Am I going about this all
wrong? Any help is appreciated.

Function SaveToPDF() As Boolean

Dim folderName As String
Dim fileName As String
Dim namingError
fileName = ""

folderName = BrowseFolder(Caption:="Select A Folder",
InitialFolder:="C:\Documents and Settings\Jamie\Desktop\")

If folderName = "" Then
Exit Function ' User hit Cancel
Else

While fileName = ""
fileName = Application.InputBox("Enter a name for the PDF file.")
If fileName = False Then
SaveToPDF = False
Exit Function 'user hit cancel
Else
If fileName = "" Then
namingError = MsgBox("You must enter a name for the file
or click Cancel to exit. Click OK to" _
+ " return to the name entry screen", vbOKOnly, "File
Naming Error")
Else
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF,
fileName:= _
folderName + "\" + fileName + ".pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True,
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
SaveToPDF = True
End If
End If
Wend
End If
Application.WindowState = xlNormal

End Function

Dave Peterson

User hits Cancel
 

Declare
Dim fileName As Variant 'since it can be a string or a boolean.

The Inspector wrote:

Here is a function I am attempting to use. It seemed to work fine until I
attempted to account for the user hitting cancel when prompted to enter a
file name. I get a type mismatch error because the cancel button returns a
boolean instead of a string. How do I rectify this? Am I going about this all
wrong? Any help is appreciated.

Function SaveToPDF() As Boolean

Dim folderName As String
Dim fileName As String
Dim namingError
fileName = ""

folderName = BrowseFolder(Caption:="Select A Folder",
InitialFolder:="C:\Documents and Settings\Jamie\Desktop\")

If folderName = "" Then
Exit Function ' User hit Cancel
Else

While fileName = ""
fileName = Application.InputBox("Enter a name for the PDF file.")
If fileName = False Then
SaveToPDF = False
Exit Function 'user hit cancel
Else
If fileName = "" Then
namingError = MsgBox("You must enter a name for the file
or click Cancel to exit. Click OK to" _
+ " return to the name entry screen", vbOKOnly, "File
Naming Error")
Else
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF,
fileName:= _
folderName + "\" + fileName + ".pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True,
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
SaveToPDF = True
End If
End If
Wend
End If
Application.WindowState = xlNormal

End Function


--

Dave Peterson

Patrick Molloy

User hits Cancel
 

If folderName = "False" Then

"The Inspector" wrote in message
...
Here is a function I am attempting to use. It seemed to work fine until I
attempted to account for the user hitting cancel when prompted to enter a
file name. I get a type mismatch error because the cancel button returns a
boolean instead of a string. How do I rectify this? Am I going about this
all
wrong? Any help is appreciated.

Function SaveToPDF() As Boolean

Dim folderName As String
Dim fileName As String
Dim namingError
fileName = ""

folderName = BrowseFolder(Caption:="Select A Folder",
InitialFolder:="C:\Documents and Settings\Jamie\Desktop\")

If folderName = "" Then
Exit Function ' User hit Cancel
Else

While fileName = ""
fileName = Application.InputBox("Enter a name for the PDF
file.")
If fileName = False Then
SaveToPDF = False
Exit Function 'user hit cancel
Else
If fileName = "" Then
namingError = MsgBox("You must enter a name for the
file
or click Cancel to exit. Click OK to" _
+ " return to the name entry screen", vbOKOnly, "File
Naming Error")
Else
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF,
fileName:= _
folderName + "\" + fileName + ".pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True,
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
SaveToPDF = True
End If
End If
Wend
End If
Application.WindowState = xlNormal

End Function



The Inspector[_2_]

User hits Cancel
 

Thanks. That did it.

"Dave Peterson" wrote:

Declare
Dim fileName As Variant 'since it can be a string or a boolean.

The Inspector wrote:

Here is a function I am attempting to use. It seemed to work fine until I
attempted to account for the user hitting cancel when prompted to enter a
file name. I get a type mismatch error because the cancel button returns a
boolean instead of a string. How do I rectify this? Am I going about this all
wrong? Any help is appreciated.

Function SaveToPDF() As Boolean

Dim folderName As String
Dim fileName As String
Dim namingError
fileName = ""

folderName = BrowseFolder(Caption:="Select A Folder",
InitialFolder:="C:\Documents and Settings\Jamie\Desktop\")

If folderName = "" Then
Exit Function ' User hit Cancel
Else

While fileName = ""
fileName = Application.InputBox("Enter a name for the PDF file.")
If fileName = False Then
SaveToPDF = False
Exit Function 'user hit cancel
Else
If fileName = "" Then
namingError = MsgBox("You must enter a name for the file
or click Cancel to exit. Click OK to" _
+ " return to the name entry screen", vbOKOnly, "File
Naming Error")
Else
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF,
fileName:= _
folderName + "\" + fileName + ".pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True,
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
SaveToPDF = True
End If
End If
Wend
End If
Application.WindowState = xlNormal

End Function


--

Dave Peterson



All times are GMT +1. The time now is 07:03 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com