Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 178
Default Running Word to Save as PDF

I am obviously doing something wrong... I am trying desperately to avoid having to activate the Microsoft Word XXXX Object Library as this may possibly be used by others in the future and you know trying to describe how to activate references can be difficult...
The error occurs in the .ExportAsFixedFormat line, and I dunno what's wrong.. I recorded a save-do-pdf event from word itself and those are the lines I was given...
I'm running office 2003 on Win7 Pro also.



Sub DocToPDF()
Dim WordObject As Object
Dim WordDoc As Object
Dim ws As Worksheet
Dim path As String
Dim fname As String, filename2 As String, name As String, ext As String
Set WordObject = CreateObject("word.application")
WordObject.Visible = False

Set ws = ActiveSheet
path = ws.Range("a1").Value
If Right(path, 1) < "\" Then
path = path & "\"
End If
For i = 5 To ws.UsedRange.Rows.count
name = ws.Range("b" & i).Value
ext = Mid(name, InStr(name, "."), Len(name) - InStr(name, ".") + 1)
fname = path & name
filename2 = Replace(fname, ext, ".pdf")
Set WordDoc = WordObject.documents.Add(fname)
With WordDoc
.ExportAsFixedFormat OutputFileName:=filename2, ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False
.Close
End With
Next i
Set WordObject = Nothing
Set WordDoc = Nothing
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,182
Default Running Word to Save as PDF

I recorded a save-do-pdf event from word itself and those are the
lines I was given... I'm running office 2003 on Win7 Pro also.


Not in Word2003 since there's no such method. AFAIK
'ExportAsFixedFormat' wasn't available until v2007!

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 178
Default Running Word to Save as PDF

But I don't get it... the 'exportasfixedformat' was recorded BY word when I recorded the macro to see what it was doing to try and figure this out myself...

how else could it be done then? just with a saveas command?

On Thursday, January 26, 2017 at 2:26:49 PM UTC-7, GS wrote:
I recorded a save-do-pdf event from word itself and those are the
lines I was given... I'm running office 2003 on Win7 Pro also.


Not in Word2003 since there's no such method. AFAIK
'ExportAsFixedFormat' wasn't available until v2007!

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 178
Default Running Word to Save as PDF

I'm a big fat liar. It's 2007, not 2003.

On Thursday, January 26, 2017 at 2:26:49 PM UTC-7, GS wrote:
I recorded a save-do-pdf event from word itself and those are the
lines I was given... I'm running office 2003 on Win7 Pro also.


Not in Word2003 since there's no such method. AFAIK
'ExportAsFixedFormat' wasn't available until v2007!

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 178
Default Running Word to Save as PDF

I Lied - im in office 2007 :-\

On Thursday, January 26, 2017 at 2:26:49 PM UTC-7, GS wrote:
I recorded a save-do-pdf event from word itself and those are the
lines I was given... I'm running office 2003 on Win7 Pro also.


Not in Word2003 since there's no such method. AFAIK
'ExportAsFixedFormat' wasn't available until v2007!

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 178
Default Running Word to Save as PDF

I Lied, im in office 2007

On Thursday, January 26, 2017 at 1:00:45 PM UTC-7, Matthew Dyer wrote:
I am obviously doing something wrong... I am trying desperately to avoid having to activate the Microsoft Word XXXX Object Library as this may possibly be used by others in the future and you know trying to describe how to activate references can be difficult...
The error occurs in the .ExportAsFixedFormat line, and I dunno what's wrong. I recorded a save-do-pdf event from word itself and those are the lines I was given...
I'm running office 2003 on Win7 Pro also.



Sub DocToPDF()
Dim WordObject As Object
Dim WordDoc As Object
Dim ws As Worksheet
Dim path As String
Dim fname As String, filename2 As String, name As String, ext As String
Set WordObject = CreateObject("word.application")
WordObject.Visible = False

