Solved

Word VBA - pre-selecting a xxx.additem comboboxitem

Posted on 2007-11-19
4
289 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 500 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

773 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