Hi,
This query is related to word vba and I am fairly new to writing vba macro.
I have a mail merged word document. The code is supposed to save each letter as a separate pdf file with the given name which it picks from the table in the document.
The issue is:
1) It asks for a folder to save the word document.
2) It then saves the word document also
3) It adds an extra page at the end of every document.
I am not sure that in order to save the pdf file, it necessarily have to save a word document. The word document which gets created does not even have the same name as pdf file.
I tried attaching the word document but the .doc or .docx extension is not supported here.
Thanks for your help.
This query is related to word vba and I am fairly new to writing vba macro.
I have a mail merged word document. The code is supposed to save each letter as a separate pdf file with the given name which it picks from the table in the document.
The issue is:
1) It asks for a folder to save the word document.
2) It then saves the word document also
3) It adds an extra page at the end of every document.
I am not sure that in order to save the pdf file, it necessarily have to save a word document. The word document which gets created does not even have the same name as pdf file.
I tried attaching the word document but the .doc or .docx extension is not supported here.
Thanks for your help.
Code:
Sub Savepdf()
Dim feedback As String
Dim firstname As String
Dim lastname As String
Dim Docname As String
Selection.EndKey Unit:=wdStory
Letters = Selection.Information(wdActiveEndSectionNumber)
Selection.HomeKey Unit:=wdStory
Counter = 1
While Counter < Letters
ActiveDocument.Sections.First.Range.Cut
Documents.Add
Selection.Paste
ActiveDocument.Tables(2).Cell(1, 2).Select
firstname = ActiveDocument.Tables(2).Cell(1, 2).Range.Text
firstname = Left(firstname, Len(firstname) - 2)
ActiveDocument.Tables(2).Cell(2, 2).Select
lastname = ActiveDocument.Tables(2).Cell(2, 2).Range.Text
lastname = Left(lastname, Len(lastname) - 2)
Docname = "Feedback" & "_" & firstname & "_" & lastname & ".pdf"
ActiveDocument.ExportAsFixedFormat OutputFileName:=Docname, _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
ActiveDocument.Close
Counter = Counter + 1
Wend
End Sub