VBA borderless print - vba

I have to set up a page in Excel VBA ... the problem is that the page must be borderless. So not just the usual way of the option left,right, top etc. , I also have to manipulate the printer to ensure that no border will be seen... well, I can do that manually, but even if I record the macro... after I finsihed I found out that nothing had been recorded at all for this issue .... don't need help with any macro recording I did it wright... the macro is there... with sub ..... but no VB Code at all... I also could code it manualy.. but how... there isn't any printer object.... any solution ?

I think the following code should help you.
Public Sub PrintButton1_Click()
Dim CurrRange As Range, CurrRange2 As Range
Set CurrRange = ThisWorkbook.Worksheets("Sheet1").Range("A1:--")
Set CurrRange2 = ThisWorkbook.Worksheets("rough").Range("A1:--")
CurrRange2.ClearContents
CurrRange.Copy
CurrRange2.PasteSpecial xlPasteAllExceptBorders
above statement should paste the content in the format you want to take printout.
Then setup the page using the following code and print
With PageSetup
.Zoom = False
.FitToPagesTall = 1
.FitToPagesWide = 1
End With
CurrRange2.PrintOut 1, 1, 1
End Sub

Related

PowerPoint crashes while running excel vba

I have currently written an Excel VBA code which creates 40+ PowerPoint Slides.
However, while running this code PowerPoint crashes after creating 22 slides.
This issue only occurs when the VBA code is run at one go. Because, when I try to run the same code line by line it runs successfully till end.
For me this is something weird. Do we have any suggestion for this problem ?
=> My code till creation of slide 2 is listed below (thereafter it create the other slides one after another till 43rd Slide)
Regards,
Alok
Sub test25()
Dim pApp As PowerPoint.Application
Dim pPres As PowerPoint.Presentation
Dim pSlid As PowerPoint.Slide
Set pApp = New PowerPoint.Application
pApp.Visible = True
pApp.Activate
Set pPres = pApp.Presentations.Open("C:\.....\Template.pptx")
pPres.Slides(1).Select
Sheets("S01").Select
ActiveSheet.Range("A1:P27").Select
ActiveSheet.Shapes.SelectAll
Selection.copy
pPres.Slides(1).Shapes.PasteSpecial DataType:=wdPasteText
pPres.Slides(2).Duplicate
pPres.Slides(2).Select
Sheets("S02").Select
ActiveSheet.Range("A1:P27").Select
ActiveSheet.Shapes.SelectAll
Selection.copy
pPres.Slides(2).Shapes.PasteSpecial DataType:=wdPasteText
End Sub
I see multiple potential issues, some of which are just code improvements.
On the line:
pPres.Slides(2).Duplicate
You are referring to slide 2, but you have not yet created slide 2 (as this is the line that creates it). Change this to:
pPres.Slides(1).Duplicate
I don't see how your code is running, even line by line, without that...unless 'template.pptx' already has all of the slides (in which case, why are you duplicating? I assumed 'template.pptx only contained the first slide)
Initially I suspected a race condition, but typically VBA handles these well. To be sure, and just general good practice, you may want to use a variable to refer to the current slide, instead of just a number (you already have the variable declared)
Set pSlid = pPres.Slides(1)
Sheets("S01").Select
ActiveSheet.Range("A1:P27").Select
ActiveSheet.Shapes.SelectAll
Selection.copy
pSlid.Shapes.PasteSpecial DataType:=wdPasteText
Set pSlid = pSlid.Duplicate
...
Also for your own convenience, you way want to look into using a loop, like:
Set pSlid = pPres.Slides(1)
For i = 1 to 43
Sheets("S" & Format(CStr(i), "00")).Select
ActiveSheet.Range("A1:P27").Select
ActiveSheet.Shapes.SelectAll
Selection.copy
pSlid.Shapes.PasteSpecial DataType:=wdPasteText
Set pSlid = pSlid.Duplicate
Next

Adding pagebreaks via VBA doesn't work

