Send txt file to printer with Excel VBA

早过忘川 提交于 2019-12-08 09:54:52

问题


I have a sub that creates a .txt file and I want to print it in the default printer. How can I achieve this in VBA?

I think I need to call the ShellExecute API Function, but I did not find the correct sintax for that.

I would appreciate any help!


回答1:


I cannot comment yet, so most will ask you to show the code that you tried and then you get help here. I did a quick google search and found many examples. Search this and I found some code snippets

excel vba print file



回答2:


I found a code that do the trick:

Option Explicit 

Declare Function apiShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _ 
ByVal hwnd As Long, _ 
ByVal lpOperation As String, _ 
ByVal lpFile As String, _ 
ByVal lpParameters As String, _ 
ByVal lpDirectory As String, _ 
ByVal nShowCmd As Long) _ 
As Long 

Public Sub PrintFile(ByVal strPathAndFilename As String) 

    Call apiShellExecute(Application.hwnd, "print", strPathAndFilename, vbNullString, vbNullString, 0) 

End Sub 

Sub Test() 

    PrintFile ("C:\Test.pdf") 

End Sub 


来源:https://stackoverflow.com/questions/30332521/send-txt-file-to-printer-with-excel-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!