title: Change margins in Excel using VBScript Code
body: <p>I have a script that currently takes in an Excel document and turns it into a report (still in the Excel format), broken up in to two sheets: UPLOAD and PRINTOUT. </p>
  
  <p>The Upload sheet shows all of the information that was taken in from the original Excel doc and filters it into the right columns/rows for the report. The Printout sheet takes the Upload sheet and formats it further to be ready for an actual printout.</p>
  
  <p>The problem is, I'm trying to apply Orientation and Margin PageSetup parameters, but it is just ignoring them. No matter what I do, the page format won't change.</p>
  
  <p>Mainly, I need these parameters changed (margins are in inches):</p>
  
  <ul>
  <li>Landscape Orientation</li>
  <li>Left Margin (0.36)</li>
  <li>Right Margin (0.25)</li>
  <li>Top and Bottom Margins at (0.5)</li>
  <li>Header and Footer Margins at (0.25)</li>
  </ul>
  
  <p>Here's the Code where I think there's an issue:</p>
  
  <pre><code>'Start printout sheet
  
  Set objLastSheet = objWorkbook.Worksheets("Upload")
  Set objWorksheet = objWorkbook.Worksheets("Upload")
  objWorksheet.Copy, objLastSheet
  
  Set objWorksheet2 = objWorkbook.Worksheets("Upload (2)")
  objWorksheet2.Name = "Printout"
  
  Set objRange = objWorksheet2.Pagesetup
  Set objRange.PageSetup.Orientation= xlLandscape
  Set objRange.PageSetup.LeftMargin = Application.InchesToPoints(0.36) 
  Set objRange.PageSetup.RightMargin = Application.InchesToPoints(0.25)
  Set objRange.PageSetup.TopMargin = Application.InchesToPoints(0.5)  
  Set objRange.PageSetup.BottomMargin = Application.InchesToPoints(0.5) 
  Set objRange.PageSetup.HeaderMargin = Application.InchesToPoints(0.25) 
  Set objRange.PageSetup.FooterMargin = Application.InchesToPoints(0.25) 
  </code></pre>
  
  <p>I didn't write the rest of the code for the upload, I was just asked to make the margins work so I've been trying to add whatever need be to the existing code. Not sure if it's syntax or if I'm just missing something crucial. Thank you in advance!</p>
  
  <p><strong>EDIT</strong>: Current code. Orientation is Landscape and is fitting to one page wide now, but the Margin parameters are still not taking (When I go to print preview, it still says "Normal Margins"). </p>
  
  <pre><code>Const xlLandscape = 2
  
  With objWorksheet2.Pagesetup
  .Orientation = xlLandscape
  .Zoom = False
  .FitToPagesWide = 1
  .FitToPagesTall = False
  .LeftMargin = Application.InchesToPoints(0.5) 
  .RightMargin = Application.InchesToPoints(0.5) 
  .TopMargin = Application.InchesToPoints(0.5) 
  .BottomMargin = Application.InchesToPoints(0.5) 
  .HeaderMargin = Application.InchesToPoints(0.5) 
  .FooterMargin = Application.InchesToPoints(0.5)
  End With
  </code></pre>
  
  <p><strong>FINAL EDIT</strong></p>
  
  <p>For some reason, my Application.InchesToPoints() function wasn't working. Maybe I didn't have a library included or something, but nonetheless, when I manually converted the values and entered their point values, it worked.</p>
  
  <p><strong>Final Solution:</strong></p>
  
  <pre><code>Const xlLandscape = 2
  
  With objWorksheet2.Pagesetup
  .Orientation = xlLandscape
  .Zoom = False
  .FitToPagesWide = 1
  .FitToPagesTall = False
  .LeftMargin =  26
  .RightMargin =  18
  .TopMargin =  36
  .BottomMargin = 36
  .HeaderMargin = 18
  .FooterMargin = 18
  End With
  </code></pre>