Need help writing a macro. What would he save letters from a certain addressee in HTML format, ie if the letter came with a picture, the macro should save it in a place with a picture, and that it would be possible to view the letter in the browser. I tried to do it myself, but I don’t have enough experience.

 Sub SaveAsTXT() Dim myItem As Outlook.Inspector Dim objItem As Object Set myItem = Application.ActiveInspector If Not TypeName(myItem) = "Nothing" Then Set objItem = myItem.CurrentItem strname = objItem.Subject 'Prompt the user for confirmation Dim strPrompt As String strPrompt = "Are you sure you want to save the item? " & _ "If a file with the same name already exists, " & _ "it will be overwritten with this copy of the file." If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbYes Then objItem.SaveAs Environ("HOMEPATH") & "\My Documents\" & strname & ".html", olTXT End If Else MsgBox "There is no current active inspector." End If End Sub 

    1 answer 1

    One of the options for implementing the task is as follows:

    1. Analysis of recipients whose letters are to be saved in HTML format is produced using the built-in MS Outlook rules engine;
    2. Saving letters in HTML format is done using a macro.

    In detail.

    1. Create a macro code that saves letters in HTML format. Details in the comments:

     Option Explicit ' ограничСния макроса ' Ρ„Π°ΠΉΠ»Ρ‹ /ΠΏΠ°ΠΏΠΊΠΈ Π½Π΅ ΠΏΡ€ΠΎΠ²Π΅Ρ€ΡΡŽΡ‚ΡΡ Π½Π° Π½Π°Π»ΠΈΡ‡ΠΈΠ΅ ' для ΠΊΠΎΡ€Ρ€Π΅ΠΊΡ‚Π½ΠΎΠΉ Ρ€Π°Π±ΠΎΡ‚Ρ‹ Ρ„Π°ΠΉΠ»Ρ‹ / ΠΏΠ°ΠΏΠΊΠΈ Π½Π΅ Π΄ΠΎΠ»ΠΆΠ½Ρ‹ Π±Ρ‹Ρ‚ΡŒ созданы Π½Π° дискС Sub MailSaveToHTML(myItem As Outlook.MailItem) Dim sName As String ' имя ΠΏΠ°ΠΏΠΊΠΈ, имя Ρ„Π°ΠΉΠ»Π° = Ρ‚Π΅ΠΌΠ΅ письма Dim sSaveFolder As String ' ΠΏΠ°ΠΏΠΊΠ° для сохранСния письма Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ HTML Dim sFilesFolder As String ' Ссли письмо содСрТит присоСдинСнныС Ρ„Π°ΠΉΠ»Ρ‹, Ρ‚ΠΎ сохраняСм Π² ΠΏΠ°ΠΏΠΊΠ΅, которая создаСся ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ Π² Π²ΠΈΠ΄Π΅ <sName.files> Dim oAttchmnt As Outlook.Attachment sName = myItem.Subject sName = sReplacedSymbols(sName, "_") ' функция замСняСт нСдопустимыС символы Π² ΠΈΠΌΠ΅Π½Π°Ρ… Ρ„Π°ΠΉΠ»ΠΎΠ²/ΠΏΠ°ΠΏΠΎΠΊ sSaveFolder = CStr(Environ("USERPROFILE")) + "\Documents\" sFilesFolder = sSaveFolder + sName + ".files" ' ΠΎΠ±Ρ€Π°Π±Π°Ρ‚Ρ‹Π²Π°Π΅ΠΌ Ρ‚ΠΎΠ»ΡŒΠΊΠΎ ΠΏΠΎΡ‡Ρ‚ΠΎΠ²Ρ‹Π΅ сообщСния If TypeName(myItem) = "MailItem" Then ' Ссли ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠ° Π²Π½Π΅Π΄Ρ€Π΅Π½Π° Π² письмо, Ρ‚ΠΎ Π±ΡƒΠ΄Π΅Ρ‚ сохранСна Π² ΠΏΠ°ΠΏΠΊΡƒ sFilesFolder для отобраТСния Π² Ρ„Π°ΠΉΠ»Π΅ HTML myItem.SaveAs sSaveFolder + sName + ".htm", olHTML ' записываСм всС присоСдинСнныС Ρ„Π°ΠΉΠ»Ρ‹ Π² ΠΏΠ°ΠΏΠΊΡƒ sFilesFolder ' Ссли ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠ° Π²Π½Π΅Π΄Ρ€Π΅Π½Π° Π² письмо, Ρ‚ΠΎ ΠΏΠ΅Ρ€ΠΎΠ²Π½Π°Ρ‡Π°Π»ΡŒΠ½ΠΎ Π±ΡƒΠ΄Π΅Ρ‚ сохранСна с ΠΈΠΌΠ΅Π½Π΅ΠΌ image001.jpg (ΠΈ. Ρ‚. 002, 003 Ссли нСсколько) ' ΠΈ ΠΏΠΎΠ²Ρ‚ΠΎΡ€Π½ΠΎ ΠΊΠ°ΠΊ Π²Π»ΠΎΠΆΠ΅Π½ΠΈΠ΅ For Each oAttchmnt In myItem.Attachments oAttchmnt.SaveAsFile sFilesFolder + "\" + oAttchmnt.FileName 'MsgBox (oAttchmnt.FileName) Next Else 'всС ΠΏΡ€ΠΎΡ‡ΠΈΠ΅ сообщСния (Π·Π°Π΄Π°Ρ‡ΠΈ, встрСчи ΠΈ Ρ‚.ΠΏ. Π½Π΅ ΠΎΠ±Ρ€Π°Π±Π°Ρ‚Ρ‹Π²Π°ΡŽΡ‚ΡΡ) MsgBox ("Π‘ΠΎΠΎΠ±Ρ‰Π΅Π½ΠΈΠ΅ Π½Π΅ являСтся ΠΏΠΎΡ‡Ρ‚ΠΎΠ²Ρ‹ΠΌ") End If End Sub ' ΠΊΠΎΠ΄ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ Π·Π°ΠΌΠ΅Π½Ρ‹ символов нСдопустимых Π² имСнях Ρ„Π°ΠΉΠ»ΠΎΠ²/ΠΏΠ°ΠΏΠΎΠΊ взят с ' http://www.mrexcel.com/forum/general-excel-discussion-other-questions/714054-macro-save-selected-outlook-2010-email-folder-msg-file.html Function sReplacedSymbols(sStr As String, sSmbl As String) As String sReplacedSymbols = sStr sReplacedSymbols = Replace(sReplacedSymbols, "/", sSmbl) sReplacedSymbols = Replace(sReplacedSymbols, "\", sSmbl) sReplacedSymbols = Replace(sReplacedSymbols, ":", sSmbl) sReplacedSymbols = Replace(sReplacedSymbols, "?", sSmbl) sReplacedSymbols = Replace(sReplacedSymbols, Chr(34), sSmbl) sReplacedSymbols = Replace(sReplacedSymbols, "<", sSmbl) sReplacedSymbols = Replace(sReplacedSymbols, ">", sSmbl) sReplacedSymbols = Replace(sReplacedSymbols, "|", sSmbl) End Function 

    2.We create a rule that runs a macro on certain conditions (in our case, from certain recipients):

    2.1.Main-Rules-Create rule ...

    Home-Rules-Create rule ...

    2.2.Creating-Rules.

    Rule creation

    2.3.Master Rules-Run the script. Select the MailSaveToHTML script from the list.

    Rules Wizard - Run Script

    2.4. Rules Wizard - Completing Rules Setting

    enter image description here

    2.5.Rules and alerts

    Rules and alerts

    1. Use options:

    3.1.Activate the rule. At the time of receiving letters from the recipients specified in the Rule, a macro will be executed. When testing the receipt of letters from the yandex.ru mailbox with file attachments, even if their volumes are insignificant, no export occurs. Macro runs faster than files are loaded. As a solution - to set the delay execution of the macro.

    3.2. For example, enforce the rule for accepted and unread letters. The macro works correctly. Apply rule

    • thank you very much kind man. - Paul