Save file as PDF files

Hello,

The below codes splits the files and creates a word document and its perfectly working to me desire, I want this files to be saved as pdf files instead of .doc file and want to add addin which allows the user to click a button, to chose the file, split it and save as a pdf file.

Here's my code:

Code:

Sub BreakOnPage()
' Used to set criteria for moving through the document by page.
Application.Browser.Target = wdBrowsePage
For i = 1 To ActiveDocument.BuiltInDocumentProperties("Number of Pages")
'Select and copy the text to the clipboard.
ActiveDocument.Bookmarks("\page").Range.Copy
' Open new document to paste the content of the clipboard into.
Documents.Add
Selection.Paste
' Removes the break that is copied at the end of the page, if any.
Selection.TypeBackspace
ChangeFileOpenDirectory "C:\Monthly"
DocNum = DocNum + 1
ActiveDocument.SaveAs FileName:="Receipt No -" & DocNum & ".doc"
ActiveDocument.Close
' Move the selection to the next page in the document.
Application.Browser.Next
Next i
ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Sub

It looks like your requirements are to paste each page's content into a new document and then save each page worth of content as a PDF. Depending on what you want, your posted method causes issues such as:
1. Pasting a page's content to a new document will change the headers/footers to that of the new document rather than the source document
2. Pasting the content may result in a big difference in formatting due to different styles in target document.
3. Fields may break if they are updated (eg TOC, cross-references)

I would be more inclined to save each page as a PDF from within the original doc. Actually, the fastest way to do this would be to save the entire file to PDF and then use Adobe Acrobat itself to split the document into single pages.

Questions I would ask are:
Do you have Adobe Acrobat or just the Reader?
Does the process have to be automated or can you do it manually with a couple of steps?
Do you need hyperlinks on pages to be active in the PDF?
Do you need to control the starting point for the DocNum variable?

Assuming Word 2007 & later, even faster would be to use the ExportAsFixedFormat method iteratively, specifying the page(s) to be saved as a PDF for each iteration:

Code:

Sub Demo()
Dim i As Long, StrNm As String
StrNm = "C:\Monthly\Receipt No -"
With ActiveDocument
For i = 1 To .ComputeStatistics(wdStatisticPages)
.ExportAsFixedFormat OutputFileName:=StrNm & i, ExportFormat:=wdExportFormatPDF, _
From:=i, To:=i, OptimizeFor:=wdExportOptimizeForPrint
Next
End With
End Sub

Hello,
Nicely written codes it works like a charm.
I want to name the splitted pfd files from the word document which is located on the 6th line for the next 8 characters instead of ascending numbers.

That would require significantly more code and, to get the correct data, I'd need to see a document that contains these 'lines' because lines within a paragraph need to be treated differently to lines with paragraph breaks. Can you attach a document to a post with some representative data (delete anything sensitive)? You do this via the paperclip symbol on the 'Go Advanced' tab.

Granted, the 'Employee Code or ID' would be more sensible, but I'm not sure where that is. I can see what looks like it might be one as the last number string in the second paragraph (i.e. end of the 3rd line in the document), but that has only 6 characters, not 8.

I think it depends on what is a unique identifier for each file you save. If you pick something that is then repeated on a later page then the first file could be over-written unless you handle that possibility.

Your latest attachment has no manual page breaks in it. Instead, you've padded out the first page with empty paragraphs to force the next page's content down. Bad practice. However, since you're apparently using the mailmerge that inserts manual page breaks, the following should work: