How get list of available page size in combobox and change size

孤街醉人 提交于 2021-01-29 16:50:57

问题


i am trying to get list of all available page sizes in combobox as dropdown with vba. when user select the size, i need to change the worksheet size accordingly. also in another combobox i need to populate width and height of that paper size when page size in combobox is selected.

I tried something like.

Dim i As integer
for i = 1 to 30
activesheet.combobox1.value = Activesheet.PageSetup.Papersize(i)
next i

Thanks


回答1:


here's a solution based on This link

ActiveSheet.ComboBox1.List = GetPaperSizes

where GetPaperSizes is the following function you must place in a standard module:

Option Explicit

'Written: June 14, 2010
'Author:  Leith Ross
'Summary: Lists the supported paper sizes for the default printer in a message box.

Private Const DC_PAPERNAMES = &H10

Private Declare Function DeviceCapabilities _
  Lib "winspool.drv" _
    Alias "DeviceCapabilitiesA" _
      (ByVal lpDeviceName As String, _
       ByVal lpPort As String, _
       ByVal iIndex As Long, _
       ByRef lpOutput As Any, _
       ByRef lpDevMode As Any) _
    As Long

Private Declare Function StrLen _
  Lib "kernel32.dll" _
    Alias "lstrlenA" _
      (ByVal lpString As String) _
    As Long

Function GetPaperSizes() As Variant

    Dim AllNames As String
    Dim I As Long
    Dim Msg As String
    Dim PD As Variant
    Dim Ret As Long
    Dim papersizes() As Byte
    Dim PaperSize As String

    'Retrieve the number of available paper names
    PD = Split(Application.ActivePrinter, " on ")'<<<== change "on" with its local Language translation from english
    Ret = DeviceCapabilities(PD(0), PD(1), DC_PAPERNAMES, ByVal 0&, ByVal 0&)

    'resize the array
    ReDim papersizes(0 To Ret * 64) As Byte

    'retrieve all the available paper names
    Call DeviceCapabilities(PD(0), PD(1), DC_PAPERNAMES, papersizes(0), ByVal 0&)

    'convert the retrieved byte array to an ANSI string
    AllNames = StrConv(papersizes, vbUnicode)

     'loop through the string and search for the names of the papers
    For I = 1 To Len(AllNames) Step 64
        PaperSize = Mid(AllNames, I, 64)
        PaperSize = Left(PaperSize, StrLen(PaperSize))
        If PaperSize <> vbNullString Then Msg = Msg & PaperSize & vbCrLf
    Next I

    GetPaperSizes = Split(Left(Msg, Len(Msg) - 2), vbCrLf)

End Function


来源:https://stackoverflow.com/questions/61188638/how-get-list-of-available-page-size-in-combobox-and-change-size

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!