问题描述:

This is the Sub that I have built so far:

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

I am sure there are much better ways to do it, but I am still on the kiddie side of the pool.

This takes a URL that I have in a spreadsheets, and then opens IE, navigates to that page, opens the Print dialogue box, selects XPS Document Writer, navigates to the pathway field, and then highlights the value.

Now I want to pass a base directory, and a file name from a cell, something like

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

Tinkering around but cant find any existing documentation that lines up with what I'm trying to do that I can comprehend.

网友答案:

In order to print the content of the external file (for example, C:\Temp\TestFile.txt) from Excel VBA macro you can use ShellExecute() function as shown below.

First, insert the VBA Module (e.g. Module1) and place the following declarations and Sub into that 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

When called, this Sub ShellExecuteSample() will print the content and (optionally) open the text file: C:\Temp\TestFile.txt. Pertinent to your case, it could be the file specified by your string:

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

Apparently, you do not need a PrintDialog to complete this task.

Hope this may help.

网友答案:

So, im sure this isn't nearly the most elegant way to achieve the task, but this is what I was able to write that does what I want it to do:

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

The 101 lesson was that SendKeys can send an entire string, not just a single key. I didnt need to merge the file name with the pathway. I just needed to drop the pathway, tab back to file name, and drop the file name, which I could copy before opening IE.

I think there is a way to do this task just by opening the print dialogue and activating print to file, but I have not tried to figure that out yet.

相关阅读:
Top