Run time error '1004: Document not saved, because the data field starts with a number/date?

Dear Microsoft Community,


I have like many others before me, "made" (I found someone who already did) a macro that converts a excel page to a PDF, and saves it in a given location.

I get an error whenever I let it choose the red data field you see in the posted picture. However the green works perfectly.

"My" code looks like this:

Don't mind the blurred data, it is just company path, that is blurred, to not get that mixed.

But as my "test" concluded, it works with a text field, but not a mixed text field, likely due to the formatting that cannot be used as the / symbol is not allowed.

UPDATE: The reason it didnt work, was the format of the red field indeed, if you use custom format, and type date as 16.08.2022_DK_Test1234_JohnLena it works!

Thank you for driving me to post here, it unlocked my brain Microsoft!! 🙌

Without removing "_" and spaces between the lines, then it should (if the code is OK) work.

...or you can use this code :-)

Option Explicit

Sub RDB_Worksheet_Or_Worksheets_To_PDF()

Dim FileName As String

If ActiveWindow.SelectedSheets.Count > 1 Then

MsgBox "There is more then one sheet selected," & vbNewLine & _

"be aware that every selected sheet will be published"

End If

'Call the function with the correct arguments

'Tip: You can also use Sheets("YourSheetName") instead of ActiveSheet in the code(sheet not have to be active then)

FileName = RDB_Create_PDF(Source:=ActiveSheet, _

FixedFilePathName:="", _

OverwriteIfFileExist:=True, _

OpenPDFAfterPublish:=True)

'For a fixed file name use this in the FixedFilePathName argument

'FixedFilePathName:="C:\Users\User\Test\YourPdfFile.pdf"

If FileName <> "" Then

'Ok, you find the PDF where you saved it

'You can call the mail macro here if you want

Else

MsgBox "ASK - Das Erstellen einer PDF Datei war nicht möglich:" & vbNewLine & _

"Microsoft Add-in is not installed" & vbNewLine & _

"You canceled the save dialog" & vbNewLine & _

"The path to save the file is incorrect" & vbNewLine & _

"You didn't want to overwrite the existing PDF if it exists"

End If

End Sub

Function RDB_Create_PDF(Source As Object, FixedFilePathName As String, _

OverwriteIfFileExist As Boolean, OpenPDFAfterPublish As Boolean) As String

Dim FileFormatstr As String

Dim Fname As Variant

If FixedFilePathName = "" Then

'Open the GetSaveAsFilename dialog to enter a file name for the pdf

FileFormatstr = "PDF Files (*.pdf), *.pdf"

Fname = Application.GetSaveAsFilename("", fileFilter:=FileFormatstr, _

Title:="Create PDF")

'If you cancel this dialog Exit the function

If Fname = False Then Exit Function

Else

Fname = FixedFilePathName

End If

'If OverwriteIfFileExist = False we test if the PDF

'already exist in the folder and Exit the function if that is True

If OverwriteIfFileExist = False Then

If Dir(Fname) <> "" Then Exit Function

End If

'Now the file name is correct we Publish to PDF

On Error Resume Next

Source.ExportAsFixedFormat _

Type:=xlTypePDF, _

FileName:=Fname, _

Quality:=xlQualityStandard, _

IncludeDocProperties:=True, _

IgnorePrintAreas:=False, _

OpenAfterPublish:=OpenPDFAfterPublish

On Error GoTo 0

'If Publish is Ok the function will return the file name

If Dir(Fname) <> "" Then RDB_Create_PDF = Fname

End Function

* Kindly Mark this reply if it helps please, as it will be beneficial to more Community members reading here.

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated February 10, 2024 Views 871 Applies to: