All PDF files are created and saved with the correct name in the correct location, but when I try to open the PDF, I get a message that it does not work because the file is damaged, can you help me so I can open the file?
Thanks in advance.
my script:
Sub SerienbriefOneDoc()
'
' SerienbriefOneDoc Makro
'
'
Dim Dateiname As String
Dim LetzterRec As Long
Application.ScreenUpdating = False
Application.Visible = False
Const path As String = "G:\Laptop\Volume_D\Lehre\Basislehrjahr\Auftraege\Projektarbeit\WordMakro\Serienbrief\save\" 'Speicherpfad des Resultates
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdLastRecord
LetzterRec = Word.ActiveDocument.MailMerge.DataSource.ActiveRecord
ActiveDocument.MailMerge.DataSource.ActiveRecord = wdFirstRecord
With ActiveDocument.MailMerge
.DataSource.ActiveRecord = wdFirstRecord
Do
If .DataSource.ActiveRecord > 0 Then
If .DataSource.DataFields("Name").Value <> "0" Then
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = .ActiveRecord
.LastRecord = .ActiveRecord
DName = path & .DataFields("Name").Value & "_" & .DataFields("Vorname").Value & ".pdf"
End With
.Execute Pause:=False
Set dlgSaveAs = Dialogs(wdDialogFileSaveAs)
With dlgSaveAs
.Format = wdFormatPDF
ActiveDocument.SaveAs2 FileName:=DName
ActiveDocument.Close False
End With
End If
End If
If .DataSource.ActiveRecord < LetzterRec Then
.DataSource.ActiveRecord = wdNextRecord
Else
Exit Do
End If
Loop
End With
Application.Visible = True
Application.ScreenUpdating = True
End Sub
According to the documentation of the Document.SaveAs2 method you need to specify the second paramater FileFormat
as wdFormatPDF
otherwise it will save as word document.
Note that
With dlgSaveAs
.Format = wdFormatPDF
will only set the fileformat for the dialog box that let you choose the path and filename. But the SaveAs2
does not care about that if you don't tell it the correct format too.
Anyway it looks like you don't use the save-as dialog box at all. I think you can safely remove this part
Set dlgSaveAs = Dialogs(wdDialogFileSaveAs)
With dlgSaveAs
.Format = wdFormatPDF
End With
and only keep that part there
ActiveDocument.SaveAs2 FileName:=DName, FileFormat:=wdFormatPDF
ActiveDocument.Close False
… is that you saved a file your-file.pdf
with an .pdf
extension but a Word format document inside. If you rename it to your-file.docx
it would probably open in Word.