Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using VBA to Set SMTP Servers

Status
Not open for further replies.

Harlequ1n

Technical User
Jul 1, 2004
12
GB
Hi all.

Could anyone help or point me in the direction of resources for changing SMTP servers in Outlook 2003 using VBA...?

I use a laptop from two locations and need to change the SMTP servers daily and it's a real pain doing it through the interface because there are two to change and will be more in the future.

Any suggestions would be helpful


-----------------------
[colorface] Michael Mason [colorface]
-----------------------
 
Have you tried using profiles (outlook not windows) for each location?
 
I tried that at first Dan and it worked OK actually. But I had to set up seperate PST files and rules to copy all the MSG files from the secondary profile to the primary.

I've lost mail using this facility though.

I think VBA might be able to provide something a little more slick but as I've never used VBA for Outlook I need some help unfortunately.


-----------------------
[colorface] Michael Mason [colorface]
-----------------------
 
That's strange, I would think you could use the same PST in both profiles, removing the need to copy mail from one PST to the other.

There's no easy way to change the servers settings that I saw exposed in the Outlook object model. It may be possible to shell the .cpl file for mail settings and use send keys or the sendmessage library, but send keys IMO is unreliable and sendmessage is fairly advanced and usually more time consuming to code in than it's worth.
 
Hi
I am looking for the same answer i have to operate from three or four locations a week and its a real pain the profile option is no good as i need to have all mail and othere programs all up to date at each location and can not always change profiles to access them.
If you get a reply that work please let me know.
if i find it i will post it here.
 
Hi,

I am now going through the same problem and changing profiles is not really working well.
Did anyone figure out how to use VBA?
 
Hi,

I am now using a SendKey macro for each location and buttons placed in tool bars.
The Macro is:

Sub MACRONAME()

Dim AltKey As String
Dim CtrlKey As String
Dim ShiftKey As String
Dim TabKey As String
Dim EnterKey As String
'--------------------------
AltKey = "%"
CtrlKey = "^"
ShiftKey = "+"
TabKey = "{TAB}"
EnterKey = "~"

SendKeys AltKey & "(TA)", False
SendKeys EnterKey, False
SendKeys AltKey & "(C)", False

SendKeys TabKey, False
SendKeys TabKey, False
SendKeys TabKey, False

SendKeys "SMTP HERE", False

SendKeys AltKey & "(N)", False

SendKeys EnterKey, False
End Sub

It is possible to Autorun Macros at start up but I have not figured out how to pick which one to run.
Best.
 
Hi,
I am posting how I dealt with the issue in case it may help someone. Not the perfect solution but it works for me at the moment.

1. Create Macros in Outlook

Sub Location1()
'- change SMTP to desired Location1

Dim AltKey As String
Dim CtrlKey As String
Dim ShiftKey As String
Dim TabKey As String
Dim EnterKey As String
'--------------------------
AltKey = "%"
CtrlKey = "^"
ShiftKey = "+"
TabKey = "{TAB}"
EnterKey = "~"

SendKeys AltKey & "(TA)", False
SendKeys EnterKey, False
SendKeys AltKey & "(C)", False

SendKeys TabKey, False
SendKeys TabKey, False
SendKeys TabKey, False

SendKeys "SMTP FOR Location1", False

SendKeys AltKey & "(N)", False

SendKeys EnterKey, False
End Sub

2. Create Macros for each location required
3. Digitally sign the codes (you can use Selfcert.exe to get your certificate)

4. Create VBScript "outlook.vbs"

IP_Address = GetIP()
arrOctets = Split(IP_Address, ".")
Select Case arrOctets(0) & "." & arrOctets(1) & "." & arrOctets(2)
Case "111.222.3" 'Location = home
strMacro = "Location1"
Case "10.100.110" 'Location = office
strMacro = "Location2"
End Select
Set objShell = CreateObject("Wscript.Shell")
objShell.run "Outlook /autorun " & strMacro

Function GetIP()
Dim ws : Set ws = CreateObject("WScript.Shell")
Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
Dim TmpFile : TmpFile = fso.GetSpecialFolder(2) & "/ip.txt"
Dim ThisLine, IP
If ws.Environment("SYSTEM")("OS") = "" Then
ws.run "winipcfg /batch " & TmpFile, 0, True
Else
ws.run "%comspec% /c ipconfig > " & TmpFile, 0, True
End If
With fso.GetFile(TmpFile).OpenAsTextStream
Do While NOT .AtEndOfStream
ThisLine = .ReadLine
If InStr(ThisLine, "Address") <> 0 Then IP = Mid(ThisLine, InStr(ThisLine, ":") + 2)
Loop
.Close
End With
'WinXP (NT? 2K?) leaves a carriage return at the end of line
If IP <> "" Then
If Asc(Right(IP, 1)) = 13 Then IP = Left(IP, Len(IP) - 1)
End If
GetIP = IP
fso.GetFile(TmpFile).Delete
Set fso = Nothing
Set ws = Nothing
End Function


The code could be cleaned up but I cut and pasted from many sources (thanks all over the Web) and stopped when it worked. When you run the .vbs it checks the IP to identify your location and starts outlook plus the proper macro.

If you need to key on just the first two octets of the IP address, then replace this

Select Case arrOctets(0) & "." & arrOctets(1) & "." & arrOctets(2)

with

Select Case arrOctets(0) & "." & arrOctets(1)

The GetIP() function works but it will cause a problem if a wired and wireless NIC both have IP addresses at the same time. Also, SendKey may give trouble.
Again, thanks to all that helped me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top