Solved

Word VBA - pre-selecting a xxx.additem comboboxitem

Posted on 2007-11-19
4
299 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
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…
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 Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

710 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