Macro changing "page setup" not working - vba

I have a simple macro that was recorded to format some cells and also setup some some page formatting (margins footer header ect..) The macro runs without errors and it formats the cells just fine but does not save/change any of the page setup options. After i run it if i check the page setup everything is still at default. Below is the page setup portion. I have tried this on multiple files/workbooks it does not seem to save the changes no matter what. Any ideas?
Thanks
ActiveSheet.PageSetup.PrintArea = ""
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = "&F"
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = "Page &P of &N"
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0.25)
.RightMargin = Application.InchesToPoints(0.25)
.TopMargin = Application.InchesToPoints(0.5)
.BottomMargin = Application.InchesToPoints(0.5)
.HeaderMargin = Application.InchesToPoints(0.25)
.FooterMargin = Application.InchesToPoints(0.25)
.PrintGridlines = True
.Orientation = xlLandscape
.FirstPageNumber = xlAutomatic
.FitToPagesWide = 1
End With

I was having the same problem and later after some research, found the solution below.
"You cannot use page setup properties in Excel if no printers were installed"
http://support.microsoft.com/kb/291298
I hope this helps. Thanks.

Related

Excel VBA Application.PrintCommunication not working anymore with network printer

I always found my answers here in the past so I hope one of you Genius out there could pull one more trick for me. Here's the situation.
I've created a small application in Excel 2016 a couple of years ago. It's been running for 2 years now flawlessly. I have to say that it was at my previous job (but I still do kind of after sale service here...). Anyway, lately they moved to Office 365 and then kaboom! They can't use the feature that prints the report created.
The error is
Run time error 1004 Method 'PrintCommunication' of object
'_Application' failed
Remember that before they switch to Office 365. Everything was working fine.
Also, you have to know that if you use a local printer (plugged with a USB cable) it works perfectly (just like before). This method (local printer) was tested on 2 computers and works fine. But it won't work if it's a network printer in the company.
EDIT: I've just found out that it works in some part of the company and not in one. For example, it works fine for the employees of all provinces (we're in Canada: provinces are the equivalent of States in the US) except for one. So there must be something that is not compatible with a parameter on a server or something like that. Does it help someone?
END of EDIT
I've looked around here and on other sites. I've tried pretty much all the tricks about "commenting out" lines like "print quality = 600" and other stuff like that.
Below is the code. The error is at line
Application.PrintCommunication = True
4 lines before the "End Sub" . The other line when I switch .printCommunication = True there is no error there
Sub imprime_feuille_identification(trois_feuille)
'
Sheets("IDENTIFICATION").Activate
ActiveWorkbook.Worksheets("MOYENS_CONTROLE").Cells(9, 16384) = ActiveSheet.Name 'identifie de quelle feuille vient la demande d'impression sert à y revenir ensuite
ActiveWorkbook.Worksheets("MOYENS_CONTROLE").Cells(10, 16384) = "" 'va servir à identifier qu'on veut imprimer une seule feuille
Range("A1:P38").Select
ActiveSheet.PageSetup.PrintArea = "$A$1:$P$38" 'définition de la zone d'impression
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.PrintTitleRows = "$2:$2"
.PrintTitleColumns = ""
End With
Application.PrintCommunication = True
ActiveSheet.PageSetup.PrintArea = "$A$1:$P$38"
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = " &9&P de &N "
.LeftMargin = Application.InchesToPoints(9.84251968503937E-02)
.RightMargin = Application.InchesToPoints(9.84251968503937E-02)
.TopMargin = Application.InchesToPoints(9.84251968503937E-02)
.BottomMargin = Application.InchesToPoints(9.84251968503937E-02)
.HeaderMargin = Application.InchesToPoints(0.196850393700787)
.FooterMargin = Application.InchesToPoints(0.196850393700787)
.PrintHeadings = False
.PrintGridlines = False
'.PrintComments = xlPrintNoComments
'.PrintComments = False 'xlPrintNoComments
.PrintQuality = 600 'Tried to comment out this line: still get the error
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlLandscape
.Draft = False
.PaperSize = xlPaperLetter
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 0
.PrintErrors = xlPrintErrorsDisplayed
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.ScaleWithDocHeaderFooter = True
.AlignMarginsHeaderFooter = True
.EvenPage.LeftHeader.Text = ""
.EvenPage.CenterHeader.Text = ""
.EvenPage.RightHeader.Text = ""
.EvenPage.LeftFooter.Text = ""
.EvenPage.CenterFooter.Text = ""
.EvenPage.RightFooter.Text = ""
.FirstPage.LeftHeader.Text = ""
.FirstPage.CenterHeader.Text = ""
.FirstPage.RightHeader.Text = ""
.FirstPage.LeftFooter.Text = ""
.FirstPage.CenterFooter.Text = ""
.FirstPage.RightFooter.Text = ""
End With
Application.PrintCommunication = True 'THE ERROR HAPPENS HERE'
If trois_feuille <> 1 Then 'si le sub a été appelé en dehors du sub "imprime trois feuille" alors on fait
Application.CommandBars.ExecuteMso ("PrintPreviewAndPrint") 'affiche la page d'impression
End If
End Sub
So anyone, any bright idea and solution? Don't hesitate to ask precision if my explanations of the problem are not clear.
Thank you very much in advance.
The error is being caused by the line
.Zoom = False
When setting the Zoom property via VBA, you must set .Zoom = integer value between 10 and 400 (e.g., .Zoom = 25) if you want to control scaling using the zoom method. The value is converted to percentages by Excel (e.g., 10% to 400%) and then used as a multiplier.
If you want to control the number of pages wide and tall then use the properties .FitToPagesWide = some Integer and .FitToPagesTall = some Integer.
If you use the .FitTo... properties then .Zoom is set to false by Excel not the VBA code. Here is a link to MS Documentation explaining the use of .Zoom property.
So you'll need to remove or comment out either the .Zoom = False line or the two lines
.FitToPagesWide = 1
.FitToPagesTall = 0

Excel vba plot showing worksheet name + page number/adding title page in plot

I made the following script to automatic plot the active worksheet to a certain layout, in the right footer I would like to show the name of the active worksheet followed by the page number, 'edit: found the 'Activesheet.Name' formula, but still don't know how to add the page number (layout of page number: actual page numer/total amount of pages)
Example of a wanted right footer:
PartA - 1/12
Is it btw also possible to include a front page with just the name of the current worksheet, displayed in a big font in the center? Or is it needed to design the excel worksheet that way that the first page just contains one active cell with the title in the center?
Application.PrintCommunication = False
Application.Dialogs(xlDialogPrinterSetup).Show
With ActiveSheet.PageSetup
ActiveWindow.View = xlPageBreakPreview
ActiveSheet.PageSetup.PrintArea = "$A:$N"
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
Application.PrintCommunication = True
ActiveSheet.PageSetup.PrintArea = ""
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = "PROJECT X"
.RightHeader = ""
.LeftFooter = Sheets("instellingen").Cells(20, 2).Value
.CenterFooter = Sheets("instellingen").Cells(22, 2).Value
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0.7)
.RightMargin = Application.InchesToPoints(0.7)
.TopMargin = Application.InchesToPoints(0.75)
.BottomMargin = Application.InchesToPoints(0.75)
.HeaderMargin = Application.InchesToPoints(0.3)
.FooterMargin = Application.InchesToPoints(0.3)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = 600
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlPortrait
.Draft = False
.PaperSize = xlPaperA4
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 0
.PrintErrors = xlPrintErrorsDisplayed
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.ScaleWithDocHeaderFooter = True
.AlignMarginsHeaderFooter = True
.EvenPage.LeftHeader.Text = ""
.EvenPage.CenterHeader.Text = ""
.EvenPage.RightHeader.Text = ""
.EvenPage.LeftFooter.Text = ""
.EvenPage.CenterFooter.Text = ""
.EvenPage.RightFooter.Text = ""
.FirstPage.LeftHeader.Text = ""
.FirstPage.CenterHeader.Text = ""
.FirstPage.RightHeader.Text = ""
.FirstPage.LeftFooter.Text = ""
.FirstPage.CenterFooter.Text = ""
.FirstPage.RightFooter.Text = ""
End With
Application.PrintCommunication = True
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
Thanks :)
When you are looking for a syntax, recording a macro while you do the manual steps can be very helpful.
I used Excel's built-in Page 1 of ? custom filter to get the syntax you are after. Manually, I went to Page Layout > Page Setup > Header / Footer > Footer > Page 1 of ?
.RightFooter = ActiveSheet.Name & " - " & "Page &P of &N"
You can really force a page number, since you don't know how many pages there are. So it's the same as setting it in regular Excel.
Try this Page &[Page] of &[Pages]
Regular format taken from https://support.office.com/en-us/article/Add-or-remove-page-numbers-57ebb99e-0cfc-408b-864a-c805b5040ae7
Side note, isn't using .PageSetup slow? I can't remember if they fixed it or not? There is a trick with using the print setup from the Excel4 Macros that was much faster.

Excel 2010 Using VBA to print sheet(s)

