VBA将单元格值传递给“打印”对话框

这是我迄今为止build立的Sub:

Sub Grab_Screencap() 'Open URL With CreateObject("InternetExplorer.Application") .Visible = True .Navigate _ Worksheets("Queue").Range("A3").Value Application.Wait (Now + #12:00:02 AM#) SendKeys "^p", True Application.Wait (Now + #12:00:02 AM#) SendKeys "{UP}", True SendKeys "{UP}", True SendKeys "~", True Application.Wait (Now + #12:00:02 AM#) SendKeys "+{TAB}", True SendKeys "+{TAB}", True SendKeys "+{TAB}", True SendKeys "+{TAB}", True SendKeys "+{TAB}", True SendKeys "~", True End With End Sub 

我相信有更好的办法可以做,但我仍然在池的小孩一边。

这需要我在电子表格中的URL,然后打开IE,导航到该页面,打开“打印”对话框,select“XPS文档编辑器”,导航到path字段,然后突出显示该值。

现在我想传递一个基本目录,并从一个单元格的文件名称,如

 "C:\users\user1\desktop\" & Worksheets("Queue").Range("A5").Value 

修修补补,但不能find任何现有的文件,与我想要做的,我可以理解。

为了从Excel VBAmacros中打印外部文件的内容(例如,C:\ Temp \ TestFile.txt),可以使用ShellExecute()函数,如下所示。

首先,插入VBA Module (例如Module Module1 ),并将下列声明和Sub放入该模块Module1

 Public Declare Function ShellExecute 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 Const SW_SHOWNORMAL = 1 Public Sub ShellExecuteSample() 'shown as example: it will open the text file C:\Temp\TestFile.txt OpenFile = ShellExecute(hwnd, "open", "C:\Temp\TestFile.txt", "", "C:\", SW_SHOWNORMAL) 'this will print the content of the text file C:\Temp\TestFile.txt PrintFile = ShellExecute(hwnd, "print", "C:\Temp\TestFile.txt", "", "C:\", SW_SHOWNORMAL) End Sub 

当被调用时,这个Sub ShellExecuteSample()将打印内容并(可选)打开文本文件: C:\Temp\TestFile.txt 。 与您的情况相关,它可能是由您的string指定的文件:

 "C:\users\user1\desktop\" & Worksheets("Queue").Range("A5").Value 

显然,你不需要一个PrintDialog来完成这个任务。

希望这可能有帮助。

所以,我确定这不是实现这个任务的最优雅的方式,但是这就是我所能做到的,并且做到了我想做的事情:

 Sub Grab_Screencap() Dim i As Integer i = 3 Do Until IsEmpty(Cells(i, 2)) 'Copy Screencap Name Sheets("Queue").Cells(i, 6).Copy 'Open URL Set IE = CreateObject("InternetExplorer.Application") With IE .Visible = True .Navigate _ Worksheets("Queue").Cells(i, 2).Value Application.Wait (Now + #12:00:02 AM#) SendKeys "^p", True Application.Wait (Now + #12:00:02 AM#) SendKeys "{UP}", True SendKeys "{UP}", True SendKeys "~", True Application.Wait (Now + #12:00:02 AM#) SendKeys "{TAB}", True SendKeys "{TAB}", True SendKeys "{TAB}", True SendKeys "{TAB}", True SendKeys "{TAB}", True Application.Wait (Now + #12:00:02 AM#) SendKeys "~", True SendKeys "C:\Users\Johnny\Desktop\Job Listings\" Application.Wait (Now + #12:00:02 AM#) SendKeys "+{TAB}", True SendKeys "+{TAB}", True SendKeys "+{TAB}", True SendKeys "+{TAB}", True SendKeys "+{TAB}", True Application.Wait (Now + #12:00:02 AM#) SendKeys "^v", True Application.Wait (Now + #12:00:02 AM#) SendKeys "%s", True Application.Wait (Now + #12:00:02 AM#) IE.Quit End With i = i + 1 Loop End Sub 

101课是SendKeys可以发送一个完整的string,而不只是一个单一的密钥。 我并不需要将文件名与path合并。 我只需要删除path,制表符回到文件名,并删除文件名,我可以复制之前打开IE浏览器。

我认为只要打开打印对话框并激活打印文件就可以完成这项任务,但我还没有试图弄清楚。