I created code to set the pagebreaks in an excel report to deal with the orphan issue (i.e. one line of text spills over onto the next page, etc.). The code works fine when I run it with the report open / visible.
It is part of a larger application which is opened and the code executed from MS Access. Excel is not visible during the process to improve performance.
When I run my code from MS Access it no longer works... it doesn't produce an error, but simply ignores the actual pagebreak setting command.
I read on various forums that in order to avoid this problem, excel needs to be first switched over to ActiveWindow.View = xlPageBreakPreview, but that doesn't work either (I suspect since Excel isn't visible).
I have tested for the following:
Code works when it is started manually or stepped through with F8
Code is executed when called upon from Access (I set breakpoints)
Switching the window view doesn't do anything either
How can I get Excel to change the pagebreaks via code when Excel is run in the background?
This is my code:
Sub TheOrphanProblem()
Dim iPageBrkRow
'Determine if there are page breaks and if so on which row of the document
If FindNthAutoPageBreak(wsRptHolding, 1) Is Nothing Then
'No pagebreak found so we exit the sub
Exit Sub
Else
iPageBrkRow = FindNthAutoPageBreak(wsRptHolding, 1).Row 'Get row
End If
Debug.Print iPageBrkRow
Dim x As Integer
Dim sCase As String
Dim rNewposition As Range
With wsRptHolding
'Code edited out for brevity. This part checks if there is an orphan problem and finds the new position for a pagebreak if needed.
It then provides that position as a range called "rNewposition".
'Moves page break to calculated position
ActiveWindow.View = xlPageBreakPreview
.HPageBreaks.Add rNewposition
ActiveWindow.View = xlNormalView
End With
End Sub
This is the code I use to find the pagebreak positions...
Private Function FindNthAutoPageBreak(Sht As Worksheet, Nth As Long) As Range
'Set page break of the last page so that sub asset groups are kept together
Dim HP As HPageBreak
Dim Ctr As Long
For Each HP In Sht.HPageBreaks
If HP.Type = xlPageBreakAutomatic Then
Ctr = Ctr + 1
If Ctr = Nth Then
Set FindNthAutoPageBreak = HP.Location
End If
End If
Next HP
End Function
Try this
ActiveSheet.DisplayPageBreaks = True

VBA - range.sort script unexpectedly runs other script it shouldn't run

