Dati kod radi za verzije MS Word 2010 i novije kao sto je navedeno u pocetnom opisu.
Postoji mogucnost da se koristi u MS Word 2007 ali mora da se instalira dodatak (ADD-IN) od Microsoft-a koji je besplatan za MS Office 2007.
Ja sam brzinski odradio podrsku i za MS Word 2007 u dole navedenom kodu i napravio obe varijante, za MS Word 2007 i za novije verzije od MS Word 2007.
Pomenuti dodatak za MS Office 2007 (znaci i MS Word 2007) mozete preuzeti sa nevedenog linka.
U pitanju je instalacioni file koji je manji od 1MB. Isti, sam postavio i na G-Drive za download.
Microsoft Save as PDF or XPS Add-in for Microsoft Office 2007
Microsoft Save as PDF or XPS Add-in for Microsoft Office 2007 on personal G-Drive
U prilogu se nalaze dva screenshot-a i to prvi prikazuje okvirno kako bi trebalo da izgleda SAVE AS dialog nakon instalcije dodatka, dok drugi predstavlja preporuceni deo gde bi trebalo ubaciti kod, pre svega PublishPDF().
Pored istih je i .bas file sa kodom koji je dole naveden.
Kod koji se moze postaviti po zelji u modul ili direktno u Normal template.
Code:
' Publish current document to PDF - Input arguments: filename as full file location, PageFrom/PageTo page range; If PageTo isn't set or is less 1 then will try to get total pages of current document
' Tested with MS Word versions: 2010, 2013, 2016
' Added suport for version 2007 using add-in from Microsoft -> Microsoft Save as PDF or XPS Add-in for Microsoft Office 2007
' Download url: http://r.office.microsoft.com/r/rlidMSAddinPDFXPS
Public Sub PublishPDF(ByVal Filename As String, Optional ByVal PageFrom As Long = 1, Optional ByVal PageTo As Long = 0)
Dim lRange As Word.WdExportRange
On Error GoTo ErrHandler
' If PageTo isn't set or left 0 then
If PageFrom = 1 And PageTo < 1 Then
' Set range to export all pages
lRange = wdExportAllDocument
ElseIf PageFrom <= PageTo Then
If PageFrom = 0 Then
' Set range to export current page
lRange = wdExportCurrentPage
Else
' Repaginate current document
ActiveDocument.Repaginate
' Set range to export from page to page
lRange = wdExportFromTo
End If
End If
Dim lVer As Long
' Get version of MS Word
lVer = Val(Application.Version)
' If version is 2010 and newer then
If lVer > 12 Then
' Try to export document to PDF
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
Filename, _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=True, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=lRange, From:=PageFrom, To:=PageTo, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
' If version is 2007 then
ElseIf lVer = 12 Then
' Add-in must be installed first
' Try to export document to PDF
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
Filename, ExportFormat:=wdExportFormatPDF, _
OpenAfterExport:=False, OptimizeFor:=wdExportOptimizeForPrint, Range:= _
lRange, From:=PageFrom, To:=PageTo, Item:=wdExportDocumentContent, _
IncludeDocProps:=True, KeepIRM:=True, CreateBookmarks:= _
wdExportCreateNoBookmarks, DocStructureTags:=True, BitmapMissingFonts:= _
True, UseISO19005_1:=False
' Everything else
Else
' Show message
MsgBox "No support for this version of MS Word."
End If
Exit Sub
ErrHandler:
' Show message
MsgBox "Error in exporting selected document to PDF file." & vbCrLf & vbCrLf & "Error: " & Err.Number & " - " & Err.Description, vbExclamation, "Publish PDF"
End Sub
' Test Example - How can be used
Public Sub TestPublish()
' Export to PDF all pages of current document
PublishPDF "C:\Users\ieuser\Desktop\test_allpages.pdf"
' Export to PDF from page to page current document
PublishPDF "C:\Users\ieuser\Desktop\test_frompagetopage.pdf", 5, 10
' Export to PDF current page of current document
PublishPDF "C:\Users\ieuser\Desktop\test_currentpage.pdf", 0
End Sub
p.s. Namerno je razdvojeno sto se tice verzija jer gledano iz koda nema razlike ali zbog dodatka je tako namerno odradjen cisto da bi se videlo da mora da ide dodatak. To moze da se izbaci i preradi...