View Single Post
 
Old 11-30-2018, 04:39 AM
JordyI89 JordyI89 is offline Windows 10 Office 2016
Novice
 
Join Date: Nov 2018
Posts: 3
JordyI89 is on a distinguished road
Default VBA Macro to convert .docm to multiple pdf files

Hi all,

My name is Jordy and I'm a new member on this forum.

Since a a couple of days I'm working with VBA.

I've created a word file with a command button on the first page of the word document (.docm). Once I click on the command button the data on page 2 to 19 are being extracted from an excel file by using ActiveX-elements (type: Label) in word. The data import into Microsoft Word works fine for me.

After this data import in word I need to save the .docm file to separate pdf files for pages 2 to 19.

I've searched on the internet and discovered a lot of solutions, however they all do not match my specific requirements so I was hoping you guys could help me out.

Requirements:
- .docm should be saved in predetermined folder which is always the same. So no need for popupp boxes where I want to save it
- pages 2 until 19 should always be saved as separate documents (pdf). Page 1 with the commandbutton should not be saved as pdf. So please also no popupp boxes here where I can select that I would like to save pages 2 to 19. It's always the same so preferably add it into the VBA-code.
- name convention: should be saved as Jordy1, Jordy2, Jordy3, etc.

There is a code from the internet that could be useful, please see below:
Code:
Private Sub CommandButton1_Click()
Dim docMultiple As Document
Dim docSingle As Document
Dim rngPage As Range
Dim iCurrentPage As Integer
Dim iPageCount As Integer
Dim strNewFileName As String
Application.ScreenUpdating = False
Set docMultiple = ActiveDocument
Set rngPage = docMultiple.Range
iCurrentPage = 2
iPageCount = docMultiple.Content.ComputeStatistics(wdStatisticPages)
Do Until iCurrentPage > iPageCount
  If iCurrentPage = iPageCount Then
    rngPage.End = ActiveDocument.Range.End
  Else
    Selection.GoTo wdGoToPage, wdGoToAbsolute, iCurrentPage + 1
    rngPage.End = Selection.Start
  End If
  rngPage.Copy
  Set docSingle = Documents.Add
  docSingle.Range.Paste
  docSingle.Range.Find.Execute Findtext:="^m", ReplaceWith:=""
  strNewFileName = Replace(docMultiple.FullName, ".docm", "_" & Right$("000" & iCurrentPage, 4) & ".pdf")
  docSingle.SaveAs strNewFileName
  iCurrentPage = iCurrentPage + 1
  docSingle.Close
  rngPage.Collapse wdCollapseEnd
Loop
Application.ScreenUpdating = True
Set docMultiple = Nothing
Set docSingle = Nothing
Set rngPage = Nothing
End Sub
With the code above separate pdf-files are created with correct name convention in the correct folder, however I can't open the pdf-files. That's probably because I'm using the ActiveX Label and it might not recognize it. Next to that it's also good to mention that I'm using a watermark in the background that should be "printed"/saved to pdf as well.

Hope someone could help me out with either a completely new code or modification of the code above.

If my question and/or requirements is/are not clear, please let me know.

Thanks in advance.

Best regards,
Jordy

Last edited by macropod; 12-02-2018 at 12:36 AM. Reason: Added code tags & formatting
Reply With Quote