vba courses london - table macro
The UK's Number 1 for Microsoft Office Training Add this page to your favourites/bookmarksBookmark page
 
View printable version of pagePrintable version
Plus One Google
Customer: Sign in
Delegate: Sign in
Trainer: Log in

Forum home » Delegate support and help forum » Microsoft VBA Training and help » vba courses london - Table Macro

vba courses london - Table Macro

resolvedResolved · Low Priority · Version Standard

replyReply Mon 4 Dec 2006, 09:47Delegate John said...

John has attended:
Excel VBA Intro Intermediate course

Table Macro

If anyone can see fault in the macro below please help me.
I use this macro to open outlook e mails and format the contents into a table. each time only a small amount of lines are produced as tables the rest as one continous column. any help appreciated

thanks

John






Sub GetHMASplits()
Dim myNameSpace As NameSpace
Dim myMailbox As String
Dim myInbox As MAPIFolder
Dim myUnreadFolder As MAPIFolder
Dim myReadFolder As MAPIFolder
Dim myUnreadMailItem As Object
Dim strBranchCode As String
Dim strBody As String
Dim intFullLength As Integer
Dim NextInString
Dim strHMA As String
Dim myInspector As Inspector

Dim myExcel As Excel.Application
Dim myWkb As Workbook
Dim myWks As Worksheet
Dim myRowNo As Integer

'Make reference to inbox
Set myNameSpace = ThisOutlookSession.GetNamespace("Mapi")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
'Make reference to HMA folders
myMailbox = "MailBox - $Finance Heal Regions"
Set myUnreadFolder = myNameSpace.Folders(myMailbox).Folders("HMA Unread")
Set myReadFolder = myNameSpace.Folders(myMailbox).Folders("HMA Read")

Set myExcel = New Excel.Application
myExcel.Visible = True
Set myWkb = myExcel.Workbooks.Add
Set myWks = myWkb.Worksheets(1)
myRowNo = 1
'Process until the unread message folder is empty
Do Until myUnreadFolder.Items.Count = 0
'Make a reference to the last email received
Set myUnreadMailItem = myUnreadFolder.Items.Item(1)
'Check to see if the item is a mail item
'If not, move the item to the Other Items folder
If myUnreadMailItem.Class <> olMail Then
myUnreadMailItem.Move myInbox
Else
'Check to see what the subject of the mail is and process as appropriate
If Mid(myUnreadMailItem.Subject, 6, 15) = "HMA Bonus Split" Then
strBranchCode = Left(myUnreadMailItem.Subject, 4)
strBody = myUnreadMailItem.Body
Do Until InStr(1, strBody, strBranchCode, 1) = 0
NextInString = InStr(2, strBody, strBranchCode, 1)
If NextInString = 0 Then
strHMA = strBody
strBody = ""
Else
strHMA = Left(strBody, NextInString - 1)
strBody = Right(strBody, Len(strBody) - Len(strHMA))
End If
myWks.Cells(myRowNo, 1) = strHMA
myRowNo = myRowNo + 1
Loop
Set myInspector = myUnreadMailItem.GetInspector
myInspector.CurrentItem.UnRead = False
myInspector.Close olDiscard
Set myInspector = Nothing
myUnreadMailItem.Move myReadFolder
Else

End If
End If
Loop
'Tidy up the data
myWkb.Activate
myWks.Activate
If myWks.Range("A1") <> "" Then
myWks.Columns("A:A").TextToColumns Destination:=myWks.Range("A1"), _
DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _
Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), _
Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1))
myWks.Cells.EntireRow.AutoFit
myWks.Cells.EntireColumn.AutoFit
myWks.Range("A1").Select
End If
'Free memory
Set myNameSpace = Nothing
Set myInbox = Nothing
Set myUnreadFolder = Nothing
Set myReadFolder = Nothing
Set myUnreadMailItem = Nothing
Set myInspector = Nothing
Set myExcel = Nothing
Set myWkb = Nothing
Set myWks = Nothing
End Sub

For upcoming training course dates see: Pricing & availability

replyReply Tue 12 Dec 2006, 09:44Trainer David said...

RE: Table Macro

Hi John

Your problem is proving quite complex, so far both our VBA trainners have looked at your code, along with a myself who has high level programming language knowledge (Java, C, C++). I am sorry to say that so far we have been unable to spot the problem that you have.

I would ask that if you have got any further with the problem, could you post back and let us know, as we are continuing to work on this problem for you.

Some advise that I think will be useful for you, is that when coding you should go through and write code comments as you go, so both yourself and others know exactly what your code is meant to be doing at each point. This will make your code much clearer to read for people that have not worked on coding it.

If we find an answer, we will get back to you as soon as possible.

David

 

Vba tip:

Count the Rows and Columns in a Selection

If you need to count the number of rows or columns in a worksheet use the following code:

Selection.Rows.Count - Returns the number of rows in the selection

Selection.Columns.Count - Returns the number of columns in the selection

Selection.CurrentRegion.Rows.Count - Returns the number of rows in the current region of the selection

View all VBA hints and tips


Microsoft Certified Partner Accredited Training Provider: Institute of IT Training Institute of Leadership and Management - Certified Courses Security Seal verified by visa, mastercard securecard