Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1116
  • Last Modified:

Print Area in excel from vb6 form

I am opening an excel file from a vb 6 form.  I want code to do foolowing:

1. Determine if there is a print area is set.
2. If there is no print area, locate the last cell(s) and set a print area.
2. Change View/Normal setting to View/Page Break Review.

Thanks
0
Mike Eghtebas
Asked:
Mike Eghtebas
  • 3
2 Solutions
 
ampapaCommented:
I think this will work.

Public Sub SetPA()

Dim ws As Worksheet
Dim pa As String

Set ws = ActiveSheet
pa = ActiveSheet.PageSetup.PrintArea

If pa = "" Then

With ws.PageSetup
 .PrintArea = ws.UsedRange.Address
End With

End If

End Sub
0
 
ampapaCommented:
Moving to VB....

Private Sub SetPA()
Dim xlApp As Excel.Application
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet

Set xlApp = CreateObject("Excel.application")
Dim pa As String

Set wb = xlApp.Workbooks.open("filename")
Set ws = wb.ActiveSheet
pa = ws.PageSetup.PrintArea

If pa = "" Then

With ws.PageSetup
 .PrintArea = ws.UsedRange.Address
End With

End If

Set ws = Nothing
Set wb = Nothing
xlApp.Quit
Set xlApp = Nothing

End Sub
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:

How about: <3. Change View/Normal setting to View/Page Break Review.>
0
 
craigewensCommented:
ActiveWindow.View = xlPageBreakPreview

or

for ampapa's VB version


Private Sub SetPA()
Dim xlApp As Excel.Application
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet

Set xlApp = CreateObject("Excel.application")
Dim pa As String

Set wb = xlApp.Workbooks.open("filename")
Set ws = wb.ActiveSheet

-------------------
ws.view = xlPageBreakPreview
-------------------

pa = ws.PageSetup.PrintArea

If pa = "" Then

With ws.PageSetup
 .PrintArea = ws.UsedRange.Address
End With

End If

Set ws = Nothing
Set wb = Nothing
xlApp.Quit
Set xlApp = Nothing

End Sub
0
 
ampapaCommented:
ActiveWindow.View = xlPageBreakPreview
ActiveWindow.View = xlNormalView


Private Sub SetPA()
Dim xlApp As Excel.Application
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet

Set xlApp = CreateObject("Excel.application")
Dim pa As String

Set wb = xlApp.Workbooks.open("filename")
Set ws = wb.ActiveSheet
pa = ws.PageSetup.PrintArea

If pa = "" Then

With ws.PageSetup
 .PrintArea = ws.UsedRange.Address
End With

End If

ActiveWindow.View = xlPageBreakPreview

Set ws = Nothing
Set wb = Nothing
xlApp.Quit
Set xlApp = Nothing

End Sub
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now