[英]VBA pass cell value to Print dialogue box
这是我到目前为止构建的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,导航到该页面,打开“打印”对话框,选择“ XPS Document Writer”,导航到“路径”字段,然后突出显示该值。
现在,我想传递基本目录和单元格中的文件名,例如
"C:\users\user1\desktop\" & Worksheets("Queue").Range("A5").Value
随意修改,但找不到与我能理解的内容相符的任何现有文档。
为了从Excel VBA宏打印外部文件(例如C:\\ Temp \\ TestFile.txt)的内容,可以使用ShellExecute()
函数,如下所示。
首先,插入VBA 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
。 与您的情况有关,它可能是您的字符串指定的文件:
"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可以发送整个字符串,而不仅仅是单个密钥。 我不需要将文件名与路径合并。 我只需要删除路径,重新选择文件名,然后删除文件名即可在打开IE之前将其复制。
我认为有一种方法可以通过打开打印对话框并激活打印到文件来完成此任务,但是我还没有尝试解决这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.