#1
|
|||
|
|||
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 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 |
#2
|
||||
|
||||
Try:
Code:
Private Sub CommandButton1_Click() Dim i As Long With ActiveDocument For i = 2 To 19 .ExportAsFixedFormat OutputFileName:=Split(.FullName, ".doc")(0) & "_" & Format(i - 1, "000") & ".pdf", _ ExportFormat:=wdExportFormatPDF, Range:=wdExportFromTo, From:=i, To:=i If i = .ComputeStatistics(wdStatisticPages) Then Exit For Next End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Thank you so much Macropod / Paul!!
This perfectly works BR, Jordy @Moderator: please close this topic since Macropod has provided me with the solution. If I can close it myself, please let me know. Last edited by JordyI89; 12-03-2018 at 02:21 AM. Reason: Topic can be closed |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
convert multiple pptx files into mp4 videos using vba | shiva_b | PowerPoint | 1 | 05-02-2017 05:05 PM |
Rename docm to value from checkbox, convert to .pdf, email, delete | Lortiz70 | Word VBA | 1 | 01-19-2017 02:48 AM |
Macro to change/convert/delete txt files in folder+subfolders | NoS | Word VBA | 4 | 03-03-2016 12:10 PM |
How to convert docm to dotm without opening the file | Moz | Word | 1 | 12-20-2012 04:23 PM |
convert multiple csv files to multiple excel files | mit | Excel | 1 | 06-14-2011 10:15 AM |