VBA截取网页的截图,将其保存为文件,并将其附加到新的电子邮件中 [英] VBA to take screenshot of a web page, save it as file and attach it in new email

查看:2780
本文介绍了VBA截取网页的截图,将其保存为文件,并将其附加到新的电子邮件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是VBA世界的新手。但是我发现了一些有希望的东西:



如何使用vba拍摄网页的屏幕截图



它提供了如何在打开IE后拍摄截图的想法。



但是并没有说明如何将其保存在本地机器上,并附加到新的电子邮件中。



我该怎么做?



编辑 - 现在,我正在使用selenium并运行一个shell脚本来执行VBA中的selenium脚本来存储它。但是这样会更好

解决方案

Outlook的基本代码:

  Sub test_Prateek_Narendra()
Dim FilePath As String
Dim objMsg As Object
FilePath = StoreScreenShotFrom_As(www.google.com,TestScrenShot, jpg)

设置objMsg = Application.CreateItem(0)'olMailItem = 0
使用objMsg
.To =email@email.com
。 =Test Subject
.Attachments.Add FilePath
.Display
End with'objMsg
End Sub

并且屏幕截图的功能(全屏)并保存为文件:

 私有声明Sub Sleep Libkernel32(ByVal dwMilliseconds As Long)
#If VBA7然后
声明PtrSafe Sub keybd_event Libuser32(ByVal bVk As Byte,ByVal bScan作为Byte,ByVal dwFlags As LongLong,ByVal dwExtraInfo As LongPtr)
#Else
Declare Sub keybd_ev ent Libuser32(ByVal bVk As Byte,ByVal bScan As Byte,ByVal dwFlags As Long,ByVal dwExtraInfo As Long)
#End如果
Private Const VK_SNAPSHOT As Byte = 44

公共功能StoreScreenShotFrom_As(URL_Dest As String,Img_Name As String,Img_Type As String)
Dim IE As Object,IECaption As String
Dim aXL As Object,aWB As Object,aSh As Object,aChO As Object ,Img_Path As String
Img_Path = VBA.Environ $(temp)& \& Img_Name& &安培; Img_Type

设置IE = CreateObject(InternetExplorer.Application)
与IE
.Visible = True
.FullScreen = True
.Navigate URL_Dest

'''等待页面加载的可能性
'Do While .Busy或.readyState<> 4
'DoEvents
'循环
'''OR
'睡眠5000
'''OR(自定义子级别)
WasteTime 5

'''拍摄快照
调用keybd_event(VK_SNAPSHOT,0,0,0)
DoEvents
.Quit
结束与'IE

'''启动Excel
设置aXL = CreateObject(Excel.Application)
错误恢复下一步
使用aXL
.WindowState = -4143'xlNormal
.Top = 1
.Left = 1
.Height = .UsableHeight
.Width = .UsableWidth
.WindowState = -4137'xlMaximized
On Error GoTo 0
设置aWB = .Workbooks.Add
设置aSh = aWB.Sheets(1)
设置aChO = aSh.ChartObjects.Add(0,0,.Width,.Height)
以'aXL

结束aChO
。激活
.C hart.Paste
带.ShapeRange
.Line.Visible = msoFalse
.Fill.Visible = msoFalse
结束with。.ShapeRange
With .Chart
.Export FileName:= Img_Path,Filtername:= Img_Type,Interactive = = False
结束With。.Chart
DoEvents
.Delete
End with'oChrtO
aWB。关闭False
DoEvents
aXL.Quit

StoreScreenShotFrom_As = Img_Path
结束函数

私有子浪费时间(SecondsToWait As Long)
Dim TimeLater As Date
TimeLater = DateAdd(s,SecondsToWait,Now)
立即执行< TimeLater
DoEvents
循环
End Sub


I am new to the world of VBA . However I found something promising :

How to take a screenshot of webpage using vba

It gives an idea on how to take a screenshot after opening IE.

But it doesn't tell how to save it on local machine and attach it to a new email.

How do I do that?

Edit - right now, I am using selenium and running a shell script to execute the selenium script from VBA to store it. But this would be better

解决方案

Base code for Outlook :

Sub test_Prateek_Narendra()
    Dim FilePath As String
    Dim objMsg As Object
    FilePath = StoreScreenShotFrom_As("www.google.com", "TestScrenShot", "jpg")

    Set objMsg = Application.CreateItem(0) 'olMailItem = 0
    With objMsg
        .To = "email@email.com"
        .Subject = "Test Subject"
        .Attachments.Add FilePath
        .Display
    End With 'objMsg
End Sub

And the function to take the screen shot (in full-screen) and save it as a file :

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#If VBA7 Then
    Declare PtrSafe Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As LongLong, ByVal dwExtraInfo As LongPtr)
#Else
    Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
#End If
Private Const VK_SNAPSHOT As Byte = 44

Public Function StoreScreenShotFrom_As(URL_Dest As String, Img_Name As String, Img_Type As String)
    Dim IE As Object, IECaption As String
    Dim aXL As Object, aWB As Object, aSh As Object, aChO As Object, Img_Path As String
    Img_Path = VBA.Environ$("temp") & "\" & Img_Name & "." & Img_Type

    Set IE = CreateObject("InternetExplorer.Application")
    With IE
        .Visible = True
        .FullScreen = True
        .Navigate URL_Dest

        '''Possibilities to wait until the page is loaded
            'Do While .Busy Or .readyState <> 4
            '    DoEvents
            'Loop
        '''OR
            'Sleep 5000
        '''OR (custom sub below)
            WasteTime 5

        '''Take a snapshot
        Call keybd_event(VK_SNAPSHOT, 0, 0, 0)
        DoEvents
        .Quit
    End With 'IE

    '''Start Excel
    Set aXL = CreateObject("Excel.Application")
    On Error Resume Next
        With aXL
            .WindowState = -4143 'xlNormal
            .Top = 1
            .Left = 1
            .Height = .UsableHeight
            .Width = .UsableWidth
            .WindowState = -4137  'xlMaximized
    On Error GoTo 0
            Set aWB = .Workbooks.Add
            Set aSh = aWB.Sheets(1)
            Set aChO = aSh.ChartObjects.Add(0, 0, .Width, .Height)
        End With 'aXL

    With aChO
        .Activate
        .Chart.Paste
        With .ShapeRange
            .Line.Visible = msoFalse
            .Fill.Visible = msoFalse
        End With '.ShapeRange
        With .Chart
            .Export FileName:=Img_Path, Filtername:=Img_Type, Interactive:=False
        End With '.Chart
        DoEvents
        .Delete
    End With 'oChrtO
    aWB.Close False
    DoEvents
    aXL.Quit

    StoreScreenShotFrom_As = Img_Path
End Function

Private Sub WasteTime(SecondsToWait As Long)
    Dim TimeLater As Date
    TimeLater = DateAdd("s", SecondsToWait, Now)
    Do While Now < TimeLater
        DoEvents
    Loop
End Sub

这篇关于VBA截取网页的截图,将其保存为文件,并将其附加到新的电子邮件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