0

I am using the below code to export emails as individual text files to system folder.I need to replace the third line in the text file as a string for all the text files each time in the loop.any one can suggest a solution

' General Declarations
Option Explicit

' Public declarations
Public Enum olSaveAsTypeEnum
  olSaveAsTxt = 0
  olSaveAsRTF = 1
  olSaveAsMsg = 3
End Enum

Sub COBExport_MailasMSG()
' Routine will take all selected mails and export them as .MSG files to the
' directory defined by
' Error Handling
On Error Resume Next

' Varaiable Declarations
Dim objItem As Outlook.MailItem
Dim strExportFolder As String: strExportFolder = "I:\Documents\"
Dim strExportFileName As String
Dim strExportPath As String
Dim objRegex As Object
Dim OldName As String, NewName As String

' Initiate regex search
Set objRegex = CreateObject("VBScript.RegExp")
With objRegex
.Pattern = "(\s|\\|/|<|>|\|\|\?|:)"
.Global = True
.IgnoreCase = True
End With

' Check if any objects are selected.
If Application.ActiveExplorer.Selection.Count = 0 Then
   MsgBox ("No item has been selected.")
Else
    ' Cycle all selected objects.
    For Each objItem In Application.ActiveExplorer.Selection
        ' If the currently selected item is a mail item we can proceed
        If TypeOf objItem Is Outlook.MailItem Then
            ' Export to the predefined folder.
            strExportFileName = objRegex.Replace(objItem.Subject, "_")
            strExportPath = strExportFolder & strExportFileName & ".txt"


            objItem.SaveAs strExportPath, olSaveAsTxt
            'MsgBox ("Email saved to: " & strExportPath)
            OldName = Dir(strExportPath)
    NewName = Left(strExportPath, Len(strExportPath) - Len(OldName)) & _
              Left(OldName, Len(OldName) - 4) & "Dir" & _
              CStr(Format(FileDateTime(strExportPath), "ddmmyyhhmmss")) & ".txt"
     Name strExportPath As NewName


        Else
            ' This is not an email item.
        End If
    Next 'objItem
End If



' Clear routine memory
Set objItem = Nothing
Set objRegex = Nothing

End Sub
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user3668427
  • 19
  • 1
  • 11
  • Are you just trying to change the name of the file? Or edit a line within the file. If that is the case what are you changing it from/to? – Matt Jul 24 '14 at 13:58
  • I want to edit a word with in the file.I need to replace a word with a character "abcdefgh". – user3668427 Jul 24 '14 at 14:07
  • Show a sample line and what you are trying to replace and an example of what you want it to look like. Your solution will be vague until that point. – Matt Jul 24 '14 at 14:09
  • example..In my file always there will be a word called "scantext" i need to replace this word with "Tscanfile".We can use the code in http://stackoverflow.com/questions/14840574/find-and-replace-string-in-all-excel-files-in-folder for this purpose.But i dont know how to integrate this in our code.Can you please help – user3668427 Jul 24 '14 at 14:12

1 Answers1

0

This solution might cut out the middle man of what you are trying to do. Instead of updating the file after it is exported why dont we just edit the body of the email before hand!

BE WARNED that this will temporarily change the body of your emails. If the process fails or the code is not used properly you can damage email permanently. You should test this on mail you don't care about.

I did try to copy the mail so that we could edit a copy but that ended up with another copy of the mail, in Outlook, that i could not programically delete. Therefore this solution seemed cleaner.

' declaration to go with the others
Dim strEmailBodybackup As String

' this will go in your for loop
' Save the body so that we can restore it after.
strEmailBodybackup = objItem.Body

' Edit the body of the mail to suit needs.
objItem.Body = Replace(objItem.Body, "scantext", "Tscanfile", , 1, vbTextCompare)

' Process the export like in your question

' Restore the body of the original mail 
objItem.Body = strEmailBodybackup

You can look up the Replace command here

Matt
  • 45,022
  • 8
  • 78
  • 119
  • Excellant!! I am very new to VBA that's why I am asking this questions...similarly how will I replace one more word in the same code..i want to replace "To" to " From" in the same. objItem.Body = Replace(objItem.Body, "scantext", "Tscanfile", 1,vbTextCompare objItem.Body, "To", "From", 1,vbTextCompare ). will this work? – user3668427 Jul 24 '14 at 16:30
  • Yes it will but remember that the '1' in this example is how many replaces will be performed. If you have other issues please make another question. Also stackoverflow has plenty of useful answers to help keep you going. So feel free to search here as well. – Matt Jul 24 '14 at 16:36