being backward compatible

C

Charlie

I have some macros in a word document. Some people use it in Word 2000
instead of Word 2003. I keep having to tell them how to change a reference
to Outlook Library 11.0 to 10.0 (Or something like that). Is there a way I
can make my document automatically see there version of Word and fix this
reference itself?
thanks,
ck
 
C

Charles Kenyon

Do you need the reference? If not delete it.
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide


--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
C

Charlie

yes. my code gets info from outlook. my code works fine in Office 2003, 2002,
& 2000, if the reference is changed... Sure would be nice to have it figure
out which outlook library to use on it's own...
ck
 
C

Charles Kenyon

To clarify, your code uses the Outlook Library? This is not the same as
"gets info from Outlook."
Your code will not run if you delete the reference?
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide


--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
C

Charlie

Yes, my code needs the Outlook Library. No, my code will not run if I delete
the reference.
 
C

Charles Kenyon

I had hoped for an easy answer because I don't know of anything that lets
you conditionally call on different references. There are codes for
conditional compilation. I have been hoping that someone who actually knows
vba would step in, since I'm a dabbler.

The vba help topic is "Understanding Conditional Compilation"

A property that will tell you the version (and build) is the build property.
MsgBox Application.Build will return it.

Hope this helps.

--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide


--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
C

Charles Kenyon

See also the vba Help topic "Set a Reference to a Type Library" for hints on
how you might be able to get your code to run without the reference.

Just tossing ignorance-based ideas out here.
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide


--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
H

Helmut Weber

Hi Charlie,

IMHO, one simple way would be to use early binding during
development time, so you can use intellisense,
and late binding at runtime.

With late binding you get access to whatever
_single_ Outlook version there is.

With several software-versions installed, though,
I haven't heard about a straightforward way,
to start a specific version with createobject.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

John Nurick

With several software-versions installed, though,
I haven't heard about a straightforward way,
to start a specific version with createobject.

I've never had to try it with Outlook, but feel that something like this
ought to work:

Dim oOLK As Object
Dim OLKVersion As Long

On Error Resume Next
OLKVersion = 12 'looking ahead<g>
Do
Set oOLK = CreateObject("Outlook.Application." & CStr(OLKVersion))
If oOLK Is Nothing Then
OLKVersion = OLKVersion - 1
End If
Loop While oOLK Is Nothing And OLKVersion >= 9 'Outlook 2000
On Error Goto 0
If oOLK Is Nothing Then
MsgBox "Outlook unfavourable"
Else
MsgBox "Outlook " & Cstr(OLKVersion) & " launched."
End If
 
H

Helmut Weber

Hi John,
On Error Resume Next
OLKVersion = 12 'looking ahead<g>
Do
Set oOLK = CreateObject("Outlook.Application." & CStr(OLKVersion))
If oOLK Is Nothing Then
OLKVersion = OLKVersion - 1
End If
Loop While oOLK Is Nothing And OLKVersion >= 9 'Outlook 2000

this looks like a good idea.
Thank you.

Have you ever tried it with Word or Excel?
Can't test it right now, but I'm curious.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
T

Tony Jollans

I believe that if you develop in Word 2000 with a reference to Outlook 2000
and then run on Word 2003, the reference should automatically 'upgrade' to
Word 11.0 - it just doesn't work the other way round.
 
C

Charlie

Hi, I'm not sure I understand. Do I put both, your code and John Nurick's
code together. Does this code set the reference for me? Sorry I'm not real
smart at this stuff, could you show or tell me exactly what code I paste into
my routine to set the right reference...
Many thanks,
ck
 
J

John Nurick

Hi John,


this looks like a good idea.
Thank you.

Have you ever tried it with Word or Excel?
Can't test it right now, but I'm curious.

I've never needed to do it for real, but on this machine, with Office 97
and Office 2003, I can do this in the immediate Window

Set W = CreateObject("Word.Application.11") launches Word 2003
...
Set W = CreateObject("Word.Application.9") gives error 429
...
Set W = CreateObject("Word.Application.8") launches Word 97
...
 
H

Helmut Weber

Hi Charlie,

here is an example for Excel from German MVP Thomas Gahler.
Set your reference
' ---------- ---------- ---------- ----------

#Const EARLYBINDING False 'or True
' for switching from early to late binding
'...
Private Sub procAdressListeXLEinlesen()
#If EARLYBINDING Then
Dim xlsApp As Excel.Application
Dim xlsWkb As Excel.Workbook
Dim xlsWks As Excel.Worksheet
Set xlsApp New Excel.Application
#Else
Dim xlsApp As Object
Dim xlsWkb As Object
Dim xlsWks As Object
Set xlsApp createobject("Excel.Application")
#End If

If everything is alright, remove your reference.

You may combine it with John's example.

Some experts, and I don't consider myself to be one
on this topic, recommend testing with the oldest version
of a programm, as it seems, upward compatibility
is easier achieved than downward compatibility.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

John Nurick

Hi Charlie,

I think everyone is suggesting you should use late binding so as to
avoid having to set a reference. The code I posted simply shows a way of
launching a specific version of Outlook when more than one is installed
on the computer; in practice, very few computers have more than one
version of Outlook (even if they have more than one version of Office),
and even if they do you would almost certainly want to use whichever
version is the default one.

To change from normal to late binding (and the computer's default
Outlook version), basically all you need do is

1) remove the reference to the Outlook library

2) change all declarations of Outlook object variables to As Object.
E.g.

Dim oOLKApp As Outlook.Application
Dim oFolder As Outlook.Folder

