Combine Word Documents through Script

saberahul

macrumors 68040
Original poster
Nov 6, 2008
3,599
85
USA
Hello,

Maybe someone can help me accomplish this. Here's what I am trying to do:
  1. For parent folder "Untitled Folder" on Desktop, there are many sub-folders inside. Each sub-folder has 1-2 .docx files
  2. Ultimate goal is to combine all the documents into a single PDF (ordering does not matter)
  3. For each document, insert a page break before inserting the next document otherwise it is not readable easily
  4. For each document, at the top of its insert, insert the filename of the document so I know what the next few pages are on until the next filename shows up

Here's what I have so far:
Automator:
Get Specified Finder Items
Get Folder Contents (Repeat for each subfolder found is checked)
Combine Word Documents
Save Word Documents as PDF

This works; however, I am unable to get filename of each document before its insert and unable to get a page break.

Apple Script:
I found this code online and tried manipulating it, but had absolutely no luck. Only change I made was add .docx to it. Same problems as above when this runs.

Can someone assist or point me in the right direction?

Here's the code:
Code:
on open theFiles
   
    tell application "Microsoft Word" to set comboDoc to make new document
   
    repeat with i from 1 to (count theFiles)
        set theFile to item i of theFiles
        set filePath to theFile as Unicode text
        tell application "Finder"
            set {creaType, fileType} to {creator type, file type} of theFile
            set ext to name extension of theFile
            set fileName to name of theFile
        end tell
        if (creaType is "MSWD" and {fileType} is in {"BINA", "W8BN", "s8BN", "W6BN", "s6BN", "RTF "}) or ext = "docx" then
            tell application "Microsoft Word"
                set comboRange to text object of comboDoc
                set collapsedRange to collapse range comboRange direction collapse end
                insert file file name filePath at collapsedRange
            end tell
        end if
    end repeat
    tell application "Microsoft Word" to activate
end open
 

kryten2

macrumors 6502a
Mar 17, 2012
995
59
Belgium
There is an insert break command in the Microsoft Word Dictionary.

insert break v : Inserts a break in the specified place of the specified kind.

insert break

at text range : The place the new break should be inserted. It must specify a range within a document i.e. the fifth word of paragraph 1 of document 1.

[break type section break next page/‌section break continuous/‌section break even page/‌section break odd page/‌line break/‌page break/‌column break] : The type of break to be inserted.