Set ws = ActiveSheet
path = ws.Range("a1").Value
If Right(path, 1) < "\" Then
path = path & "\"
End If
For i = 5 To ws.UsedRange.Rows.count
name = ws.Range("b" & i).Value
ext = Mid(name, InStr(name, "."), Len(name) - InStr(name, ".") + 1)
fname = path & name
filename2 = Replace(fname, ext, ".pdf")
Set WordDoc = WordObject.documents.Add(fname)
With WordDoc
.ExportAsFixedFormat OutputFileName:=filename2, ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False
.Close
End With
Next i
Set WordObject = Nothing
Set WordDoc = Nothing
End Sub


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,182
Default Running Word to Save as PDF

Is Word2007 the default on your system? This is important since any
repairs/updates to an earlier version will reset what CreateObject
uses! IOW, make sure your WordObject.Version is 12.

Otherwise:
Here's what I use via code in Excel to save sheets; -feel free to
modify it to suit or see how it works and adapt for Word automation.

Sub SaveAs_FixedFormat(FileType&, Filename$, _
Settings, Optional FromToRng, _
Optional IsGroup As Boolean = False, _
Optional StampIt As Boolean = True)
' Saves the following via ExportAsFixedFormat:
' Selected sheets, 1 file per sheet;
' Or a specified From/To group of sheets to 1 file,
' Or selected sheets (random grouping) to 1 file;
' Or an entire workbook to 1 file.
'
' ArgsIn:
' FileType& xlTypePDF (0) or xlTypeXSP (1)
' Filename$ Contains "<path\<wkbName" to which each wks.Name is
appended
' Settings Array containing the settings for the export params
' IsGroup !
'
Dim wks As Worksheet, sExt$, sFile$, sTS$
If Application.VERSION < 12 Then Exit Sub

sExt = IIf(FileType = 0, ".pdf", ".xps") '//always
sTS = "_" & Format(Now(), "_dd-mm-yyyy_hh-mm_AMPM") '//always

If Not IsGroup Then '//1 file per sheet
For Each wks In ActiveWindow.SelectedSheets
sFile = Filename & "_" & wks.name & IIf(StampIt, sTS & sExt, sExt)
wks.ExportAsFixedFormat Type:=FileType, Filename:=sFile, _
Quality:=Settings(0), IncludeDocProperties:=Settings(1), _
IgnorePrintAreas:=Settings(2), OpenAfterPublish:=Settings(3)
Next 'wks

Else '//multiple sheets per file
sFile = Filename & IIf(StampIt, sTS & sExt, sExt)
If Not IsMissing(FromToRng) Then '//it's a group
If Not LBound(FromToRng) = UBound(FromToRng) Then '//it's From/To
ActiveWorkbook.ExportAsFixedFormat Type:=FileType,
Filename:=sFile, _
Quality:=Settings(0), IncludeDocProperties:=Settings(1), _
IgnorePrintAreas:=Settings(2), OpenAfterPublish:=Settings(3), _
From:=FromToRng(0), To:=FromToRng(1)

Else '//it's selected sheets (random grouping)
'ExportAsFixedFormat only works with workbooks/worksheets,
'so copy selected sheets to a new (temp) workbook,
'export it, then discard it.
Application.ScreenUpdating = False '//hide activity
ActiveWindow.SelectedSheets.Copy
With ActiveWorkbook
.ExportAsFixedFormat Type:=FileType, Filename:=sFile, _
Quality:=Settings(0), IncludeDocProperties:=Settings(1), _
IgnorePrintAreas:=Settings(2),
OpenAfterPublish:=Settings(3)
.Close SaveChanges:=False
End With 'ActiveWorkbook
Application.ScreenUpdating = True
End If 'Not LBound(FromToRng) = UBound(FromToRng)

Else '//all sheets
ActiveWorkbook.ExportAsFixedFormat Type:=FileType,
Filename:=sFile, _
Quality:=Settings(0), IncludeDocProperties:=Settings(1), _
IgnorePrintAreas:=Settings(2), OpenAfterPublish:=Settings(3)
End If 'Not IsMissing(FromToRng)
End If 'Not IsGroup
End Sub 'SaveAs_FixedFormat

