通过 Excel 中的 VBA 设置打印机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2526584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printer setup via VBA in Excel
提问by Gina
I am trying to assign a cell in Excel for the user to type the printer name where they want the print out to go and then use that value in the
我正在尝试在 Excel 中为用户分配一个单元格以键入他们希望打印输出的打印机名称,然后在
Application.ActivePrinter = (use the cell value)
Even though I have done the programming assigning a name to the cell and using it in a variable it is giving me an error.
即使我已经完成了为单元格分配名称并在变量中使用它的编程,它也给了我一个错误。
I have set my variable as string, text, object and variant already and it's not working.
我已经将我的变量设置为字符串、文本、对象和变体,但它不起作用。
Do you know what code should I use to be able to do this?
你知道我应该使用什么代码才能做到这一点吗?
回答by guitarthrower
Without all the details I would guess that the user isn't entering all the information needed for the printer. On our network our printers are set up so that you need the print server, printer name and network name to get excel to print correctly. In the immediate window try this line of code to see how you need to enter the printer in the spreadsheet.
如果没有所有细节,我猜用户没有输入打印机所需的所有信息。在我们的网络上,我们设置了打印机,因此您需要打印服务器、打印机名称和网络名称才能使 excel 正确打印。在即时窗口中尝试这行代码,看看您需要如何在电子表格中输入打印机。
debug.Print application.ActivePrinter
You might want to help the user by supplying the server and network information (if it happens to be the same for all printers). In my office you'd use something like this:
您可能希望通过提供服务器和网络信息来帮助用户(如果所有打印机的信息都相同)。在我的办公室里,你会使用这样的东西:
Application.ActivePrinter = "\printserver\" & _
range("printername").value & " on Ne05:"
回答by Remi Despres-Smyth
Printer names are very particular - if it is not entered exactly as Windows expects it, you will get an error. (Since you did not note the specific error message in your question, I am assuming this is where the problem is; it is the most likely issue.)
打印机名称非常特殊 - 如果输入的名称不完全符合 Windows 的要求,您将收到错误消息。(由于您没有注意到问题中的具体错误消息,我假设这是问题所在;这是最有可能的问题。)
What I've done in this situation is provide the user with a list of available printers. You can use this code to populate a list (called lstPrinters):
在这种情况下,我所做的是向用户提供可用打印机的列表。您可以使用此代码来填充列表(称为 lstPrinters):
Private Sub LoadPrintersListBox()
Dim prtLoop As Printer
Dim strListRowSource As String
For Each prtLoop In Application.Printers
strListRowSource = strListRowSource + prtLoop.DeviceName + ";"
Next prtLoop
lstPrinters.RowSource = strListRowSource
End Sub
Then, you can use the user's selection to set the printer. (This code assumes a button called cmdSetPrinter is available, that the user will click once the printer is selected.)
然后,您可以使用用户的选择来设置打印机。(此代码假定名为 cmdSetPrinter 的按钮可用,一旦选择了打印机,用户就会单击该按钮。)
Private Sub cmdSetPrinter_Click()
Application.ActivePrinter = lstPrinters.Column(0)
End Sub
You can be confident that the printers in the list are named according to what the system will need, and you don't need to worry about typos.
您可以确信列表中的打印机是根据系统需要命名的,您无需担心打字错误。
回答by Waggers
If you face the problem that the printer in question isn't installed on their machine, this code should do the trick for you:
如果您遇到有关打印机未安装在他们的机器上的问题,此代码应该为您解决问题:
Set WshNetwork = CreateObject("WScript.Network")
PrinterPath = "\SERVER\PRINTER
WshNetwork.AddWindowsPrinterConnection PrinterPath