Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Word VBA - pre-selecting a xxx.additem comboboxitem

Posted on 2007-11-19
4
Medium Priority
?
322 Views
Last Modified: 2012-08-13
Hi -

I have a bespoke printer box for a user to select plain & headed copies of their document in Word VBA.  I have hard-coded the printers into a combobox form, part of which as follows:

Public Sub UserForm_Initialize()
ComboPtrList.AddItem "HP Laserjet 4350 - Secretaries Left"
ComboPtrList.AddItem "HP Laserjet 4350 - Secretaries Right"
ComboPtrList.AddItem "HP Laserjet 4100 - 4th Floor Risk Assessment"
ComboPtrList.AddItem "HP Laserjet 4100 - 5th Floor Lloyd"
ComboPtrList.AddItem "HP Laserjet 4100 - 5th Floor Nick"
ComboPtrList.AddItem "HP Laserjet 4100 - 5th Floor John"
End Sub

What I need is to be able to pre-select one of the items depending in a variable which will be passed as to pre-select their current printer - ie, if they previously printed to "5th Floor Lloyd" then this printer will be selected the next time the box comes up.  The text string variable value of the current printer will be passed to the sub.

I am sure this is easy but for love nor money cannot find how to do it!

Thanks.
0
Comment
Question by:justinkent
  • 2
4 Comments
 
LVL 12

Expert Comment

by:geneus
ID: 20312175
Is this what you are looking for:

'get the current default printer
varDefaultPrinter = ActivePrinter

0
 
LVL 12

Expert Comment

by:geneus
ID: 20312201
Here is another example:

Sub DefPrinter()

'In the beginning of your code before you set the new printer
'get the current default printer
varDefaultPrinter = ActivePrinter

'See what is assigned
MsgBox varDefaultPrinter

'Set to a temporary Printer
ActivePrinter = "\\ServerX\PrinterNameY" 'NOTE: You need to select a Printer Designation here

'At the end of your code, reset to the Default Printer
ActivePrinter = varDefaultPrinter

'See what is assigned
MsgBox varDefaultPrinter

End Sub
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 20312220

Public Sub UserForm_Initialize()
    Dim i As Integer
   
    ComboPtrList.AddItem "HP Laserjet 4350 - Secretaries Left"
    ComboPtrList.AddItem "HP Laserjet 4350 - Secretaries Right"
    ComboPtrList.AddItem "HP Laserjet 4100 - 4th Floor Risk Assessment"
    ComboPtrList.AddItem "HP Laserjet 4100 - 5th Floor Lloyd"
    ComboPtrList.AddItem "HP Laserjet 4100 - 5th Floor Nick"
    ComboPtrList.AddItem "HP Laserjet 4100 - 5th Floor John"
    For i = 0 To ComboPtrList.ListCount - 1
        If ComboPtrList.List(i) = strPassedParameter Then
            ComboPtrList.ListIndex = i
            Exit For
        End If
    Next i
End Sub



0
 

Author Closing Comment

by:justinkent
ID: 31409925
Brilliant. Just what I needed. Thanks.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Lost Word File? Eagerly, need it back? Read ahead; this File Recovery guide is for you.
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question