Sub Test_SaveAs_FixedFormat()
' Shows the various ways to use the SaveAs_FixedFormat routine.
' How the values passed to it are assembled is up to you!
' This example's focus is on how to prep the args only.

Dim sFile$, rng, vSettings
Const lTypePDF& = 0: Const lTypeXPS& = 1

'ExportAsFixedFormat accepts the following ArgsIn:
' Quality: Standard=0, Minimum=1 (file size)
' IncludeDocProperties: False=0, True=1
' IgnorePrintAreas: False=0, True=1
' OpenAfterPublish: False=0, True=1
'We pass our preferences for these to SaveAs_FixedFormat as a variant
array.
vSettings = Split("0,0,0,0", ",") '//edit to suit


'[Construct the Filename according to output path]
'NOTE: Do not include the filename extension
'when using the ExportAsFixedFormat feature.

'If output to ActiveWorkbook.Path, use
'.....................................
sFile = Split(ActiveWorkbook.FullName, ".")(0)
'Edit workbook ref to suit

'If output to a different path, use
'..................................
'Build sFile in logical steps
sFile = "C:\Users\Garry\Documents\VBA_Stuff\" '//path
'Append the filename as per your requirements
sFile = sFile & Split(ActiveWorkbook.name, ".")(0)
'Edit workbook ref to suit


'[Specifying a range of sheets, or a selected sheets grouping]
'................................................. ............
'To Export StartWith/EndWith range of sheets, use
rng = Split("1,2", ",") '//From=rng(0),To=rng(1)
'OR
'To Export a random grouping as selected while pressing 'Ctrl', use
rng = Split("1", ",") '//makes LBound=UBound


'[Exporting scenarios]
'To Export 1 file per selected sheet (random grouping)
SaveAs_FixedFormat FileType:=lTypeXPS, Filename:=sFile,
Settings:=vSettings
'OR
SaveAs_FixedFormat FileType:=lTypePDF, Filename:=sFile,
Settings:=vSettings


'To Export a From/To range of sheets to 1 file
rng = Split("1,2", ",")
SaveAs_FixedFormat FileType:=lTypeXPS, Filename:=sFile, _
Settings:=vSettings, IsGroup:=True, FromToRng:=rng
'OR
SaveAs_FixedFormat FileType:=lTypePDF, Filename:=sFile, _
Settings:=vSettings, IsGroup:=True, FromToRng:=rng


'To Export selected sheets to 1 file (random grouping)
rng = Split("1", ",")
SaveAs_FixedFormat FileType:=lTypeXPS, Filename:=sFile, _
Settings:=vSettings, IsGroup:=True, FromToRng:=rng
'OR
SaveAs_FixedFormat FileType:=lTypePDF, Filename:=sFile, _
Settings:=vSettings, IsGroup:=True, FromToRng:=rng


'To Export all sheets to 1 file
SaveAs_FixedFormat FileType:=lTypeXPS, Filename:=sFile, _
Settings:=vSettings, IsGroup:=True
'OR
SaveAs_FixedFormat FileType:=lTypePDF, Filename:=sFile, _
Settings:=vSettings, IsGroup:=True
End Sub 'Test_SaveAs_FixedFormat

Sub PrintTo_FixedFormat(FileType&, Filename$, _
Optional NumCopies& = 1, Optional FromToRng, _
Optional IsGroup As Boolean = False, _
Optional StampIt As Boolean = True)
' Prints the following choices via XPS Document Writer OR PDF printer,
' as specified by 'FileType':
' Selected sheets, 1 file per sheet;
' Or a specified From/To range of sheets to 1 file,
' Or selected sheets (random grouping) to 1 file;
' Or an entire workbook to 1 file.
'
' ArgsIn:
' FileType& lTypePDF=0; lTypeXPS=1
' Filename Contains "<path\<wkbName"
' NumCopies !
' IsGroup !
'
Dim wks As Worksheet, sExt$, sFile$, sStamp$
Dim sPrinter$, sDfltPrn$

Const sPrnXPS$ = "Microsoft XPS Document Writer on NE00:"
Const sPrnPDF$ = "deskPDF on DDM:"
'To quickly find the port your 'FileType' printer uses,
' - change the printer in the Print dialog and close without
printing;
' - in the VBE Immediate Window type the following, then press
'Enter';
' ?activeprinter
' - reset the printer in the Print dialog to your default!

'Initialize essential vars
sDfltPrn = Application.ActivePrinter '//reset when done
sPrinter = IIf(FileType = 0, sPrnPDF, sPrnXPS)
sExt = IIf(FileType = 0, ".pdf", ".xps")
sStamp = "_" & Format(Now(), "dd-mm-yyyy_hh-mm_AMPM")

If Not IsGroup Then '//1 file per sheet
For Each wks In ActiveWindow.SelectedSheets
sFile = Filename & "_" & wks.name & IIf(StampIt, sStamp & sExt,
sExt)
wks.PrintOut ActivePrinter:=sPrinter, Copies:=NumCopies, _
PrintToFile:=True, PrToFileName:=sFile
Next 'wks

Else
sFile = Filename & IIf(StampIt, sStamp & sExt, sExt)
If Not IsMissing(FromToRng) Then '//it's a range
If Not LBound(FromToRng) = UBound(FromToRng) Then
ActiveWorkbook.PrintOut From:=CLng(FromToRng(0)),
To:=CLng(FromToRng(1)), _
ActivePrinter:=sPrinter, Copies:=NumCopies,
PrintToFile:=True, PrToFileName:=sFile

Else '//it's a random grouping
ActiveWindow.SelectedSheets.PrintOut ActivePrinter:=sPrinter, _
Copies:=NumCopies, PrintToFile:=True, PrToFileName:=sFile
End If 'Not LBound(FromToRng) = UBound(FromToRng)

Else
ActiveWorkbook.PrintOut ActivePrinter:=sPrinter,
Copies:=NumCopies, _
PrintToFile:=True, PrToFileName:=sFile
End If 'Not IsMissing(FromToRng)
End If 'Not IsGroup

Application.ActivePrinter = sDfltPrn
End Sub 'PrintTo_FixedFormat

Sub Test_PrintTo_FixedFormat()
' Shows the various ways to use the PrintTo_FixedFormat routine.
' How the values passed to it are assembled is up to you!
' This example's focus is on how to prep the args only.

Dim sFile$, rng
Const lTypePDF& = 0: Const lTypeXPS& = 1

'[Construct the Filename according to output path]
'NOTE: Do not include the filename extension
'when using an XPS Document Writer.

'If output to ActiveWorkbook.Path, use
'.....................................
sFile = Split(ActiveWorkbook.FullName, ".")(0)
'Edit workbook ref to suit

'If output to a different path, use
'..................................
'Build sFile in logical steps
sFile = "C:\Users\Garry\Documents\VBA_Stuff\" '//path
'Append the filename as per your requirements
sFile = sFile & Split(ActiveWorkbook.name, ".")(0)
'Edit workbook ref to suit


'[Specifying a range of sheets, or a selected sheets grouping]
'................................................. ............
'To print StartWith/EndWith range of sheets, use
rng = Split("1,2", ",") '//From=rng(0),To=rng(1)
'OR
'To print a random grouping as selected while pressing 'Ctrl', use
rng = Split("1", ",") '//makes LBound=UBound


'[Printing scenarios]
'To print 1 file per selected sheet (random grouping)
PrintTo_FixedFormat FileType:=lTypeXPS, Filename:=sFile
'OR
PrintTo_FixedFormat FileType:=lTypePDF, Filename:=sFile


'To print a From/To range of sheets to 1 file
rng = Split("1,2", ",")
PrintTo_FixedFormat FileType:=lTypeXPS, Filename:=sFile,
IsGroup:=True, FromToRng:=rng
'OR
PrintTo_FixedFormat FileType:=lTypePDF, Filename:=sFile,
IsGroup:=True, FromToRng:=rng


'To print selected sheets to 1 file (random grouping)
rng = Split("1", ",")
PrintTo_FixedFormat FileType:=lTypeXPS, Filename:=sFile,
IsGroup:=True, FromToRng:=rng
'OR
PrintTo_FixedFormat FileType:=lTypePDF, Filename:=sFile,
IsGroup:=True, FromToRng:=rng


'To print all sheets to 1 file
PrintTo_FixedFormat FileType:=lTypeXPS, Filename:=sFile,
IsGroup:=True
'OR
PrintTo_FixedFormat FileType:=lTypePDF, Filename:=sFile,
IsGroup:=True
End Sub 'Test_PrintTo_FixedFormat

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 178
Default Running Word to Save as PDF

Yes, I just ran a simple msgbox command -
MsgBox Word.Application.Version

and it's 12.0 - so this should be much easier than it is...

On Thursday, January 26, 2017 at 3:03:44 PM UTC-7, GS wrote:
Is Word2007 the default on your system? This is important since any
repairs/updates to an earlier version will reset what CreateObject
uses! IOW, make sure your WordObject.Version is 12.

Otherwise:
Here's what I use via code in Excel to save sheets; -feel free to
modify it to suit or see how it works and adapt for Word automation.

Sub SaveAs_FixedFormat(FileType&, Filename$, _
Settings, Optional FromToRng, _
Optional IsGroup As Boolean = False, _
Optional StampIt As Boolean = True)
' Saves the following via ExportAsFixedFormat:
' Selected sheets, 1 file per sheet;
' Or a specified From/To group of sheets to 1 file,
' Or selected sheets (random grouping) to 1 file;
' Or an entire workbook to 1 file.
'
' ArgsIn:
' FileType& xlTypePDF (0) or xlTypeXSP (1)
' Filename$ Contains "<path\<wkbName" to which each wks.Name is
appended
' Settings Array containing the settings for the export params
' IsGroup !
'
Dim wks As Worksheet, sExt$, sFile$, sTS$
If Application.VERSION < 12 Then Exit Sub

sExt = IIf(FileType = 0, ".pdf", ".xps") '//always
sTS = "_" & Format(Now(), "_dd-mm-yyyy_hh-mm_AMPM") '//always

If Not IsGroup Then '//1 file per sheet
For Each wks In ActiveWindow.SelectedSheets
sFile = Filename & "_" & wks.name & IIf(StampIt, sTS & sExt, sExt)
wks.ExportAsFixedFormat Type:=FileType, Filename:=sFile, _
Quality:=Settings(0), IncludeDocProperties:=Settings(1), _
IgnorePrintAreas:=Settings(2), OpenAfterPublish:=Settings(3)
Next 'wks

Else '//multiple sheets per file
sFile = Filename & IIf(StampIt, sTS & sExt, sExt)
If Not IsMissing(FromToRng) Then '//it's a group
If Not LBound(FromToRng) = UBound(FromToRng) Then '//it's From/To
ActiveWorkbook.ExportAsFixedFormat Type:=FileType,
Filename:=sFile, _
Quality:=Settings(0), IncludeDocProperties:=Settings(1), _
IgnorePrintAreas:=Settings(2), OpenAfterPublish:=Settings(3), _
From:=FromToRng(0), To:=FromToRng(1)

Else '//it's selected sheets (random grouping)
'ExportAsFixedFormat only works with workbooks/worksheets,
'so copy selected sheets to a new (temp) workbook,
'export it, then discard it.
Application.ScreenUpdating = False '//hide activity
ActiveWindow.SelectedSheets.Copy
With ActiveWorkbook
.ExportAsFixedFormat Type:=FileType, Filename:=sFile, _
Quality:=Settings(0), IncludeDocProperties:=Settings(1), _
IgnorePrintAreas:=Settings(2),
OpenAfterPublish:=Settings(3)
.Close SaveChanges:=False
End With 'ActiveWorkbook
Application.ScreenUpdating = True
End If 'Not LBound(FromToRng) = UBound(FromToRng)

Else '//all sheets
ActiveWorkbook.ExportAsFixedFormat Type:=FileType,
Filename:=sFile, _
Quality:=Settings(0), IncludeDocProperties:=Settings(1), _
IgnorePrintAreas:=Settings(2), OpenAfterPublish:=Settings(3)
End If 'Not IsMissing(FromToRng)
End If 'Not IsGroup
End Sub 'SaveAs_FixedFormat

Sub Test_SaveAs_FixedFormat()
' Shows the various ways to use the SaveAs_FixedFormat routine.
' How the values passed to it are assembled is up to you!
' This example's focus is on how to prep the args only.

Dim sFile$, rng, vSettings
Const lTypePDF& = 0: Const lTypeXPS& = 1

'ExportAsFixedFormat accepts the following ArgsIn:
' Quality: Standard=0, Minimum=1 (file size)
' IncludeDocProperties: False=0, True=1
' IgnorePrintAreas: False=0, True=1
' OpenAfterPublish: False=0, True=1
'We pass our preferences for these to SaveAs_FixedFormat as a variant
array.
vSettings = Split("0,0,0,0", ",") '//edit to suit


'[Construct the Filename according to output path]
'NOTE: Do not include the filename extension
'when using the ExportAsFixedFormat feature.

'If output to ActiveWorkbook.Path, use
'.....................................
sFile = Split(ActiveWorkbook.FullName, ".")(0)
'Edit workbook ref to suit

'If output to a different path, use
'..................................
'Build sFile in logical steps
sFile = "C:\Users\Garry\Documents\VBA_Stuff\" '//path
'Append the filename as per your requirements
sFile = sFile & Split(ActiveWorkbook.name, ".")(0)
'Edit workbook ref to suit


'[Specifying a range of sheets, or a selected sheets grouping]
'................................................. ............
'To Export StartWith/EndWith range of sheets, use
rng = Split("1,2", ",") '//From=rng(0),To=rng(1)
'OR
'To Export a random grouping as selected while pressing 'Ctrl', use
rng = Split("1", ",") '//makes LBound=UBound


'[Exporting scenarios]
'To Export 1 file per selected sheet (random grouping)
SaveAs_FixedFormat FileType:=lTypeXPS, Filename:=sFile,
Settings:=vSettings
'OR
SaveAs_FixedFormat FileType:=lTypePDF, Filename:=sFile,
Settings:=vSettings


'To Export a From/To range of sheets to 1 file
rng = Split("1,2", ",")
SaveAs_FixedFormat FileType:=lTypeXPS, Filename:=sFile, _
Settings:=vSettings, IsGroup:=True, FromToRng:=rng
'OR
SaveAs_FixedFormat FileType:=lTypePDF, Filename:=sFile, _
Settings:=vSettings, IsGroup:=True, FromToRng:=rng


'To Export selected sheets to 1 file (random grouping)
rng = Split("1", ",")
SaveAs_FixedFormat FileType:=lTypeXPS, Filename:=sFile, _
Settings:=vSettings, IsGroup:=True, FromToRng:=rng
'OR
SaveAs_FixedFormat FileType:=lTypePDF, Filename:=sFile, _
Settings:=vSettings, IsGroup:=True, FromToRng:=rng


'To Export all sheets to 1 file
SaveAs_FixedFormat FileType:=lTypeXPS, Filename:=sFile, _
Settings:=vSettings, IsGroup:=True
'OR
SaveAs_FixedFormat FileType:=lTypePDF, Filename:=sFile, _
Settings:=vSettings, IsGroup:=True
End Sub 'Test_SaveAs_FixedFormat

