这是我到目前为止构建的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文档编写器”,导航到“路径”字段,然后突出显示该值。
现在我想传递一个基本目录和一个单元格的文件名,比如
"C:\users\user1\desktop\" & Worksheets("Queue").Range("A5").Value
摆弄,但无法找到任何现有的文档,与我尝试做的事情一致,我能理解。
答案 0 :(得分:0)
要从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
来完成此任务。
希望这可能会有所帮助。
答案 1 :(得分:0)
所以,我确定这几乎不是实现任务的最优雅的方式,但这是我能够编写的,我想要它做的事情:
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之前将其复制。
我认为有一种方法可以通过打开打印对话框并激活打印到文件来完成此任务,但我还没有试图弄明白。