Dear All,
Not sure if this is the right place to ask. I am using vba code to generate a pdf file from word. When it is in word, it only consists one page but when i converted the file into pdf, it is splitting into two pages. Is there vba code that i can add in my current vba code to eliminate the extra blank page.
Here is my original vba code.
Option Explicit
Const FOLDER_SAVED As String = "S:\SA Report\"
Const SOURCE_FILE_PATH As String = "S:\SA\SA Data Master Sheet Version 2021.xlsm"
Sub TestRun()
Dim MainDoc As Document, TargetDoc As Document
Dim dbPath As String
Dim recordNumber As Long, totalRecord As Long
Set MainDoc = ActiveDocument
With MainDoc.MailMerge
'// if you want to specify your data, insert a WHERE clause in the SQL statement
.OpenDataSource Name:=SOURCE_FILE_PATH, sqlstatement:="SELECT * FROM [Sheet1$]"
totalRecord = .DataSource.RecordCount
For recordNumber = 1 To totalRecord
With .DataSource
.ActiveRecord = recordNumber
.FirstRecord = recordNumber
.LastRecord = recordNumber
End With
.Destination = wdSendToNewDocument
.Execute False
Set TargetDoc = ActiveDocument
Selection.MoveLeft
Selection.Delete
TargetDoc.SaveAs2 FOLDER_SAVED & .DataSource.DataFields("File_Name").Value & ".docx", wdFormatDocumentDefault
TargetDoc.ExportAsFixedFormat OutputFileName:=FOLDER_SAVED & .DataSource.DataFields("File_Name").Value & ".pdf", exportformat:=wdExportFormatPDF
TargetDoc.Close False
Set TargetDoc = Nothing
Next recordNumber
End With
Set MainDoc = Nothing
End Sub
Not sure if this is the right place to ask. I am using vba code to generate a pdf file from word. When it is in word, it only consists one page but when i converted the file into pdf, it is splitting into two pages. Is there vba code that i can add in my current vba code to eliminate the extra blank page.
Here is my original vba code.
Option Explicit
Const FOLDER_SAVED As String = "S:\SA Report\"
Const SOURCE_FILE_PATH As String = "S:\SA\SA Data Master Sheet Version 2021.xlsm"
Sub TestRun()
Dim MainDoc As Document, TargetDoc As Document
Dim dbPath As String
Dim recordNumber As Long, totalRecord As Long
Set MainDoc = ActiveDocument
With MainDoc.MailMerge
'// if you want to specify your data, insert a WHERE clause in the SQL statement
.OpenDataSource Name:=SOURCE_FILE_PATH, sqlstatement:="SELECT * FROM [Sheet1$]"
totalRecord = .DataSource.RecordCount
For recordNumber = 1 To totalRecord
With .DataSource
.ActiveRecord = recordNumber
.FirstRecord = recordNumber
.LastRecord = recordNumber
End With
.Destination = wdSendToNewDocument
.Execute False
Set TargetDoc = ActiveDocument
Selection.MoveLeft
Selection.Delete
TargetDoc.SaveAs2 FOLDER_SAVED & .DataSource.DataFields("File_Name").Value & ".docx", wdFormatDocumentDefault
TargetDoc.ExportAsFixedFormat OutputFileName:=FOLDER_SAVED & .DataSource.DataFields("File_Name").Value & ".pdf", exportformat:=wdExportFormatPDF
TargetDoc.Close False
Set TargetDoc = Nothing
Next recordNumber
End With
Set MainDoc = Nothing
End Sub