Results 1 to 15 of 15
  1. #1
    New Lounger
    Join Date
    Nov 2013
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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
    Please Help. Thanks in advance.

  2. #2
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,520
    Thanks
    3
    Thanked 143 Times in 136 Posts
    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?
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  3. #3
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,913
    Thanks
    0
    Thanked 192 Times in 176 Posts
    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
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  4. #4
    New Lounger
    Join Date
    Nov 2013
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  5. #5
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,913
    Thanks
    0
    Thanked 192 Times in 176 Posts
    Quote Originally Posted by carmine View Post
    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.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  6. #6
    New Lounger
    Join Date
    Nov 2013
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi,

    I have uploaded the sample file.
    Attached Files Attached Files

  7. #7
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,913
    Thanks
    0
    Thanked 192 Times in 176 Posts
    So, from your sample file, you want 'William Fernandes'? That's what is on the 6th line, but "the next 8 characters" seems meaningless in this context.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  8. #8
    New Lounger
    Join Date
    Nov 2013
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by macropod View Post
    So, from your sample file, you want 'William Fernandes'? That's what is on the 6th line, but "the next 8 characters" seems meaningless in this context.
    Hello Paul,

    Well, I agree with it and if i flag Employee Code or ID on the very first line than that would be more sensible. What's your advise.
    Last edited by carmine; 2013-12-28 at 14:46.

  9. #9
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,913
    Thanks
    0
    Thanked 192 Times in 176 Posts
    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.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  10. #10
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,520
    Thanks
    3
    Thanked 143 Times in 136 Posts
    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.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  11. #11
    New Lounger
    Join Date
    Nov 2013
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hello,


    Paul Edstein

    Granted, the 'Employee Code or ID' would be more sensible, but I'm not sure where that is.
    Now onwards the document with a 'Employee Code' on the very first line of the document.

  12. #12
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,913
    Thanks
    0
    Thanked 192 Times in 176 Posts
    Please post a sample document, so we can see exactly what you're working with.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  13. #13
    New Lounger
    Join Date
    Nov 2013
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by macropod View Post
    Please post a sample document, so we can see exactly what you're working with.
    Hello,

    I have attached the sample file wherein i have mentioned the Employeed ID in the very first line,
    Once the letter is printed i will mark Employee ID on very first line.

    Thanks in advance.
    Attached Files Attached Files

  14. #14
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,913
    Thanks
    0
    Thanked 192 Times in 176 Posts
    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:
    Code:
    Sub Demo()
    Application.ScreenUpdating = False
    Dim StrPages As String, StrPath As String, StrFile As String, StrNms As String
    Dim i As Long, iLo As Long, iHi As Long
    With ActiveDocument
      'Get the output path
      StrPath = .Path & "\"
      'Get the name for the first output document
      StrNms = .Paragraphs.First.Range.Text
      'Initialize the counter
      i = 1
      'Find all the manual page breaks
      With .Range
        With .Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Text = "^m"
          .Replacement.Text = ""
          .Forward = True
          .Wrap = wdFindStop
          .Execute
        End With
        Do While .Find.Found
          'Add the page #s to the pages list
          StrPages = StrPages & i & vbTab & .Duplicate.Information(wdActiveEndPageNumber) & vbCr
          i = .Duplicate.Information(wdActiveEndPageNumber) + 1
          'Add the names for the additional documents to the output names list
          StrNms = StrNms & .Duplicate.Paragraphs.Last.Next.Range.Text
          .Collapse wdCollapseEnd
          .Find.Execute
        Loop
      End With
      'Add the last page(s) to the pages list
      StrPages = StrPages & i & vbTab & .Characters.Last.Information(wdActiveEndPageNumber)
      For i = 0 To UBound(Split(StrPages, vbCr))
        StrFile = StrPath & Split(StrNms, vbCr)(i) & ".pdf"
        iLo = Split(Split(StrPages, vbCr)(i), vbTab)(0)
        iHi = Split(Split(StrPages, vbCr)(i), vbTab)(1)
        'save to PDF
        .ExportAsFixedFormat OutputFileName:=StrFile, ExportFormat:=wdExportFormatPDF, _
          OptimizeFor:=wdExportOptimizeForPrint, Item:=wdExportDocumentContent, From:=iLo, To:=iHi
      Next
    End With
    Application.ScreenUpdating = True
    End Sub
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  15. #15
    New Lounger
    Join Date
    Nov 2013
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi,

    Thank you, Paul Edstein for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •