Need to export all sheets in one PDF file, so I found this piece of code, it works (exports a single PDF with a page for each sheet). But I don't want to use select / active statements, I prefer to use variables, that store the objects.
Question: How to avoid select/ ActiveSheet in this code?
ThisWorkbook.Sheets(Array("Sheet1", "Sheet2")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\tempo.pdf", Quality:= xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
What about this? Does this work:
TL;DR: You can't avoid
Select
in this case, because you need the late-bound behavior ofActiveSheet.ExportAsFixedFormat
, which apparently takes into account the selected sheets -- which the early-boundWorksheet.ExportAsFixedFormat
doesn't do.ExportAsFixedFormat
is a member ofExcel.Worksheet
.ActiveSheet
is not anExcel.Worksheet
though, it's anObject
(so any member calls against are necessarily late-bound). This is an important detail.When you do this:
The
.Select
member call is not made against aWorksheet
object: given an array of sheet names, theSheets
(orWorksheets
) property returns aExcel.Sheets
collection object, and that is what.Select
is being invoked against. This is also an important detail.As you probably know, the objects you deal with in the Excel object model are COM objects. In COM, an interface is extensible, unless specified otherwise: that's how you can write this:
And get an output - even if
Sum
is not a compile-time member of theApplication
class: the member call is resolved at run-time (that's what "late binding" is), and because the COM object is extended with a specific selection ofWorksheetFunction
members,Application.Sum
works perfectly fine at run-time, although you get no compile-time validation for any of it: you're basically coding blindfolded, and any typo in the member name will raise error 438 at run time (but will compile perfectly fine even withOption Explicit
specified), and any error in the arguments (wrong type, wrong order, wrong number of arguments) will raise error 1004 at run time.That's why you generally want to avoid implicit late-binding, and therefore coding against
ActiveSheet
andSelection
: because theseObject
objects (same for member calls againstVariant
) define no compile-time interface, using them is writing code blindfolded, and that's very error-prone.But early-bound code is not always 100% equivalent to the late-bound alternatives.
This is one such case: the
ExportAsFixedFormat
member behaves one way at run-time when early-bound, and behaves differently when late-bound. With a late-bound call you can export a single PDF document with a page for each worksheet in theSheets
collection, while an early-bound call againstWorksheet.ExportAsFixedFormat
only exports that sheet, and since there's noSheets.ExportAsFixedFormat
, you can't make that late-bound call directly againstSheets(Array(...))
to avoid the.Select
call and subsequentActiveSheet
late-bound member call.There are many other members, notably
WorksheetFunction
members, that behave differently when late bound vs early bound.