2014-02-05 176 views
2

如何在excel中使用VBA截取網頁截圖。 問題是隻有通過按下鍵盤的F6鍵才能拍攝屏幕截圖,因爲Screenhunter用於此目的。 打印屏幕鍵被禁用。如何使用VBA截取網頁截圖?

我用下面的代碼,但意識到sendkey功能無法使用:

sub test() 

     application.sendkeys "{F6}" 

    end sub 

請幫助。

+0

我已經回答了類似的問題。現在我不確定它是在這裏還是vbforums或msdn:p讓我搜索它 –

+0

順便說一句,不是F6的激活網頁瀏覽器的地址欄? –

+0

啊!找到它...看到[這](http://stackoverflow.com/questions/10759580/vba-webbrowser-capture-full-screen)。在你的情況下,由於打印屏幕不工作,你想要你'F6',把'調用keybd_event(VK_SNAPSHOT,0,0,0)'改爲'調用keybd_event(VK_F6,0,0,0)',作爲頂級常量,就像我爲'VK_SNAPSHOT'做的那樣。 [Here](http://msdn.microsoft.com/en-us/library/dd375731%28v=VS.85%29.aspx)是虛擬列表代碼的完整列表 –

回答

4

我在最大化屏幕後添加了一些延遲,創建了Word Doc並粘貼了屏幕截圖。其餘代碼取自Siddharth提供的鏈接。

Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long) 

Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long) 

Private Const VK_SNAPSHOT As Byte = 44 

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long 

Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long 

Private Const SW_SHOWMAXIMIZED = 3 
Private Const VK_LCONTROL As Long = &HA2 
Private Const VK_V = &H56 
Private Const KEYEVENTF_KEYUP = &H2 

Sub Sample() 
    Dim IE As Object 
    Dim hwnd As Long, IECaption As String 

    Set IE = CreateObject("InternetExplorer.Application") 

    IE.Visible = True 

    IE.Navigate "www.Google.com" 

    Sleep 5000 

    '~~> Get the caption of IE 
    IECaption = "Google - Internet Explorer" 

    '~~> Get handle of IE 
    hwnd = FindWindow(vbNullString, IECaption) 

    If hwnd = 0 Then 
     MsgBox "IE Window Not found!" 
     Exit Sub 
    Else 
     '~~> Maximize IE 
     ShowWindow hwnd, SW_SHOWMAXIMIZED 
    End If 
Sleep 3000 
    DoEvents 

    '~~> Take a snapshot 
    Call keybd_event(VK_SNAPSHOT, 0, 0, 0) 

    '~~> Start Word 


    Set wordobj = CreateObject("Word.Application") 

    Set objDoc = wordobj.Documents.Add 

    wordobj.Visible = True 

    Set objSelection = wordobj.Selection 

    'Paste into Word 
    objSelection.Paste 

End Sub 
+0

嗨,我今天有這個問題。我想打印屏幕http://www.hkab.org.hk/hibor/listRates.do?lang=en&Submit=Search&year=2014&month=1&day=10我改變IE瀏覽器。瀏覽此鏈接。它彈出IE窗口沒有找到!我怎樣才能做到這一點?它能夠加載頁面。雖然不能打印屏幕。 – useR