Transposing text in text file to Excel Sheet.

S

sifar

Hi,

I am working on a code which will process a text file (TempFile.txt),
which contains a data copied from an Outlook .msg file containing a
table (single table-2 Columns data). The text file contains text
extracted from various MS Outlook .msg files & copied to a
TempFile.txt.

The data in generated textfile looks like :

Agent's Name
Thomas Rody

Gender
Male

etc....

What i want to do is to transpose the data under the same labels used
as Column Headers in an Excel sheet.


Code will look something like....

Sub ProcessTempFile(TempFileName as String, Datasheet as Worksheet)
....

I donot know how to extract data from a text file & tranpose
horizontally under each header column e.g.

Column A Column B Column C Column D
 
G

Gary Keramidas

see if this will get you going. i put my tempfile.txt in c:\files. look for
that in the code and change it to your location. also, watch for outlook
express truncating any lines of code


Dim RowNdx As Integer
Dim ColNdx As Integer
Dim WholeLine As String
Dim FName As Variant
Dim sRow As Integer
Dim lRow As Range
Dim fil As Variant
Dim FileDir As Variant
Dim x As Integer
Dim FilesInPath As String
Dim MyFiles() As String
Dim NumberOfFiles As Long
Dim lastrow As Long
Dim LastCol
Dim xR As Integer
Sub ImportText()
Application.ScreenUpdating = False
FileDir = "C:\Files\"
Sheets("sheet1").Range("A1").Select
ColNdx = ActiveCell.Column
RowNdx = ActiveCell.Row
sRow = RowNdx

' test for existing data
If Range("a1") > "" Then
MsgBox "Append And Clear Before" & Chr(10) & "Performing Another Import"
Exit Sub
End If

' determine # of files
FilesInPath = Dir(FileDir & "\*.txt")
NumberOfFiles = 0
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If

' perform import of email files
Do While FilesInPath <> ""
Open FileDir & FilesInPath For Input Access Read As #1
While Not EOF(1)
Line Input #1, WholeLine
Cells(RowNdx, ColNdx).Value = WholeLine
ColNdx = ColNdx + 1

Wend
RowNdx = RowNdx + 1
Close #1

NumberOfFiles = NumberOfFiles + 1

ReDim Preserve MyFiles(1 To NumberOfFiles)
MyFiles(NumberOfFiles) = FilesInPath
FilesInPath = Dir()
Loop

Application.ScreenUpdating = True

End Sub
 
S

sifar

Hi Gary,

The TempFile looks like this:
Start of text file.......................................

Subject: Modem Order

! Fill in the dotted boxes !


--------------------------------------------------------------------------------


MODEM ORDER [FAULTY MODEM]





1. CSA Name
jazz melano

2. Team Leader's name
Mani Ratnam

3. Date
23/09/2005

4. Time
0:13:00 PM GMT

5. Customer Forename
Wendy

6. Customer Surname
dorchester

7. Customer Email Address
test.gonet.co.uk

8. Address 1 (House Number/Name)
22, looney circle

9. Address 2 (Road)
FairyLand

10. Address 3 (Town/City)
Sussex

11. Address 4 (Country)
Rodeo

12. Postcode
SR2 0AL

13. FSBB Number
FSBB21112214

14. Account Number


15. NLD Number


16. Additional Notes


End of text file --------------------------------------------------
Now the Text file not only contains just one Escalation data extracted
from MSOutlook Inbox but many with the same Subject.
Each label value is placed under same label columns in an Excel sheet
named "Modem Order" starting from the 2nd Row (as 1st Row contains the
Labels).

e.g Column Labels in 1st Row of Excel sheet are :
1. CSA Name
2. Team Leader's name
3. Date
So, need to compare somehow the data in textfile with Excel Column
Labels & then paste their values under them in Excel Worksheet. e.g.

1. CSA Name 2. Team Leader's name 3. Date
Mani Ratnam Mani Ratnam 23/09/2005
Jazz Melano James Hadley 25/09/2005
Hosey Rathod Uri Geller 01/10/2005
Anthony Gonalves Johnny Liver 03/10/2005

Etc....

How to do this????
 
S

sifar

Hi Gary,

R u there??

Well, i went through your code & found that you are trying to read
different text files. What if its just a single text file say
TempFile.txt which contains the various MS Outlook email data as shown
in the Template source code as sent earlier?

Also, if there is a way to scan text file for a particular starting
column header in excel say, CSA's Name & then activate the
corresponding excel sheet & dump CSA's Name value under similar Column
Header in excel sheet?

if u have an email address, i can send u a copy of the worksheet which
i am working on.

sending you the source code of the worksheet. Sheet is Named (internal
name shtSetup, caption name Setup)
-------------------------------------------------------------------
Option Explicit

Sub ProcessAll()
Dim rngSubject
Dim retVal


Set rngSubject = shtSetup.Range("A14")

Do While rngSubject.Value <> ""

retVal = ProcessOutlookMessages(Trim(rngSubject.Value), _
Trim(rngSubject.Offset(0, 1).Value),
_
Trim(rngSubject.Offset(0, 2).Value))

If retVal >= 0 Then

rngSubject.Offset(0, 3).Value = rngSubject.Offset(0, 3).Value +
retVal

Else
'negative return value means error
MsgBox "Could not process mails"
Exit Sub
End If


Set rngSubject = rngSubject.Offset(1, 0)
Loop


End Sub


' Extract information from mail items with defined subject and
attachment
Function ProcessOutlookMessages(MailSubject As String, DataSheet As
String, _
ProcessedFolder As String)

Dim olApp As Outlook.Application
Dim fInbox As MAPIFolder
Dim olFolderArchive As Object
Dim olInboxCollection As Object
Dim olInboxItem As Object

Dim TempPath As String
Dim itemCount, i, n, iCount

'temp files saved here
TempPath = ThisWorkbook.Path & "\TempFile.txt"

'If this is the first time calling the sub - get a ref to Outlook
' (must be already running)
If olApp Is Nothing Then
Set olApp = GetOutlook()
End If

'should have it by now....
If olApp Is Nothing Then
ProcessOutlookMessages = -1
Exit Function
End If

'Get the inbox folder
Set fInbox =
olApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
Set olInboxCollection = fInbox.Items
itemCount = olInboxCollection.Count

iCount = 0
For n = itemCount To 1 Step -1
Set olInboxItem = olInboxCollection(n)
'check it's an email and not something else
If TypeName(olInboxItem) = "MailItem" Then

If StrComp(Trim(olInboxItem.Subject), MailSubject) = 0 Then

'Look for archive folder and create if doesn't exist.
Set olFolderArchive = EnsureInboxFolder(fInbox,
ProcessedFolder)

olInboxItem.SaveAs TempPath, olTXT
ProcessFile TempPath, DataSheet
'move the mail to the archive folder
olInboxItem.Move olFolderArchive
iCount = iCount + 1
End If

End If

Next n

ProcessOutlookMessages = iCount
Exit Function

haveError:
If Err <> 0 Then MsgBox "Error:" & vbCrLf & Err.Description

End Function


Sub ProcessFile(TempFilePath As String, WorkSheetName As String)
'#######################
'code goes here to process the temp file and extract the contents. NEED
CODE FOR THIS SUB...
'#######################
End Sub

Function GetOutlook() As Object

Dim olApp As Object

On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
On Error GoTo 0
If olApp Is Nothing Then
MsgBox "Outlook is not running: please open the application first"
End If

Set GetOutlook = olApp

End Function




Function EnsureInboxFolder(oInbox, FolderName) As Object

Dim oFold As Object

'Look for archive folder and create if doesn't exist.
On Error Resume Next 'ignore error
Set oFold = oInbox.Folders(FolderName)
If Err.Number <> 0 Then Err.Clear

If oFold Is Nothing Then
Set oFold = oInbox.Folders.Add(FolderName, olFolderInbox)
End If

Set EnsureInboxFolder = oFold

End Function

Please HELP ASAP!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top