Monday, May 12, 2008

Outlook Automated File Request System

Here is the finished code for the Outlook File Request System.

File Server Code

This is event code which acts as an automated file server, putting files into the specified folder, and sending files back to the requestor, all via email. Once the code is installed, another user requests a file from you by using the following syntax in the Subject line:

Subject: FILEGET drive:path\filename

Where 'drive' is is a valid drive letter, 'path' is the folder and 'filename' is the name of the file requested.

For example,

Subject: FILEGET E:\Files\MyFile.doc

To send a file to another user's folder, subject should be:

Subject: FILEPUT D:\

And there should be at least one attachment to the email. All attachments will be saved to the same folder.

Note that for security reasons, I limited access to the C: drive, however if you set up a folder called 'Shared' on your desktop (Windows 2000/XP), you can place files there that you want to share, and others can request files from there. Of course if this isn't a concern for you, you can simply remove those restrictions.

Due to object model restrictions, I decided to remove the feature that replies to the group (if applicable). For example, if you CC: five other people on your request, the code simply ignores the other recipients. Originally, I had it set up to reply to them all. However, accessing the Recipients collection of the mail item triggered the object model guard, which was contrary to the intent of the code which is to fully automate the request process. You'd have to sit there clicking 'OK' every time someone sent you a request!

All of this is done via Outlook automation. Due to the blogging software I use (which I'll probably change soon) I cannot post the full code here. I will be demonstrating some of the techniques used to produce this code; click the link above to get the entire thing in the proper format.

First, we initialize the event code using the following code which should be familiar to you by now, if you have browsed the Outlook page. This is stock code which should be part of your code library to be reused over and over.


Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()

Dim objNS As Outlook.NameSpace
Set objNS = GetNamespace("MAPI")
Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub


All of this code should be placed in ThisOutlookSession module in your Outlook VBE. Just press Alt-F11, double-click on ThisOutlookSession and paste in the above code. Remember, after you install (or edit) the code in this module, you have to restart Outlook for any changes to take effect. Also, any existing code you have seems to stop working until you save and restart Outlook.

Here is the ItemAdd event code that is checking the Inbox for new mail items. We first set an object reference to the mail item that is passed as an argument. Then we determine the full path of the user's desktop folder, to figure out where the 'Shared' folder should sit. Keep in mind this will only work pre-Windows Vista. Then we check the subject line to see if it fits what we are looking for.


Private Sub Items_ItemAdd(ByVal item As Object)

If TypeName(item) = "MailItem" Then
Dim ToDo As String
Dim WhatAndWhere As String
Dim Msg As Outlook.MailItem
Dim MsgAttach As Outlook.Attachments
Dim MsgReply As Outlook.MailItem
Dim SlashSign As Long
Dim sPath As String
Dim sFile As String
Dim fso As Object
Dim UserN As String
Dim DeskTopSharedFolder As String
Dim strHelpText As String

' the mailitem is passed to the event code as an argument
Set Msg = item

' get current username so we can figure out the desktop folder name
UserN = Environ("username")
DeskTopSharedFolder = "C:\Documents And Settings\" & _
UserN & "\Desktop\Shared\"

On Error Resume Next
ToDo = Left$(Msg.Subject, 7)
WhatAndWhere = Right$(Msg.Subject, Len(Msg.Subject) - 8)
On Error GoTo 0


At this point we either have a FILEGET request or a FILEPUT request (or maybe neither). Then we cycle through every possible error condition (no attachments, malformed subject) and, if no errors occurred, call the sending sub which sends back the file requested (or saves the attachments to the specified folder).

In the interest of modular programming, there are actually four separate subs which each do something different:

1) The File Server Sub, acting as an intermediary sub which takes the validated user-input and passes it to the sending sub.

2) The Sending Sub, which takes four arguments (two optional) and is responsible for sending out all emails.

3) The Logging Sub, which (if you allow it) logs all file requests and sends to a CSV file, and

4) A String Checking Sub which checks if one string is found within another and returns a Boolean value (True or False).

Check out the full code here.

File Server Code

Enjoy,
JP

Labels: , , , , , , , , ,


Digg It! Stumble It! del.icio.us Technorati

Saturday, May 10, 2008

Free VB/C#/C++ Software

In keeping with the theme from my last post, I started looking for tutorials or software for other programming languages that might make an eventual transition to VSTO easier. I found some useful software from Microsoft that is actually free.

Check out:
http://www.microsoft.com/express/download/

You can download "lite" versions of VB 2008, C# 2008, C++ 2008, SQL Server 2005, as well as something called Visual Web Developer 2008.

The one that looks most interesting to me is SQL Server, which I installed but have yet to try out. My plan is to use it to further understand the syntax and commands of SQL. I also installed VB Express, and sooner or later I'll get around to trying it.

One of the blurbs that actually made me laugh said "Control your home lights using Visual Basic Express". I just use the Off switch.

Unfortunately you need to install their Silverlight technology to properly view the page. Whatever happened to plain old HTML?

They also offer a DVD image you can download and burn. Seems convenient, but why anyone would want to do this, I don't know.

So check it out and let me know if you download or try any of them out.

