troubleshooting Question

PageSetupDlg problem

Avatar of MTecho
MTecho asked on
Visual Basic Classic
7 Comments1 Solution637 ViewsLast Modified:
I'm trying to get the current paper size dimensions (Height, Width and/or PaperSize) from the common dialog control, using the CommonDialog.ShowPrinter method. Most printer dialog panels have a combo box where the user can select a paper size, so the setting from that combo box should be available to the programmer. For some reason the CommonDialog object does not have properties for reading this result. Is PageSetupDlg the correct API function for getting this, and if so, what error have I made in the following code? (It only gives zeroes.)

Is 'Pr.hwndOwner = CommonDialog.hDC' the correct method of setting the context for the PageSetupDlg structure?

Public Type POINTAPI
   X As Long
   Y As Long
End Type

Public Type Rect
   Left As Integer
   Top As Integer
   Right As Integer
   Bottom As Integer
End Type

Public Type PageSetupDlg
       lStructSize As Long
       hwndOwner As Long
       hDevMode As Long
       hDevNames As Long
       flags As Long
       ptPaperSize As POINTAPI
       rtMinMargin As Rect
       rtMargin As Rect
       hInstance As Long
       lCustData As Long
       lpfnPageSetupHook As Long
       lpfnPagePaintHook As Long
       lpPageSetupTemplateName As String
       hPageSetupTemplate As Long
End Type

Declare Function PageSetupDlg Lib "comdlg32.dll" Alias "PageSetupDlgA" (pPagesetupdlg As PageSetupDlg) As Long

Public Function GetPaperSize(hDC As Long) As POINTAPI
   Dim Pr As PageSetupDlg, Suc As Long
   Pr.hwndOwner = hDC
   Suc = PageSetupDlg(Pr)
   Debug.Print Suc, Pr.ptPaperSize.X, Pr.ptPaperSize.Y
   GetPaperSize = Pr.ptPaperSize
End Function

Which can be called by:

Private Sub Print()
   On Error GoTo PrintError
   CommonDialog.CancelError = True
   CommonDialog.flags = cdlPDHidePrintToFile + cdlPDSelection + cdlPDReturnDC
   Printer.Orientation = CommonDialog.Orientation
   PrSz = GetPaperSize(CommonDialog.hDC)
   Printer.Height = PrSz.Y
   Printer.Width = PrSz.X
   ' Print statements
   Exit Sub
   Select Case Err.Number
   Case 32755
       'MsgBox "Print cancelled."
   End Select
   Exit Sub
End Sub

Can anyone help?

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros