PDA

View Full Version : Word Macro saving doc as PDF



Darth
05-15-2014, 10:26 PM
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.

Thanks for your help.

Sub Savepdf()

Dim feedback AsString
Dim firstname AsString
Dim lastname AsString
Dim Docname AsString

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

EndSub

macropod
05-16-2014, 04:23 PM
It is far better to use the mailmerge process to generate the individual files than to try to separate them afterwards. For example, the following macro generates one output file per record. Files are saved to the same folder as the mailmerge main document, using the ‘Last_Name’ & ‘First_Name’ fileds in the data source for the filenames. PDF & DOCX formats are catered for.

Sub Merge_To_Individual_Files()
'Merges one record at a time to the chosen output folder
Application.ScreenUpdating = False
Dim StrFolder As String, StrName As String, MainDoc As Document, i As Long, j as long
Set MainDoc = ActiveDocument
With MainDoc
StrFolder = .Path & "\"
For i = 1 To .MailMerge.DataSource.RecordCount
With .MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = i
.LastRecord = i
.ActiveRecord = i
If Trim(.DataFields("Last_Name")) = "" Then Exit For
StrName = .DataFields("Last_Name") & "_" & .DataFields("First_Name")
End With
.Execute Pause:=False
End With
For j = 1 To 255
Select Case j
Case 1 To 31, 33 To 45, 47, 58 To 64, 91 To 94, 96, 123 To 141, 143 To 149, 152 To 157, 160 To 180, 182 To 191
StrName = Replace(StrName, Chr(j), "")
End Select
Next
StrName = Trim(StrName)
With ActiveDocument
.SaveAs2 FileName:=StrPath & StrName & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
' and/or:
.SaveAs2 FileName:=StrPath & StrName & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
.Close SaveChanges:=False
End With
Next i
End With
Application.ScreenUpdating = True
End Sub
PS: When posting code, please use the code tags. They're on the # symbol on the Post/Reply menu.

Doug Robbins
05-18-2014, 10:47 PM
Use the Merge to Individual Documents utility on my MergeTools Add-in.

You can download the MergeTools – 20140218.dotm Add-in that I created from the following page of my One Drive:

http://bit.ly/1hduSCB

The file needs to be saved in the Word Startup folder. In Windows XP the default location for that folder is

C:\Documents and Settings\[User Name]\Application Data\Microsoft\Word\STARTUP

In Windows Vista and Windows 7, 8 or 8.1 it is

C:\Users\[User Name]\AppData\Roaming\Microsoft\Word\STARTUP

If you do not see the AppData folder: -

In Windows 7, - In Windows Explorer, click on the Organize drop down and then on Folder and search options and in the Folder Options dialog, go to the View tab and select the item "Show hidden files, folders, and drives". While there, it is a good idea to uncheck the box of "Hide extensions for known file types".

In Windows 8 and 8.1, in the File Explorer, click on Options on the View tab of the ribbon and then on the View tab in the dialog that appears and select the item "Show hidden files, folders, and drives". While there, it is a good idea to uncheck the box of "Hide extensions for known file types".

When that has been done and Word is started\re-started, the tab shown below will be added to the Ribbon:

11702

One thing to note is that the field names in the data source must contain only alphanumeric characters (No @,#,$,%,&,(,), etc) and the field names must not start with a numeric character (0-9).

You may also want to download:


the Merging with Attachments document that is also on that page which explains how the system is used. It is not actually necessary to have separate attachments as the facility can be used to send just the documents created by the merge itself as attachments, either as the body of the message itself or in the form of Word files or .pdf files.
the Mail Merging with Charts document that is also on that page. That document explains how you must set up the Excel Data Source and the Mail Merge Main document to be able to execute a merge with a Chart that is unique to each record in the data source.
the Using the Many to One Facility document that describes how to use that facility.