would become

Dim oOLKApp As Object 'Outlook.Application
Dim oFolder As Object 'Outlook.Folder

(I leave the original declaration types as comments to make the code
clearer.)

3) Without the reference, the Outlook constants won't be available. The
neatest thing to do is to declare the ones you use yourself, e.g.

Const olAddressEntry = 8
Const olAddressList = 7

Good luck!
 
C

Charlie

Hi, thanks, everyone for trying to help. This sounds like the simplist
answer. My code really isn't too complicated (I'm not much of a programmer),
so I'll just use someone's pc that has office 2000 and set the reference in
my templete, then it'll work on any newer versions. I use this reference to
import some of my contact names and addresses into Word. I really don't know
what early or late binding means.
Thanks,
ck
 
C

Charles Kenyon

Early binding is what you are doing now - establishing the reference before
you even start running your code. Late binding means (I intuit) establishing
the reference during your code, which means you could use conditional
formatting to parse which reference you wanted.
--
Charles Kenyon

Word New User FAQ & Web Directory: http://addbalance.com/word

Intermediate User's Guide to Microsoft Word (supplemented version of
Microsoft's Legal Users' Guide) http://addbalance.com/usersguide


--------- --------- --------- --------- --------- ---------
This message is posted to a newsgroup. Please post replies
and questions to the newsgroup so that others can learn
from my ignorance and your wisdom.
 
C

Charlie

I pasted this code in front of my code but when I remove the reference to
Outlook (early binding) I get an error as so as it gets to Dim ol As New
Outlook.Application
I think I must be getting close to figuring this out, but don't quite
understand yet.
Thanks for all your help!
ck

Dim oOLK As Object
Dim OLKVersion As Long

On Error Resume Next
OLKVersion = 12 'looking ahead<g>
Do
Set oOLK = CreateObject("Outlook.Application." & CStr(OLKVersion))
If oOLK Is Nothing Then
OLKVersion = OLKVersion - 1
End If
Loop While oOLK Is Nothing And OLKVersion >= 9 'Outlook 2000
On Error Goto 0
If oOLK Is Nothing Then
MsgBox "Outlook unfavourable"
Else
MsgBox "Outlook " & Cstr(OLKVersion) & " launched."
End If


' Set up Outlook objects.
Dim ol As New Outlook.Application
Dim olns As Outlook.Namespace
Dim cf As Outlook.MAPIFolder
Dim c As Outlook.ContactItem
Dim objItems As Outlook.Items
Dim Prop As Outlook.UserProperty

Set olns = ol.GetNamespace("MAPI")
Set cf = olns.GetDefaultFolder(olFolderContacts)
...code continues
 
T

Tony Jollans

Hi Charlie,

The Dim statement you have combines two functions:
1. It declares the variable with a type of Outlook.Application
2. It initialises it to an Outlook session (created if need be).

It is generally considered bad practice to combine these two functions in
this way, and it simply cannot be done with late binding.

So, first step is to separate the statement out into its two component
parts:

Dim ol As Outlook.Application
Set ol = New Outlook.Application

This will work fine with early binding, but when you switch to late binding,
the compiler doesn't recognise Outlook.anything any more, so you must

1. Dim ol as Object
2. Replace the Set statement with a construct which supplies the
"Outlook.Application" as a string for processing at run time (when the late
binding happens).

This you can do like this:

Dim ol As Object
Set ol = CreateObject("Outlook.Application")

You will hit the same problem with all your Outlook.xxx variable types, and
all of them must be declared as type Object. There shouldn't be any further
changes needed for them, though, as the required binding happens when you
link to the Outlook Application and all its methods and properties will work
and assign the appropriate types to each of your variables at run time.

Just glancing at your code before hitting Send I see that you already have a
CreateObject in there so the Dim ... As New was redundant anyway and all you
(probably) need to do is make all your variables Dim ... As Object.
 
C

Charlie

hi, thanks, you made things clear to me finally. But now something must
still be wrong.
Here's what I've got now. I get an error that says it can't create when it
get to the line: Set olns = CreateObject("Outlook.Namespace")
I put a ' in front of this line just out of curiosity and the same thing
happened with the next line, and the next...

Dim oOLK As Object
Dim OLKVersion As Long

On Error Resume Next
OLKVersion = 12 'looking ahead
Do
Set oOLK = CreateObject("Outlook.Application." & CStr(OLKVersion))
If oOLK Is Nothing Then
OLKVersion = OLKVersion - 1
End If
Loop While oOLK Is Nothing And OLKVersion >= 9 'Outlook 2000
On Error GoTo 0
If oOLK Is Nothing Then
MsgBox "Outlook unfavourable"
Else
MsgBox "Outlook " & CStr(OLKVersion) & " launched."
End If

Dim ol As Object
Set ol = CreateObject("Outlook.Application")
'Dim ol As New Outlook.Application

Dim olns As Object
Set olns = CreateObject("Outlook.Namespace")
'Dim olns As Outlook.Namespace

Dim cf As Object
Set cf = CreateObject("Outlook.MAPIFolder")
'Dim cf As Outlook.MAPIFolder

Dim c As Object
Set c = CreateObject("Outlook.ContactItem")
'Dim c As Outlook.ContactItem

Dim objItems As Object
Set objItems = CreateObject("Outlook.Items")
'Dim objItems As Outlook.Items

Dim Prop As Object
Set Prop = CreateObject("Outlook.UserProperty")
'Dim Prop As Outlook.UserProperty
 

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