Solved

Word VBA - pre-selecting a xxx.additem comboboxitem

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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 Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

820 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