Print Area in Excel Addin

Print Area in Excel Addin

PrintArea:

Another recommendation before printing your data into paper or any digital format. If you set a print area in Excel, only that area will be printed. The print area is saved when you save the workbook. Followings are the steps that you need to perform to set print area before executing print else entire output would be messed.

  • Select the area that you want to print
  • Select Page Layout ribbon tab
  • Click on Print Area dropdown button (please refer screen shot below)
  • Choose Set Print Area
  • The selection will tun withing dashed boundary

Let’s do some code around to set print area or clear print area in C#, VB.Net and VBA (Visual Basic for Applications)

Prerequisites:

  • Visual Studio 2015 or above having Microsoft Office for Developer Tool installed
  • Create Excel Addin in style (Visual Studio Tools for Office)
  • Create a ribbon designer and put button

C# code example Set Print Area:

private void btnSetPrintArea_Click(object sender, RibbonControlEventArgs e)
{
	//reference active sheet
	excel.Worksheet oWorksheet = Globals.ThisAddIn.Application.ActiveSheet;

	//retain user selection 
	excel.Range oRange = Globals.ThisAddIn.Application.Selection;

	//get Page Setup object reference
	excel.PageSetup oPageSetup = oWorksheet.PageSetup;

	//Set print area
	oPageSetup.PrintArea = oRange.Address;
}

C# code example Clear Print Area:

private void btnClearPrintArea_Click(object sender, RibbonControlEventArgs e)
{
	//reference active sheet
	excel.Worksheet oWorksheet = Globals.ThisAddIn.Application.ActiveSheet;

	//retain user selection 
	excel.Range oRange = Globals.ThisAddIn.Application.Selection;

	//get Page Setup object reference
	excel.PageSetup oPageSetup = oWorksheet.PageSetup;

	//Set print area
	oPageSetup.PrintArea = string.Empty;
}

VB.Net code example Set Print Area:

 Private Sub setPrintArea_Click(sender As Object, e As RibbonControlEventArgs) Handles Button1.Click
        'Bind worksheet object reference
        Dim oWorkSheet as excel.Worksheet
        oWorkSheet = Globals.ThisAddIn.Application.ActiveSheet

        'Bind Range object from selection
        Dim oRange As excel.Range
        oRange=Globals.ThisAddIn.Application.Selection

        'Get pagesetup object
        Dim oPageSetup As excel.PageSetup
        oPageSetup=oWorkSheet.PageSetup

        'set print area
        oPageSetup.PrintArea=oRange.Address()

    End Sub

VB.Net code example Clear Print Area:

 Private Sub clearPrintArea_Click(sender As Object, e As RibbonControlEventArgs) Handles Button1.Click
        'Bind worksheet object reference
        Dim oWorkSheet as excel.Worksheet
        oWorkSheet = Globals.ThisAddIn.Application.ActiveSheet

        'Bind Range object from selection
        Dim oRange As excel.Range
        oRange=Globals.ThisAddIn.Application.Selection

        'Get pagesetup object
        Dim oPageSetup As excel.PageSetup
        oPageSetup=oWorkSheet.PageSetup

        'set print area
        oPageSetup.PrintArea=String.Empty

    End Sub

VBA code example Set Print Area:

Public Sub SetPrintArea()
	'get active sheet reference
	Dim oWorkSheet As Worksheet
	Set oWorkSheet = ActiveWorkbook.ActiveSheet

	'get PageSetup reference
	Dim oPageSetup As PageSetup
	Set oPageSetup = oWorkSheet.PageSetup

	'Change paper size
	oPageSetup.PrintArea = Selection.Address
End Sub

VBA code example Clear Print Area:

Public Sub ClearPrintArea()
	'get active sheet reference
	Dim oWorkSheet As Worksheet
	Set oWorkSheet = ActiveWorkbook.ActiveSheet

	'get PageSetup reference
	Dim oPageSetup As PageSetup
	Set oPageSetup = oWorkSheet.PageSetup

	'Change paper size
	oPageSetup.PrintArea = ""
End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *