2015-05-04 96 views
1

我是新的宏,可以真正使用一些帮助。下面的代码来自Excel,用于访问网页,搜索超链接并下载PDF文件并将其保存在桌面上。展望VBA代码

我需要修改它的展望:

  1. 因此,它检测到发件人地址,即[email protected]
  2. 检测在电子邮件和网页上的超链接来检测按钮“导出详细信息“并按下它
  3. 然后在下一页按下”导出“按钮并在桌面上保存CVS文件:”C:\ Users \ mlad1406 \ Desktop \ Test“。

我知道寻求帮助有很多,但我不知道如何前进。任何人都可以帮我调整这个代码吗?

谢谢!

Sub DownPDF() 
' This macro downloads the pdf file from webpage 
' Need to download MSXML2 and MSHTML parsers and install 

Dim sUrl As String 
Dim xHttp As MSXML2.XMLHTTP 
Dim hDoc As MSHTML.HTMLDocument 
Dim hAnchor As MSHTML.HTMLAnchorElement 
Dim Ret As Long 
Dim sPath As String 
Dim i As Long 

sPath = "C:\Users\mlad1406\Desktop\Test" 
sUrl = "https://copernicus.my.salesforce.com/00O20000006WD95" 

'Get the directory listing 
Set xHttp = New MSXML2.XMLHTTP 
xHttp.Open "GET", sUrl 
xHttp.Send 

'Wait for the page to load 
Do Until xHttp.readyState = 4 
    DoEvents 
Loop 

'Put the page in an HTML document 
Set hDoc = New MSHTML.HTMLDocument 
hDoc.Body.innerHTML = xHttp.responseText 

'Loop through the hyperlinks on the directory listing 
For i = 0 To hDoc.getElementsByTagName("a").Length - 1 
    Set hAnchor = hDoc.getElementsByTagName("a").Item(i) 

    'test the pathname to see if it matches your pattern 
    If hAnchor.PathName Like "Ordin-*.2013.pdf" Then 
     Ret = UrlDownloadToFile(0, sUrl & hAnchor.PathName, sPath, 0, 0) 

     If Ret = 0 Then 
      Debug.Print sUrl & hAnchor.PathName & " downloaded to " & sPath 
     Else 
      Debug.Print sUrl & hAnchor.PathName & " not downloaded" 
     End If 
    End If 
Next i 

End Sub 
+0

所以你没有关于你想要工作的事情,好吧。但是**你想要检测发件人邮件**?按一个按钮,你的意思是打开超链接? – R3uK

+0

嗨,我想在邮件地址列出的FROM:字段中检测它。关于第二个问题:电子邮件有一个超链接需要检测和按下,这将打开一个网页,并从那里到处都是按钮。 – maximladus

+1

好的,您可以使用我在“oMailItem.SenderEmailAddress”回答中给出的代码,并查找您的链接到主体中。然后,您可能需要使用HTML文档来查找您的按钮及其各自的链接。 – R3uK

回答

1

下面是一些代码,应该帮助你开始(如果你不看在邮件找到发件人地址):

你要找的字段是:oMailItem.SenderEmailAddress

Sub Extract_Body_Subject_From_Mails() 

Dim oNS As Outlook.NameSpace 
Dim oFld As Outlook.Folder 
Dim oMails As Outlook.Items 
Dim oMailItem As Outlook.MailItem 
Dim oProp As Outlook.PropertyPage 

Dim sSubject As String 
Dim sBody 

'On Error GoTo Err_OL 

Set oNS = Application.GetNamespace("MAPI") 
Set oFld = oNS.GetDefaultFolder(olFolderInbox) 
Set oMails = oFld.Items 

For Each oMailItem In oMails 
    MsgBox oMailItem.SenderEmailAddress 
     'MsgBox oMails.Count 'oMails.Item(omails.Find(
     sBody = oMailItem.Body 
     sSubject = oMailItem.Subject 
     'MsgBox sSubject 
     MsgBox sBody  
Next 

Exit Sub 
Err_OL: 
If Err <> 0 Then 
    MsgBox Err.Number & " - " & Err.Description 
    Err.Clear 
Resume Next 
End If 
End Sub 









'First create a rule that looks at the subject of incoming messages and fires when it sees "A new incident". Have the rule run a script. I called mine "Check_For_Ticket" in this example. See the pic of my rule attached. 
Sub Check_For_Ticket(MyMail As MailItem) 
    On Error GoTo Proc_Error 

    Dim strTicket, strSubject As String 

    ' Default value in case # is not found in the subject line 
    strTicket = "None" 

    ' Grab the subject from the message 
    strSubject = MyMail.Subject 

    ' See if it has a hash symbol in it 
    If InStr(1, strSubject, "#") > 0 Then 

     ' Trim off leading stuff up to and including the hash symbol 
     strSubject = Mid(strSubject, InStr(strSubject, "#") + 1) 

     ' Now find the trailing space after the ticket number and chop it off after that 
     If InStr(strSubject, " ") > 0 Then 
      strTicket = Left(strSubject, InStr(strSubject, " ") - 1) 
     End If 
    End If 
    MsgBox "Your Ticket # is: " & strTicket 

Proc_Done: 
    Exit Sub 

Proc_Error: 
    MsgBox "An error has occured in Check_For_Ticket. Error #" & Err & " - " & Err.Description 
    GoTo Proc_Done 
End Sub 
'Of course, you would substitute whatever processing you want where the messagebox shows the ticket number.