Hi,
I have a userfrom (Word 2010) with a listbox (lbPrinterList) and a command button (cmdClose).
In the Initialize event, i read the previous selectted listindex from the local registry.
I create a list of the the installed printers and
When running it the first time, I'll set the default listindex to -1 (nothing selected).
Then I create a list of installed printers and populate the listbox with it.
When I click then on a listitem (listbox click event ) I save the listbox listindex to the local registry.
Once i run the initialize event again i'll read the previous listindex back from the registry, but the listitem will not highlight, no matther what I try.
What am I doing wrong ?
Hope someone will bring up a solution.
Here's the full userform code + the printerlist code (source: Chip Pearson)
Best regards,
Ludo
Private Sub cmdClose_Click()
Unload Me
End Sub
Private Sub lbPrinterList_Click()
If bInitializeUserForm = True Then Exit Sub
sSelectedPrinter = Me.lbPrinterList.List(Me.lbPrinterList.ListIndex)
'save selected printer list index to the local PC registry
SaveSetting AppName:=sAppName, Section:=sSECTION_PRINT, Key:=sKEY_DEFAULT_PRINTER, setting:=Me.lbPrinterList.ListIndex
'
Me.cmdClose.Enabled = True
End Sub
Private Sub lbPrinterList_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
lbPrinterList_Click
Unload Me
End Sub
Private Sub lbPrinterList_Enter()
Me.lbPrinterList.BackColor = cYELLOW
End Sub
Private Sub lbPrinterList_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'check if printer selected from printerlist
If Me.lbPrinterList.ListIndex <> -1 Then
sSelectedPrinter = Me.lbPrinterList.List(Me.lbPrinterList.ListIndex)
Me.lbPrinterList.BackColor = cWHITE
With Me.cmdClose
.Enabled = True
.SetFocus
End With
End If
End Sub
Private Sub UserForm_Initialize()
Dim lListIndex As Long
bInitializeUserForm = True
lListIndex = GetSetting(AppName:=sAppName, Section:=sSECTION_PRINT, Key:=sKEY_DEFAULT_PRINTER, Default:=-1)
If bDuplexPintDllFound = True Then
PrinterDuplex_ListPrinters
Else
CreateInstalledPrinterList
End If
'
With Me
'center userform on screen
.Left = (Application.UsableWidth - Me.Width) / 2
.Top = (Application.UsableHeight - Me.Height) / 2
.cmdClose.Enabled = False
End With
'highlight the selected printer in the listbox
With Me.lbPrinterList
.ListIndex = lListIndex 'GetSetting(AppName:=sAppName, Section:=sSECTION_PRINT, Key:=sKEY_DEFAULT_PRINTER, Default:=-1)
.Selected(lListIndex) = True
End With
bInitializeUserForm = False
'
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode <> vbFormCode Then
Cancel = True
bCancelPrinting = True
Else
bCancelPrinting = False
End If
End Sub
Sub CreateInstalledPrinterList()
'http://www.cpearson.com/excel/GetPrinters.aspx
'modified to show only the installed network printers
Dim Printers() As String
Dim NetworkPrintersOnly() As String
Dim N As Long
Dim s As String
Dim i As Long
Dim iRow As Integer
Printers = GetPrinterFullNames()
For N = LBound(Printers) To UBound(Printers)
If Left(Printers(N), 1) = "\" Then
i = i + 1
ReDim Preserve NetworkPrintersOnly(i)
NetworkPrintersOnly(i) = Printers(N)
End If
Next N
frmSelectPrinter.lbPrinterList.List = NetworkPrintersOnly
End Sub
Display More