Sub PrintTo_FixedFormat(FileType&, Filename$, _
Optional NumCopies& = 1, Optional FromToRng, _
Optional IsGroup As Boolean = False, _
Optional StampIt As Boolean = True)
' Prints the following choices via XPS Document Writer OR PDF printer,
' as specified by 'FileType':
' Selected sheets, 1 file per sheet;
' Or a specified From/To range of sheets to 1 file,
' Or selected sheets (random grouping) to 1 file;
' Or an entire workbook to 1 file.
'
' ArgsIn:
' FileType& lTypePDF=0; lTypeXPS=1
' Filename Contains "<path\<wkbName"
' NumCopies !
' IsGroup !
'
Dim wks As Worksheet, sExt$, sFile$, sStamp$
Dim sPrinter$, sDfltPrn$

Const sPrnXPS$ = "Microsoft XPS Document Writer on NE00:"
Const sPrnPDF$ = "deskPDF on DDM:"
'To quickly find the port your 'FileType' printer uses,
' - change the printer in the Print dialog and close without
printing;
' - in the VBE Immediate Window type the following, then press
'Enter';
' ?activeprinter
' - reset the printer in the Print dialog to your default!

'Initialize essential vars
sDfltPrn = Application.ActivePrinter '//reset when done
sPrinter = IIf(FileType = 0, sPrnPDF, sPrnXPS)
sExt = IIf(FileType = 0, ".pdf", ".xps")
sStamp = "_" & Format(Now(), "dd-mm-yyyy_hh-mm_AMPM")

If Not IsGroup Then '//1 file per sheet
For Each wks In ActiveWindow.SelectedSheets
sFile = Filename & "_" & wks.name & IIf(StampIt, sStamp & sExt,
sExt)
wks.PrintOut ActivePrinter:=sPrinter, Copies:=NumCopies, _
PrintToFile:=True, PrToFileName:=sFile
Next 'wks

Else
sFile = Filename & IIf(StampIt, sStamp & sExt, sExt)
If Not IsMissing(FromToRng) Then '//it's a range
If Not LBound(FromToRng) = UBound(FromToRng) Then
ActiveWorkbook.PrintOut From:=CLng(FromToRng(0)),
To:=CLng(FromToRng(1)), _
ActivePrinter:=sPrinter, Copies:=NumCopies,
PrintToFile:=True, PrToFileName:=sFile

Else '//it's a random grouping
ActiveWindow.SelectedSheets.PrintOut ActivePrinter:=sPrinter, _
Copies:=NumCopies, PrintToFile:=True, PrToFileName:=sFile
End If 'Not LBound(FromToRng) = UBound(FromToRng)

Else
ActiveWorkbook.PrintOut ActivePrinter:=sPrinter,
Copies:=NumCopies, _
PrintToFile:=True, PrToFileName:=sFile
End If 'Not IsMissing(FromToRng)
End If 'Not IsGroup

Application.ActivePrinter = sDfltPrn
End Sub 'PrintTo_FixedFormat

Sub Test_PrintTo_FixedFormat()
' Shows the various ways to use the PrintTo_FixedFormat routine.
' How the values passed to it are assembled is up to you!
' This example's focus is on how to prep the args only.

Dim sFile$, rng
Const lTypePDF& = 0: Const lTypeXPS& = 1

'[Construct the Filename according to output path]
'NOTE: Do not include the filename extension
'when using an XPS Document Writer.

'If output to ActiveWorkbook.Path, use
'.....................................
sFile = Split(ActiveWorkbook.FullName, ".")(0)
'Edit workbook ref to suit

'If output to a different path, use
'..................................
'Build sFile in logical steps
sFile = "C:\Users\Garry\Documents\VBA_Stuff\" '//path
'Append the filename as per your requirements
sFile = sFile & Split(ActiveWorkbook.name, ".")(0)
'Edit workbook ref to suit


'[Specifying a range of sheets, or a selected sheets grouping]
'................................................. ............
'To print StartWith/EndWith range of sheets, use
rng = Split("1,2", ",") '//From=rng(0),To=rng(1)
'OR
'To print a random grouping as selected while pressing 'Ctrl', use
rng = Split("1", ",") '//makes LBound=UBound


'[Printing scenarios]
'To print 1 file per selected sheet (random grouping)
PrintTo_FixedFormat FileType:=lTypeXPS, Filename:=sFile
'OR
PrintTo_FixedFormat FileType:=lTypePDF, Filename:=sFile


'To print a From/To range of sheets to 1 file
rng = Split("1,2", ",")
PrintTo_FixedFormat FileType:=lTypeXPS, Filename:=sFile,
IsGroup:=True, FromToRng:=rng
'OR
PrintTo_FixedFormat FileType:=lTypePDF, Filename:=sFile,
IsGroup:=True, FromToRng:=rng


'To print selected sheets to 1 file (random grouping)
rng = Split("1", ",")
PrintTo_FixedFormat FileType:=lTypeXPS, Filename:=sFile,
IsGroup:=True, FromToRng:=rng
'OR
PrintTo_FixedFormat FileType:=lTypePDF, Filename:=sFile,
IsGroup:=True, FromToRng:=rng


'To print all sheets to 1 file
PrintTo_FixedFormat FileType:=lTypeXPS, Filename:=sFile,
IsGroup:=True
'OR
PrintTo_FixedFormat FileType:=lTypePDF, Filename:=sFile,
IsGroup:=True
End Sub 'Test_PrintTo_FixedFormat

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,182
Default Running Word to Save as PDF

Yes, I just ran a simple msgbox command -
MsgBox Word.Application.Version

and it's 12.0 - so this should be much easier than it is...


Please explain how you got this MsgBox result; -was it in ref to your
automated instance? Was Word12 running at the time? What version opens
when you double-click a .doc file in FileExplorer?

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,182
Default Running Word to Save as PDF

Word.Application.Version will return the version of the last running
instance; So start v2003, close it, then run the code.

What's important is the default version 'registered' on your system!!!

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 178
Default Running Word to Save as PDF

so I ran in debug mode and figured out what was happening:
..ExportAsFixedFormat OutputFileName:=filename2, ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False

when I hovered over filename2 I got the string for the file name I wanted, which was good, but when I hovered over wdExportFormatPDF it said it was set = empty. After some interweb searching, wdExportFormatPDF is 17 and wdExportFormatXPS is 18 (if you ever need it), so I just swapped out wdExportFormatPDF with 17 (so it was no longer an empty variable) and voilĂ*, pay dirt!


On Thursday, January 26, 2017 at 3:41:41 PM UTC-7, GS wrote:
Word.Application.Version will return the version of the last running
instance; So start v2003, close it, then run the code.

What's important is the default version 'registered' on your system!!!

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,182
Default Running Word to Save as PDF

wdExportFormat? is a constant of Word.Application! So...

ExportFormat:=WordObject.wdExportFormatPDF

...should work. If you note how I determine this (PDF/XPS) you should be
able to duplicate my code for use with automated Word.

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
  #13   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 178
Default Running Word to Save as PDF

I totally appreciate all your help Garry!

On Thursday, January 26, 2017 at 4:10:25 PM UTC-7, GS wrote:
wdExportFormat? is a constant of Word.Application! So...

ExportFormat:=WordObject.wdExportFormatPDF

..should work. If you note how I determine this (PDF/XPS) you should be
able to duplicate my code for use with automated Word.

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #14   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,182
Default Running Word to Save as PDF

I totally appreciate all your help Garry!

Always glad to help when I can! Sorry I didn't catch sooner that
wdExportFormatPDF wasn't defined; -I don't use Word since I can do most
everything it can do directly in Excel.<g

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Test if Word is running John Excel Programming 7 April 13th 09 06:22 PM
when i save xls file, debug script is running and canno't save fil Imtiaz Excel Discussion (Misc queries) 1 July 16th 05 03:47 PM
How to save excel spreadsheet and word doc with one save CEP Excel Programming 0 April 29th 05 10:29 PM
running a word macro from xl steve Excel Programming 0 May 3rd 04 02:33 PM
running a word macro from xl Steve Excel Programming 0 April 30th 04 08:56 PM


All times are GMT +1. The time now is 09:44 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"