Option Strict On Imports System.ComponentModel Imports System.Drawing.Printing Imports System.Runtime.InteropServices Module Module1 Sub Main() 'デフォルトプリンタの用紙設定を行う Dim ps As New PrinterSettings For Each paperSize As PaperSize In ps.PaperSizes If paperSize.Kind = PaperKind.A3 Then ps.DefaultPageSettings.PaperSize = paperSize Exit For End If Next Dim pd As New PRINTER_DEFAULTS() Dim hPrinter As IntPtr pd.DesiredAccess = PRINTER_ALL_ACCESS If Not OpenPrinter(ps.PrinterName, hPrinter, pd) Then Throw New Win32Exception() End If Try Dim pi2 As PRINTER_INFO_2 = GetPrinterInfo2(hPrinter) Dim hMem As IntPtr = ps.GetHdevmode() Try pi2.pDevMode = GlobalLock(hMem) SetPrinter(hPrinter, 2, pi2, 0) Finally GlobalUnlock(hMem) GlobalFree(hMem) End Try Finally ClosePrinter(hPrinter) End Try End Sub Private Function GetPrinterInfo2(hPrinter As IntPtr) As PRINTER_INFO_2 Dim needed As Integer GetPrinter(hPrinter, 2, IntPtr.Zero, 0, needed) If needed <= 0 Then Throw New Win32Exception() End If Dim pPrinterInfo As IntPtr = Marshal.AllocHGlobal(needed) Try Dim temp As Integer If Not GetPrinter(hPrinter, 2, pPrinterInfo, needed, temp) Then Throw New Win32Exception() End If Return Marshal.PtrToStructure(Of PRINTER_INFO_2)(pPrinterInfo) Finally Marshal.FreeHGlobal(pPrinterInfo) End Try End Function End Module