PDA

Vollständige Version anzeigen : Mail-Inhalt als .txt speichern


Gernot
17.11.2017, 09:05
Hallo,
ist es möglich den Inhalt einer email (eine zusammenhängende Zeichen/Nummernfolge) per vba auszulesen und in einem beliebigen Ordner (z.B. D:/Messwerte/M&Datum&Uhrzeit.txt zu speichern?

lg
Gernot

mumpel
17.11.2017, 09:45
Hallo!

Das ist möglich. Wie sieht denn der Aufbau der Email aus? Steht die Zeichenfolge immer an der selben Stelle? Und hat die Zeichenfolge immer die selbe Länge?

Gruß, René

Gernot
17.11.2017, 09:53
Betreff: Nachricht von der Homepage
Body Variante1 z.B. StKN~1~2
Variante2: Vor~Nach~addi~a~b~c~d~e~ .....

aber es ist immer nur ein String (Homepage-Angaben mit ~ getrennt), sonst nichts

lg
Gernot

mumpel
17.11.2017, 10:34
Ungetestet:

<pre style='border:thin solid #FF8000; padding:12px 24px; margin-left:12px; color:#000000'><span style='color:#0000EE'>Option</span> <span style='color:#0000EE'>Explicit</span> <span style='color:#0000EE'>Declare</span> <span style='color:#0000EE'>Function</span> WideCharToMultiByte <span style='color:#0000EE'>Lib</span> <span style='color:#FF0000'>&quot;kernel32.dll&quot;</span> (<span style='color:#0000EE'>ByVal</span> CodePage <span style='color:#0000EE'>As</span> <span style='color:#0000EE'>Long</span>, <span style='color:#0000EE'>ByVal</span> dwFlags <span style='color:#0000EE'>As</span> <span style='color:#0000EE'>Long</span>, _ <span style='color:#0000EE'>ByVal</span> lpWideCharStr <span style='color:#0000EE'>As</span> <span style='color:#0000EE'>Long</span>, <span style='color:#0000EE'>ByVal</span> cchWideChar <span style='color:#0000EE'>As</span> <span style='color:#0000EE'>Long</span>, _ <span style='color:#0000EE'>ByVal</span> lpMultiByteStr <span style='color:#0000EE'>As</span> <span style='color:#0000EE'>Long</span>, <span style='color:#0000EE'>ByVal</span> cbMultiByte <span style='color:#0000EE'>As</span> <span style='color:#0000EE'>Long</span>, _ <span style='color:#0000EE'>ByVal</span> lpDefaultChar <span style='color:#0000EE'>As</span> <span style='color:#0000EE'>Long</span>, _ <span style='color:#0000EE'>ByVal</span> lpUsedDefaultChar <span style='color:#0000EE'>As</span> <span style='color:#0000EE'>Long</span>) <span style='color:#0000EE'>As</span> <span style='color:#0000EE'>Long</span> <span style='color:#0000EE'>Sub</span> SaveBodyAsString() <span style='color:#0000EE'>Dim</span> olMailItem <span style='color:#0000EE'>As</span> Outlook.MailItem <span style='color:#0000EE'>Dim</span> strMailBody <span style='color:#0000EE'>As</span> <span style='color:#0000EE'>String</span> <span style='color:#0000EE'>Dim</span> strPfad <span style='color:#0000EE'>As</span> <span style='color:#0000EE'>String</span> <span style='color:#0000EE'>Select</span> <span style='color:#0000EE'>Case</span> <span style='color:#0000EE'>True</span> <span style='color:#0000EE'>Case</span> <span style='color:#0000EE'>TypeOf</span> Application.ActiveWindow <span style='color:#0000EE'>Is</span> Outlook.Inspector <span style='color:#0000EE'>Set</span> olMailItem = Application.ActiveInspector.CurrentItem <span style='color:#0000EE'>Case</span> <span style='color:#0000EE'>Else</span> <span style='color:#0000EE'>With</span> Application.ActiveExplorer.Selection <span style='color:#0000EE'>If</span> .Count <span style='color:#0000EE'>Then</span> <span style='color:#0000EE'>Set</span> olMailItem = .Item(<span style='color:#DDAA00'>1</span>) <span style='color:#0000EE'>End</span> <span style='color:#0000EE'>With</span> <span style='color:#0000EE'>If</span> olMailItem <span style='color:#0000EE'>Is</span> <span style='color:#0000EE'>Nothing</span> <span style='color:#0000EE'>Then</span> <span style='color:#0000EE'>Exit</span> <span style='color:#0000EE'>Sub</span> <span style='color:#0000EE'>End</span> <span style='color:#0000EE'>Select</span> <span style='color:#0000EE'>Rem </span><span style='color:#008000'>Mailbody auslesen (Tilte ersetzen durch Bindestrich) </span> strMailBody = Replace(olMailItem.Body, <span style='color:#FF0000'>&quot;~&quot;</span>, <span style='color:#FF0000'>&quot;-&quot;</span>) <span style='color:#0000EE'>Rem </span><span style='color:#008000'>Pfad erstellen </span> strPfad = <span style='color:#FF0000'>&quot;D:/Messwerte/M&quot;</span> & Format(Now, <span style='color:#FF0000'>&quot;DD-MM-YYYY hh-mm&quot;</span>) & <span style='color:#FF0000'>&quot;.txt&quot;</span> <span style='color:#0000EE'>Rem </span><span style='color:#008000'>Inhalt als Textdatei ausgeben </span> UTF8Output strPfad, strMailBody <span style='color:#0000EE'>End</span> <span style='color:#0000EE'>Sub</span> <span style='color:#0000EE'>Sub</span> UTF8Output(Datei <span style='color:#0000EE'>As</span> <span style='color:#0000EE'>String</span>, t <span style='color:#0000EE'>As</span> <span style='color:#0000EE'>String</span>, <span style='color:#0000EE'>Optional</span> BOM <span style='color:#0000EE'>As</span> <span style='color:#0000EE'>Boolean</span> = <span style='color:#0000EE'>False</span>) <span style='color:#0000EE'>Dim</span> tmp() <span style='color:#0000EE'>As</span> <span style='color:#0000EE'>Byte</span>, l <span style='color:#0000EE'>As</span> <span style='color:#0000EE'>Long</span>, FF <span style='color:#0000EE'>As</span> <span style='color:#0000EE'>Integer</span> <span style='color:#0000EE'>If</span> Len(Datei) = <span style='color:#DDAA00'>0</span> <span style='color:#0000EE'>Or</span> Len(t) = <span style='color:#DDAA00'>0</span> <span style='color:#0000EE'>Then</span> <span style='color:#0000EE'>Exit</span> <span style='color:#0000EE'>Sub</span> l = WideCharToMultiByte(<span style='color:#DDAA00'>65001</span>, <span style='color:#DDAA00'>0</span>, _ StrPtr(t), Len(t), <span style='color:#DDAA00'>0</span>, <span style='color:#DDAA00'>0</span>, <span style='color:#DDAA00'>0</span>, <span style='color:#DDAA00'>0</span>) <span style='color:#0000EE'>Redim</span> tmp(<span style='color:#DDAA00'>0</span> <span style='color:#0000EE'>To</span> l - <span style='color:#DDAA00'>1</span>) WideCharToMultiByte <span style='color:#DDAA00'>65001</span>, <span style='color:#DDAA00'>0</span>, StrPtr(t), Len(t), _ VarPtr(tmp(<span style='color:#DDAA00'>0</span>)), l, <span style='color:#DDAA00'>0</span>, <span style='color:#DDAA00'>0</span> FF = FreeFile <span style='color:#0000EE'>Open</span> Datei <span style='color:#0000EE'>For</span> Output <span style='color:#0000EE'>As</span> #FF <span style='color:#0000EE'>Close</span> #FF FF = FreeFile <span style='color:#0000EE'>Open</span> Datei <span style='color:#0000EE'>For</span> Binary <span style='color:#0000EE'>As</span> #FF <span style='color:#0000EE'>Put</span> #FF, , tmp <span style='color:#0000EE'>Close</span> #FF <span style='color:#0000EE'>End</span> <span style='color:#0000EE'>Sub</span> <br><br><hr style='color: #FF8000; background-color:#FF8000; height:1px; border:none;' /><p style='font-size:9px; font-family: Verdana; text-align: left; '><a href='http://www.rholtz-office.de/vba-html' target='_blank'>VBA/HTML-CodeConverter</a>, AddIn f&uuml;r Office 2002-2016 - in VBA geschrieben von <a href='http://www.office-loesung.de/viewprofile19265.php'>Lukas Mosimann</a>. Projektbetreuung:<a href='http://www.rholtz-office.de'>RMH Software & Media</a></p><p style='font-size:11px; font-weight:bold; color:#0000FF; font-family:Verdana; text-align:left;'>Code erstellt und getestet in Office 16 - mit VBAHTML 12.6.0</p></pre>