Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-30-2018, 04:39 AM
JordyI89 JordyI89 is offline VBA Macro to convert .docm to multiple pdf files Windows 10 VBA Macro to convert .docm to multiple pdf files Office 2016
Novice
VBA Macro to convert .docm to multiple pdf files
 
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
  #2  
Old 12-02-2018, 12:45 AM
macropod's Avatar
macropod macropod is offline VBA Macro to convert .docm to multiple pdf files Windows 7 64bit VBA Macro to convert .docm to multiple pdf files Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 12-03-2018, 02:15 AM
JordyI89 JordyI89 is offline VBA Macro to convert .docm to multiple pdf files Windows 10 VBA Macro to convert .docm to multiple pdf files Office 2016
Novice
VBA Macro to convert .docm to multiple pdf files
 
Join Date: Nov 2018
Posts: 3
JordyI89 is on a distinguished road
Default

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
Reply With Quote
Reply



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
VBA Macro to convert .docm to multiple pdf files 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
VBA Macro to convert .docm to multiple pdf files How to convert docm to dotm without opening the file Moz Word 1 12-20-2012 04:23 PM
VBA Macro to convert .docm to multiple pdf files convert multiple csv files to multiple excel files mit Excel 1 06-14-2011 10:15 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:52 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft