Solved

Word VBA - pre-selecting a xxx.additem comboboxitem

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
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.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now