我在Word中需要一个宏,用于将序列字母的每一页分别保存为PDF

问题描述 投票:0回答:1

所有PDF文件均已创建,并以正确的名称保存在正确的位置,但是当我尝试打开PDF时,我收到一条消息,指出该文件已损坏,因此无法正常工作,请您帮帮我,我可以打开文件?

提前感谢。

我的脚本:

    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
vba ms-word word-vba
1个回答
0
投票

根据Document.SaveAs2 method的文档,您需要将第二个参数FileFormat指定为wdFormatPDF,否则它将另存为Word文档。

请注意

With dlgSaveAs
    .Format = wdFormatPDF

只会设置对话框的文件格式,让您选择路径和文件名。但是,如果您也没有告诉它正确的格式,则SaveAs2不会在乎。

无论如何,您似乎根本不使用另存为对话框。我认为您可以安全地删除此部分

Set dlgSaveAs = Dialogs(wdDialogFileSaveAs)
With dlgSaveAs
    .Format = wdFormatPDF

End With

并且只保留那部分

ActiveDocument.SaveAs2 FileName:=DName, FileFormat:=wdFormatPDF
ActiveDocument.Close False

实际发生了什么……

…是您保存了扩展名为your-file.pdf的文件.pdf,但其中保存了Word格式的文档。如果将其重命名为your-file.docx,则可能会在Word中打开。

© www.soinside.com 2019 - 2024. All rights reserved.