I'm working on a workbook and I'm getting an error which is driving me crazy. I really have no idea what's going wrong here.
On the main worksheet (Bestandsübersicht) I have a combobox named ddBestand. On the change event of that combobox it runs a script that checks if certain buttons should be enabled or disabled. The code for this is:
Private Sub ddBestand_Change()
On Error GoTo ExitSub
Dim i As Integer
i = 3
Dim WS As Worksheet
Set WS = Sheets("Bestandsübersicht")
If ddBestand.Value = "" Then GoTo ExitSub
Do Until WS.Cells(i, 1).Value = ddBestand.Value
i = i + 1
Loop
If WS.Cells(i, 13).Value = 0 Or _
Right(Sheets("Bestandsübersicht").Range("AL1").Value, 3) <> "yes" Then
btnNetwork.Enabled = False
Else
btnNetwork.Enabled = True
End If
btnChange.Enabled = True
btnSpecifics.Enabled = True
btnCopy.Enabled = True
Exit Sub
ExitSub:
btnChange.Enabled = False
btnSpecifics.Enabled = False
btnNetwork.Enabled = False
btnCopy.Enabled = False
End Sub
This works totally fine when I use ddBestand. But sometimes when I run other scripts this script unexpectedly starts to run, even though those scripts do not relate to eachother. For example, when I run the initialize even for a userform (which is launched from another worksheet) it starts to run at this range.sort method:
Sheets("DB_Network").Columns("A:C").Sort key1:=Sheets("DB_Network").Range("A2"), _
order1:=xlAscending, Header:=xlNo
it gives the error 1004 (Unable to set the Enabled propert of the OLEObject class (which is logica, because as we're on another worksheet, the property for those buttons is wrong). As I didn't know how to stop the first script from running, I fixed the script by changing the OLEObjects to this:
Sheets("Bestandsübersicht").OLEObjects("btnChange").Object.Enabled = True
Solving the symptoms might not be the prettiest solution, but as I couldn't find out what the problem really was, I decided this was a suitable solution. But it got crazier. I'm still using another version of this document as I need it for my work. Somehow the same sort method started running the same script in the other document, which made the same error occur. Now I really want to solve this problem, as I don't want it to unexpectedly run scripts in other documents. Is there anybody out here who can help? Would be much appreciated!
I would guess that your combobox is directly linked to a range using the ListFillRange and/or LinkedCell properties. That is not a good idea for precisely this reason. I suggest you use code to populate the control using its .List property instead, which is easier and faster than .AddItem:
Sheets("Bestandsübersicht").OLEObjects("ddBestand").Object.List = Sheets("blah").Range("A2:A100").Value
for example.

Object required error in Workbook_Open with For loop

***It's working!! Thanks everyone for the help :D - turns out range was declared and set as "columnnrange" and the for loop was using "columnrange". It's the little things ;) . Appreciate the insight and guidance all!
*Thanks for the help everyone! I've updated the code per the responses. The error definitely comes when the For loop runs - if it is commented out there is no error. I believe the error is with the range "columnrange" object. Thanks!!
I have a simple Workbook_Open() routine that opens a UserForm "SelectData" and sets a ComboBox "DisplayData" value. I have a MsgBox that confirms the value set in the ComboBox "DisplayData".
Once set, the UserForm "SelectData" is hidden. Then, there is a "for" loop to hide all columns where the given cell in range "columnrange" is not equal to the ComboBox value. I'm getting an error "object required" but for the life of me cannot figure out where I'm going wrong. The goal of this spreadsheet is: "on open", allow the user to filter the visible columns on an excel doc exported from a SharePoint list.
Thanks in advance!!
Private Sub Workbook_Open()
Dim columnrange As Range
Dim cell As Range
Set columnnrange = ActiveWorkbook.Worksheets("owssvr").Range("G1:Z1")
SelectData.Show
MsgBox (SelectData.DisplayData.Value)
For Each cell In columnrange
If SelectData.DisplayData.Value <> cell Then
cell.EntireColumn.Hidden = True
Else
cell.EntireColumn.Hidden = False
End If
Next cell
End Sub
Change the loop condition to verify cell instead of columnrange. Here is a simplified code that should work.
For Each cell In columnrange
If SelectData.DisplayData.Value <> cell Then
cell.EntireColumn.Hidden = True
Else
cell.EntireColumn.Hidden = False
End If
Next cell
You will need to be sure the SelectData form is open. Do you have the code behind for the OK or Close button? If you use something like Me.Close then you won't have an object. You can, instead, use Me.Hide.

Get Printer Options in Excel VBA

I have a macro that I want to run before and after a user prints a worksheet in a Microsoft excel 2010 document. I am using the Workbook_BeforePrint method to do this. What I wanted to know is, how do I get the printer options that the user already selected when they printed the worksheet, so I can include those when I print the document using vba? For instance, my code below only prints 1 page no matter how many pages the user selected to print. I know I can use Thisworkbook.ActiveSheet.Printout copies:=2 or something like that, but I don't know how to get how many pages the user selected. Also, I'm not sure of anything else I might need to include for the print. I don't want anything from the options that the user selected to change. This would have been a lot easier if Microsoft had included a Workbook_AfterPrint method :( . Thanks in advance!
Here is what I have so far inside the workbook_beforeprint method
If (ThisWorkbook.ActiveSheet.name = "Printable Schedule" And user.colorPrintable = True) Then
Application.EnableEvents = False
Application.ScreenUpdating = False
Cancel = True
user.colorPrintable = False
Call updateEmployeeShifts
ThisWorkbook.ActiveSheet.PrintOut
user.colorPrintable = True
Call updateEmployeeShifts
Application.ScreenUpdating = True
Application.EnableEvents = True
End If
How about just using Application.Dialogs(xlDialogPrinterSetup).Show so that you can select the options you want and then print. Would that work?