I've been using the following code to print a sheet (with VBA) in a workbook. We are currently using Excel 2010. I've used this code for a long time and now I have a user who says the printed copy comes out extremely small and light.
The user says she has to enlarge the print to a #12 and bold all the info on the sheet so that the information is readable. Otherwise she claims it is impossible to read because it is so small.
Is there any properties I can add to this code or make changes to the existing properties to ensure the copy is more readable?
Would it be better to print to a PDF file?
Thanks for your help and suggestions.........
With ActiveSheet.PageSetup
.PrintTitleRows = "$1:$1"
.PrintTitleColumns = ""
.PrintArea = ""
.CenterHeader = "&A "
.RightFooter = "&""Arial,Regular""&8&P of &N"
.LeftMargin = Application.InchesToPoints(0.35)
.RightMargin = Application.InchesToPoints(0.35)
.TopMargin = Application.InchesToPoints(0.75)
.BottomMargin = Application.InchesToPoints(0.5)
.HeaderMargin = Application.InchesToPoints(0.5)
.FooterMargin = Application.InchesToPoints(0.5)
.PrintGridlines = True
.CenterHorizontally = True
.Orientation = xlLandscape
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = False
End With
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True

Regularly printing to Adobe PDF from Excel, script to force PDFs to be saved to correct path

I am working on a series of XLS files in Excel. When I am done with them, I need to print them to PDF so that they can be transmitted to a party outside my firm.
I recorded a fairly basic macro to allow me to print these files with the same setting every time. Unfortunately, it is not doing everything I need it to do.
Basically, I want Adobe to ask me where the file should be saved when it's being printed. That is not taking place right now with my script. If I manually print and select the settings "Ask to replace existing PDF file" from the Printer Properties window in Excel, it always asks me where the file should be saved. My script is not catching this, however, and I'm not sure what I need to add.
I have a lot of files to go through (and this is a task that will recur) so the sooner I get this nailed down the better.
Is this something that I can address with my Excel macro, and if so, can anyone point me in the right direction?
Sub PrintToAdobeRedactions()
'
' PrintToAdobeRedactions Macro
' Print redacted worksheets to Adobe with correct settings every time.
'
' Keyboard Shortcut: Ctrl+e
'
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
Application.PrintCommunication = True
ActiveSheet.PageSetup.PrintArea = ""
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = "[Tab]"
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = "Page [Page]"
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0.75)
.RightMargin = Application.InchesToPoints(0.75)
.TopMargin = Application.InchesToPoints(1)
.BottomMargin = Application.InchesToPoints(1)
.HeaderMargin = Application.InchesToPoints(0.5)
.FooterMargin = Application.InchesToPoints(0.5)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintSheetEnd
.PrintQuality = 600
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlLandscape
.Draft = False
.PaperSize = xlPaperLetter
.FirstPageNumber = xlAutomatic
.Order = xlOverThenDown
.BlackAndWhite = False
.Zoom = 100
.PrintErrors = xlPrintErrorsDisplayed
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.ScaleWithDocHeaderFooter = True
.AlignMarginsHeaderFooter = False
.EvenPage.LeftHeader.Text = ""
.EvenPage.CenterHeader.Text = ""
.EvenPage.RightHeader.Text = ""
.EvenPage.LeftFooter.Text = ""
.EvenPage.CenterFooter.Text = ""
.EvenPage.RightFooter.Text = ""
.FirstPage.LeftHeader.Text = ""
.FirstPage.CenterHeader.Text = ""
.FirstPage.RightHeader.Text = ""
.FirstPage.LeftFooter.Text = ""
.FirstPage.CenterFooter.Text = ""
.FirstPage.RightFooter.Text = ""
End With
Application.PrintCommunication = True
ActiveWorkbook.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False
End Sub
To SaveAs a PDF:
Sub SaveAsPDF()
Dim SaveName as String
SaveName = InputBox("Save As File Name?")
ThisWorkbook.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=Application.ActiveWorkbook.Path & Application.PathSeparator & SaveName & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End Sub

Change in Print Settings only works on first iteration of For loop

The code below is the code I have to set not only the print area, but margins and that the sheet is to fit onto one page. This code is wrapped up into a for loop that has many other sections both before and after that work just fine (so its not the 'Next i' being in the wrong place). This code will only work on the first iteration and will not work for any other iteration (each iteration includes code to create a new sheet that works just fine).
Why is this only working once? How do I get it to work on all iterations?
Application.PrintCommunication = True
ActiveSheet.PageSetup.PrintArea = Range(Cells(1, 1), Cells(53, 29)).Address
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.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)
.PrintHeadings = False
.PrintGridlines = True
.PrintComments = xlPrintNoComments
.PrintQuality = 600
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlLandscape
.Draft = False
.PaperSize = xlPaperLetter
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
.PrintErrors = xlPrintErrorsDisplayed
End With