Enjoy,
JP

Labels: , , ,


Digg It! Stumble It! del.icio.us Technorati

Wednesday, May 7, 2008

VBA Rant #1

I'm sure by now you are aware that Microsoft has been distancing itself from VBA.

As of July 1, 2007, they are no longer licensing VBA (to new clients). For example, if you wrote a program today and wanted to integrate VBA in it, you are out of luck.

And the new version of Office for Mac will not support VBA at all.

They seem to be pushing their other technologies like VSTO for automating Office programs.

I feel I have to weigh in on this subject because it seems like Microsoft used VBA to lock users into using their software, and is now abandoning them.

Now I realize VBA will still be around for many years. Hell, XLM macros are still working in Excel even after VBA was introduced 13 years ago to replace it. I still see newsgroup posts from people asking for help with macros in Excel 97 (11 years old!). But how long will Microsoft maintain backward compatibility with a language it is discouraging people from using?

I have browsed other languages like .NET, C++, and realize just how familiar VBA is. Maybe it's "Grumpy Old Man Syndrome" setting in early; I can already hear myself saying things like "Back in my day, you could write a macro in 5 minutes to list all the files in a folder!"

This is going to mean that, in general, code will start getting harder to find. VBA is the People's Code; simple to integrate and implement by almost anyone with a little experience. In contrast, you would almost have to be a professional developer to get code working in another language to interact with Office.

"Visual Studio Tools for Applications makes it easier to write reliable, robust, and secure customizations by providing managed extensibility for both COM and .NET applications." [From VSTO Site]

It reminds me of the Mission Statement Generator from Dilbert's website.

Will VBA eventually be deprecated? Will people still be coding in VBA 20 years from now? Or am I just completely off it and worrying about nothing? I will accept any suggestions for what programming language I should start learning.


--JP

Labels: , , ,


Digg It! Stumble It! del.icio.us Technorati

Saturday, May 3, 2008

Excel training class and Outlook file request system updates

I've been mentioning the Excel training class for some time now, finally the curriculum is coming together and I finally got off my lazy butt and have almost completed the handouts and sample worksheets. I am hoping with fingers crossed to have this finally completed by the end of next week. Then I have to start taking public speaking classes to learn how to present this material in a way that won't put people to sleep. For practice, try explaining to someone an INDEX/MATCH array in a way that makes it sound fun!

In other news, the file request code I have been working on for Outlook is coming along quite nicely. It consists of three subs in a standard module, and the event code which would be placed in ThisOutlookSession. It's designed for business users (AKA "office drones like me") with network shares, who have better things to do than act as a human file server pushing files around all day. Let your lazy co-workers do it themselves! It would make you a hero while at the same time give you less work to do! Here are some of the features:

Request from, or submit files to, another user's computer or network share

C: drive is blocked, but you can create a folder on your desktop called "Shared" and place files in there that you want others to have access to (or place files in)

Friendly error messages in case of malformed requests (i.e. invalid path, no attachments).

Requestor can CC: others, and the code will send the requested file (along with confirmation email), to each of the recipients of the original message. However, ugly error messages are sent back to the original requestor only.

It also includes a built-in Help system, via the event code where users get an email back with instructions on how to request or send files to a folder. The instructions are also included at the bottom of every confirmation email.

All file sends and requests are logged to a .CSV file, the location and filename of which you can change as needed. The filename is passed as an argument to the function, so for example, you could update the code to write to different log files depending on whether someone was sending a file, or requesting a file. However the code as it stands will distinguish between the two when logging the request. Errors are also logged.

It's almost like a Kazaa or Limewire type setup where you have a shared folder and you trade files with other users, only in this case it's via email. And (hopefully, in your case) it's legal.


Enjoy,
JP

Labels: , , , ,


Digg It! Stumble It! del.icio.us Technorati

Monday, April 28, 2008

Internet Explorer Automation Object Library - Update for Excel 2007

One of the most popular pages on this site shows code for automating Internet Explorer. However I have only tested the code in Excel XP/2003. The purpose of this post is to credit a gentleman named Durand Sinclair who, using Excel 2007, pointed out to me that the name of the IE object library in Excel 2007 is actually called "Microsoft Browser Helper" (click Tools>References in the VBE). Once you select the checkbox, exit the dialog box and re-enter it, the name changes to "Microsoft Internet Controls".

If anyone else would like to verify this behavior in Excel 2007 and let me know, I would be grateful to hear it.

Thanks Durand!

--JP

Labels: , , ,


Digg It! Stumble It! del.icio.us Technorati

Contribute to this site!

It's not what you think.

I'm asking for contributors to the VBA search engine!

If you know of a website with VBA samples, or think you have what it takes to identify good websites with code, I'd like to know about it.

Visit the VBA Search Engine Homepage and click "Volunteer to contribute". Send me a message (click here) and let me know you are going to do so.

You'll need a Google account to contribute to the search engine. An added bonus is you can add the search box to your Google homepage, so you can search whenever you want, and (if my eyes don't deceive me) you can also add the search engine code to your own blog or website!


Enjoy,
JP


Digg It! Stumble It! del.icio.us Technorati