PDA

View Full Version : Can't get my macro to run - it doesn't do anything



Sherrie
11-05-2011, 07:07 PM
Hi all, as you know, i have been posting questions for bits and pieces.

This is what I have ended up with so far. It is not complete, but I thought it better to get smaller chunks working before working on the next chunks.


Sub ElectronicFiling()

Call LoopInboxFolders

End Sub

Sub ProjYear()

Dim ns As Outlook.NameSpace
Dim myfolder As Outlook.Folder
Dim mysubfolder As Outlook.Folder
Dim strProjYear As String
Dim strProjYr As String

Set ns = Application.GetNamespace("MAPI")
Set myfolder = ns.GetDefaultFolder(olFolderInbox)
Set mysubfolder = myfolder.Folders

strProjYr = Left(mysubfolder.Name, 2)
strProjYear = "20" & strProjYr

End Sub

Sub CorrInOut()

Dim iItem As Long
Dim strAddress As String
Dim strCorr As String

With Outlook.ActiveExplorer.Selection

strAddress = .Item(iItem).SenderEmailAddress

If strAddress Like "*@gascoigneconsultants.com" Then
'Sender is internal therefore email is outgoing

strCorr = "Out"

Else

strCorr = "In"

End If

End With

End Sub

Sub SavePath()

Dim strSavePath As String

Call ProjYear
Call CorrInOut

strSavePath = "G:\" & strProjYear & "\Projects\" & mysubfolder.Name & "\Correspondence\" & strCorr

End Sub

Sub LoopInboxFolders()

Dim ns As Outlook.NameSpace
Dim myfolder As Outlook.Folder
Dim mysubfolder As Outlook.Folder

Set ns = Application.GetNamespace("MAPI")

'Get the default inboxfolder
Set myfolder = ns.GetDefaultFolder(olFolderInbox)

'Loop through each folder and display name of the folder
For Each mysubfolder In myfolder.Folders

Call SavePath

MsgBox mysubfolder.Name
MsgBox strSavePath

Next mysubfolder

End Sub


I have ensured that macros are enabled in Outlook 2007 however nothing happens when I run the main macro which is ElectronicFiling.

Can anyone please let me know what I've done wrong?

Thanks heaps!
Sherrie

JP2112
11-07-2011, 10:23 AM
What I would do is put the cursor inside the ElectronicFiling procedure and press F8 repeatedly to step through the code.

One thing I noticed was that you use "mysubfolder.Name" in your SavePath procedure, but this variable is defined locally in the LoopInboxFolders procedure. This variable will be invisible outside of that procedure.

What you need to do is pass the value of mysubfolder.Name to SavePath like this:

1) Add a parameter to SavePath -- redeclare it like this:

Sub SavePath(myFolderName As String)

2) Use the parameter inside SavePath -- change

strSavePath = "G:\" & strProjYear & "\Projects\" & mysubfolder.Name & "\Correspondence\" & strCorr

to

strSavePath = "G:\" & strProjYear & "\Projects\" & myFolderName & "\Correspondence\" & strCorr

3) Pass the parameter to SavePath when you call it -- in LoopInboxFolders, change

Call SavePath

to

SavePath mysubfolder.Name

You have this issue in other places as well -- in SavePath you also use variables that are locally declared in other procedures, for example strProjYear and strCorr.

Sherrie
11-07-2011, 07:52 PM
Thanks again for your reply!

I'm confused about something (not that that's difficult to do) -



Add a parameter to SavePath -- redeclare it like this:


VBA:

Sub SavePath(myFolderName As String)

VBA tags courtesy of www.thecodenet.com (http://www.thecodenet.com/)



I do not have any variables named myFolderName. Therefore is myFolderName a new variable?

JP2112
11-07-2011, 07:59 PM
It's not a new variable per se, it's just the name for the variable you will be passing inside the SavePath procedure. It doesn't matter what it's called, you could call it anything. You just need it so that you can pass the value of mysubfolder.Name into the SavePath procedure.