打印输出选项的vba代码对我不起作用

时间:2017-10-06 07:42:10

标签: vba

尝试提供打印输出操作时出错,如果提供的任何解决方案都有帮助......

Dim objTextStream
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set objTextStream = fso.opentextfile("E:\NewFile.prn")`
objTextStream.PrintOut(1) 

// not executing

调试错误

Object doesn't support this property or method

2 个答案:

答案 0 :(得分:0)

您可以像这样遍历读取文件的每一行。然后,您可以debug.print该行或将其写入单独的写文件,如图所示。

Const ForReading = 1, ForWriting = 2
Dim FSO, FileIn, FileOut As Object
Dim strTmp As String

Set FSO = CreateObject("Scripting.FileSystemObject")
Set FileIn = FSO.OpenTextFile("E:\NewFile.prn", ForReading)
Set FileOut = FSO.OpenTextFile("E:\NewWriteFile.prn", ForWriting, True)

Do Until FileIn.AtEndOfStream
    strTmp = FileIn.ReadLine
    FileOut.WriteLine strTmp
Loop

FileIn.Close
FileOut.Close

答案 1 :(得分:0)

TextStream对象中没有打印方法(这是opentextfile返回的内容)。 要打印文件,您必须使用Windows API函数apiShellExecute

这是从http://www.dbforums.com/showthread.php?1066955-How-do-i-print-a-txt-file-with-VBA复制的(但未经测试):

如果将模块放在标准模块中,请将以下内容放在模块的声明部分(顶部),将Private更改为Public:

Private 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

然后在您需要打印的例程中,使用以下命令调用它:

lngReturn = apiShellExecute(hWndAccessApp, "print", strFileAndPath, vbNullString, vbNullString, 0)

其中strFileAndPath是您的文件名(例如"E:\NewFile.prn"