You are on page 1of 12

Corporate Developer's Guide to Office 95 API Issues Page 1 of 12

MSDN H om e > MSDN Libra ry Archiv e >

Archived content. No warranty is made as to techni cal accuracy. Cont ent may c on tain URLs t hat were vali d when
origin ally publis hed, but now link to sit es or pages that no l on ger exist.

Corporate Developer's Guide to Office 95 API Issues


Ken Las sesen
Mi cros oft D eveloper Network Technology Group

April 28, 19 95

Ab stract
This tech nical article examines how to write Microsoft ® Office solut ion code that will s uccess fully make appl ication
programmi ng interfac e (API) calls us ing both 16 -bit and 32 -bit versi on s of Office produ cts. In partic ular, th is art icle
app lies to Microsoft Access ®, Vis ual Basi c ®, Mi cros oft Word, Microsoft Excel, and Microsoft Project. There are t hree
ways to make s uch API calls : us ing REGISTER, us ing Declar e s tatements, and usi ng a type l ibrary. Th is art icle
examines each way of making an API call , ac companied by examples . Th e reader is ass umed t o b e famili ar with my
earlier t ec hnic al artic le, "Porting Your 16 -Bit Office -Based Solution s to 32-Bit Office," and i s assumed to be an
experienced Offic e developer who needs to port or writ e s ol utions to meet th e n ew 16 -bit to 32 -bit int eroperabil ity
requiremen t.

Introduction
The c hallenge of writi ng a s olution for several generati ons of Mi cros oft ® Office produ cts across several pl atforms is
unfamiliar t o mos t compiled -language devel opers. Compiled-language applic at ion devel opers enjoy the luxury of
picking a time sl ice of a compi ler's evolu tion, encapsu lating th e s olution i nto an execu table file, and rarely h avi ng to
deal wit h issues c aused by the evol ution of a produc t. An Offi ce soluti on should have one soluti on file (an .XLS,
.MD B, .MPP, or .DOT fil e) th at wil l exec ute correctly with all Office produc ts and on all operating syst ems . Office
developers must wri te soluti ons that are backward -compatible with vers ions rel eas ed fi ve years ago and forward -

8
compatible with th e next vers ions of Office. For example, an expens e report th at runs on Mi crosoft Exc el 4 .0 and
Mi cros oft E xc el 5.0 shoul d b e abl e to run on Microsoft Excel NT, Microsoft Excel 95, and Mic rosoft Excel 2 001

/1
(ass uming there i s one).

1
Office 95 int roduc es 32 -bit Offic e products into the mains tream corp orate environment , where t hey will c oexi st with

1
16 -bit Offic e products for many years . My earl ier techn ical art icle, "Porting Your 16-Bit Office-Based Solution s to 32 -

/
Bit Office," des cri bes the problems of not bein g abl e t o c all 16 -bit APIs from a 32-bit applic ation and not bein g abl e
to call 32 -bit APIs from a 16-bit applic ation. The 16 -bit/32-bit int eroperabil ity solut ion present ed i n that art icle is
examined in d ept h in thi s arti cle.

0 4
0
Note A ru le of th umb that will con vert many API c al ls in Wi ndows ® version 3.1 t o Wi n32®
app licati on programmi ng interfac e (API) calls i s wh at I call th e Rule of 32A: "Add a 32 t o t he D LL

2
name and an A (for ANSI) t o t he fu nction n ame if a string is u sed in any of t he c all's parameters."
This rule of t humb wi ll work for the majorit y of API calls , bu t is not a true soluti on bec ause it will fail
to work for s ome fun ctions .

s
n t
The 1 6 -bit/32 -bit int eroperabil ity solut ion requi res more th an p ortin g a 16 -bit API call to it s equivalent 32 -bit API

e
call (in other words, usin g t he 32A Rule); it requi res that t he soluti on select the appropriat e API call when th e
soluti on is exec uted in a 16 -bit or a 32-bit Offic e product.

r e m
Terminology
To reduce confus ion over the word app licati on, t he foll owi ng table defines the terms used in thi s techni cal art icle.

ui
Ter m
Office produ ct
Definition
A prod uct that does n ot compile code to an exec utable. One of the following products :

eq Mi cros oft Projec t, Micros oft Acc ess, Word for W indows, Micros oft Excel.

Rhttp://msdn.microsoft.com/archive/en-us/dnaroffdev/html/msdn_off95api.asp?frame=true 10/20/2007
Corporate Developer's Guide to Office 95 API Issues Page 2 of 12

Compil ed l anguage A prod uct that c omp iles code to creat e an executabl e, for example, Visual Basic® ,
FORTRAN PowerStati on ™, Visu al FoxPro® , an d Vis ual C++™.
Solut ion An appli cation developed by a t hird p arty or a developer using an Offi ce produc t and in
its file format, s uch as Microsoft Excel (.XLS), Word (.DOT), Mic rosoft Project (.MPP), or
Mi cros oft Acces s (.MD B).
Solut ion code The c ode (XLM or Basic or both) in which th e s ol ution i s written.
Pl atforms Mi cros oft ® Windows and Appl e® Mac intosh ® op erating s ys tems. Some Offic e products
exis t for all vers ions of Windows and Macint osh operatin g systems. Microsoft Excel can
execut e t he s ame appl ications (with s ome restriction s) on bot h a Maci ntosh and a
Windows PC by copyin g t he soluti on to the other operat ing syst em.

API-Calling Method s
Solut ion code can be writt en i n one of th ree ways —using Mic rosoft Excel macros (XLM), Basic (Access Basic, Visual
Basic , Vis ual Basi c for Applicat ions, and Word Basi c), or a c omb ination of XLM and Bas ic. XLM uses the REGISTER
an d the CALL c ommand s to make API calls whi le Basi c uses a Declare s tatement, a type l ibrary, or both to make
API c al ls. A Mi crosoft Exc el soluti on that has been passed from developer t o developer may use all th ese ways of
makin g API c al ls. All three ways wi ll work in the lat es t version of Microsoft Excel.

Table 1. API-Calling Methods Described

Method Language Technique for Making API Calls


REGISTER XLM macros Us es the REGISTER and CALL fun ctions from mac ros .
Declare Acc es s Bas ic, Vi sual Basic, Visual Basic Us es a Declare s tatement.
for Appl ication s, WordBasic
Type li brary Vis ual Basi c for Applicat ions only Select t ype li brary method from t he Object Browser.

These met hods are not product vers ion -specific bu t are dependent on developers' c ode-writing p references or the
range of produc ts on wh ich th e sol ution mus t run. For example, a developer may wri te an s olution i n XLM for
Mi cros oft E xc el 95 because the sol ution must al so work wit h Micros oft Excel 4.0. Table 2 shows which API-callin g
meth ods work in which Mic rosoft products .

Table 2. API-Calling Methods for Microsoft Products

Pr oduct
Mi cros oft E xc el
Version
3.0, 4.0
REGISTER Method

X
Declare Method Type Library Method

/1 8
Mi cros oft E xc el
Word
Vis ual Basi c
5.0, 5.0 NT, 95
2.0, 6.0, 95
1.0, 2.0, 3.0
X X
X
X
X

/ 11
Vis ual Basi c
Project
Mi cros oft Acces s
4.0
4.0, 95
1.0, 1.1, 2.0

0
X
X
X
X

4
Mi cros oft Acces s
FoxPro
FoxPro
95
2.5
3.0

20 X
X
X
X

t s
As we look at each meth od of making 16 -bit and 32 -bit API calls , we wi ll examine the same simple API call:
GetTickCount.

n
e
REGISTER Method
API c al ls usi ng the REGISTER meth od are unfamiliar t o d evelopers wh o s tart ed wri ting Offi ce soluti ons after they

m
start ed t o c ode i n Vis ual Bas ic. The older generations of Microsoft Excel developers make API calls from XLM and

e
avoid u sing Visual Basic for Appl ications . XLM s hould not be viewed as ol d technology —even thou gh it has not been

ir
changed sinc e Mi crosoft E xc el 4.0. It may be the best choic e i f performance is a criti cal iss ue. Th e Mic rosoft Excel
5.0 Developer's Kit s tates one advan tage of making an API call di rectl y from XLM:

u
q
Because th e C API is optimi zed for use from the Mic rosoft Excel macro language and the worksheet, it
is not a very good mec hanism for wri ting extern al fu nction s to be u sed by Visu al Basi c (al though

R e
http://msdn.microsoft.com/archive/en-us/dnaroffdev/html/msdn_off95api.asp?frame=true 10/20/2007
Corporate Developer's Guide to Office 95 API Issues Page 3 of 12

Vis ual Basi c and the C API can be combi ned into hybrid solut ions).

To make an API c all from XLM requi res the us e of the REGISTER fun ction to reference the dynamic -link library
(DLL) and the CALL fun ction to execute it . Th e REGISTER fun ction performs the equivalent of a Declare s tatement
in Vi sual Bas ic for Applic ations. The rec ommend ed REGISTER s yn tax i s:

REGISTER(module_text,procedure,type_text,function_text,argument_text,macro_type,category,shortcut_text)

To return to our sample API call , we can implement GetTickCount i n a few lines of XLM code.

This code produces the following out put in 16 -bit Microsoft Excel:

The c ode fail s if we run it with 32 -bit Microsoft Excel. The API call ret urns #VALUE!, in dicatin g a fail ure to pass
valu es to or from the DLL:

We mus t convert i t to a 3 2-bit API call, so we use the 32A Rul e of addin g a 32 t o t he D LL name and an A i f i t's a
stri ng. Code th at implements th e 32 A Rule is sh own below.

/1 8
/ 11
0 4
When we call TestGetTickCount32A, we receive a different error. The #NAME ? value is ret urned, meaning t he
fu nction does n ot exis t in the DLL. It's eit her the wrong D LL or the wrong funct ion name. (Rememb er, functi on

0
names are case -sensiti ve.) This mu st be one of th e excepti ons to the 32A Ru le. (I c onfess, I pic ked t his funct ion on
purpose.)

s 2
n t
In the MSD N Li brary, look up GetTickCount i n the Platform SDK u sing th e Keyword Index. The Qui ck Info jump at
the top of t he t opic tell s you the fu nction i s in the KERNEL3 2 library. (It was in the USE R li brary i n Windows 3. x.)
The c ode bel ow shows th e c orrec ted mac ro for 32 -bit Microsoft Excel.

me
ir e
q u
R e
http://msdn.microsoft.com/archive/en-us/dnaroffdev/html/msdn_off95api.asp?frame=true 10/20/2007
Corporate Developer's Guide to Office 95 API Issues Page 4 of 12

When this c ode i s run with 3 2 -bit Microsoft Excel, we obt ai n the correct results :

To i llust rate the way API call s behave with 16 -bit and 32 -bit Microsoft Excel, exami ne Tab le 3, whi ch shows the
result s of t hese mac ros. The macro that uses th e 3 2A Rule ("3 2A bit s") produces th e s ame res ult in bot h 16 -bit and
32 -bit solut ions. Thi s illu strat es the abilit y of the REGISTER fun ction to loc ate (or fail to locate) the functi on name
in both 16 -bit and 32 -bit DLLs from ei ther 16 -bit and 32 -bit solut ions. The ret urn of #VALUE! i ndicat es p roblems in
pas sing or receiving p arameters.

Table 3. Results of Making Calls Acr oss 16 -Bit and 32-Bit Layers fr om Microsoft Excel

Microsoft Excel 4.0 (16-bit v ersion) Microsoft Excel 95 (32-bit v ersion)

We c an write macros t hat make 16 -bit API calls or 3 2-bit API calls —but we mu st write macros t hat can make 16 -bit
API c al ls an d 3 2-bit API calls , dependi ng on the versi on of th e Offi ce produc t. Solu tion code must run on both 1 6-bit
Mi cros oft E xc el an d 32 -bit Microsoft Excel. The Vi sual Basic for Appli cations fun ction Engine32 , des crib ed i n my
earlier articl e ( "Porting Your 16-Bit Office-Based Solution s to 32 -Bit Office") does not work with Mic rosoft Excel 4.0 .
(Mic rosoft Excel 4.0 d oes n ot incl ude Vis ual Basi c for Applicat ions.) Engine32 does work for Mic rosoft Excel 5 .0 or
higher. A Microsoft Excel 4.0 -compatible Engine32 fun ction mus t use macro code.

Engine32 Function for XL M


For XLM, t he s olution i s simil ar to the Visual Basic for Appli cations s olution . Th e fun ction Info("osv ersion") wil l
contain 32 i f Mic rosoft Excel i s a 32 -bit versi on . T he Engine32 macro shown below returns TRUE if Mi cros oft E xc el is
a 32 -bit versi on , FALSE if Microsoft Excel is a 16 -bit versi on .

/1 8
/ 11
Sample REGISTER-method solution
With Engine32 defin ed, t he macro code for GetTickCount i s simple:

0 4
20
n t s
me
Engine32 wil l work c orrec tly wi th all vers ions of Micros oft Excel. The GetTickCount fun ction performs the same as
the API c all did in Wi ndows 3.1.

ir e Note If you are concern ed abou t performanc e, you shou ld regis ter all th e API cal ls when you l oad

u
the soluti on.

eq
Steps of the REGISTER-method solution

Rhttp://msdn.microsoft.com/archive/en-us/dnaroffdev/html/msdn_off95api.asp?frame=true 10/20/2007
Corporate Developer's Guide to Office 95 API Issues Page 5 of 12

If you are converti ng Mi cros oft Excel solut ions to ru n on both 16 -bit and 32-bit produ cts, I s uggest th e foll owin g
steps:

1. Creat e a new mac ro sheet cal led APICAL LS.

2. Creat e t he Engine32 fun ction in t he APICAL LS mac ro sheet.

3. Locate all t he REGISTER fun ctions i n the solut ion and move th em to APICAL LS, one per c olumn.

4. Us ing the macro code above in th e s ampl e REGISTER s olution as a templat e, create functi on s for each API on
your macro sh eets .

 Add the needed ARGUMENT l ines.

 Add the needed RESULT l ines.

 Add the 16 -bit API REGISTER l ine, appending 16 t o t he fu nction t ext.

 Add the 32 -bit API REGISTER l ine, appending 32 t o t he fu nction t ext.

 Do an y needed data manipul ation to make t he API c al l.

 Add an If l ine to call th e appropri at e API.

 RETURN t he return val ue (if any).

5. Tes t the functi on.

6. Define the functi on to Mi cros oft Exc el.

This process allows existi ng API calls i n mac ro sheets t o be left un touched. On ce you h ave creat ed t he mac ro sheet
APICAL LS (and test ed i t), you may import i t into other soluti ons and reus e t he mac ros, thu s cutt ing convers ion
time. The c ontent of this mac ro sheet is s olution -independent (it cont ains only Win dows API cal ls ) an d reusable in
ot her solu tions. Thi s macro sh eet APICAL LS bec omes a Rosetta st on e for fu ture 16 -bit/32-bit solut ion
development .

Declare Me thod
The i ntrod uction of Visual Basic for Appli cations g ives Mi cros oft Exc el devel op ers an alternative to XLM. Vis ual Basi c,
WordBasic, and Microsoft Access developers are able to also write Mic rosoft Excel and Mic rosoft Project soluti ons
wit h litt le difficult y. Basic c ode c an be exchang ed among a wide variety of produc ts. The younger generat ion of

/1 8
Office devel opers will write code in Visual Basic for Appli cations an d rarely us e XLM.

/ 11
To c al l an API from Vis ual Bas ic for Applic at ions requ ires an API Declare s tatement. A decl ared API is call ab le
an ywhere i n Basic code (or in a mac ro). There are two met hods of doing Declare s tatements, as shown below.

Word Declare statements

0 4
'Word was first in doing Declare statements.
'Its format was frozen for backward compatability.
Declare Sub SubName Lib LibName$ [(ArgumentList)] [ Alias Routine$]

20
s
Declare Function FunctionName[$] Lib LibName$ [( ArgumentList)] [Alias Routine$] As Type

t
Visual Basic for Applications, Basic, and Microsoft Access Declare statements

n
e
Declare Sub globalname Lib "libname" [Alias "aliasname" ][([ argumentlist])]
Declare Function globalname Lib libname [Alias aliasname ] [([ argumentlist])] [As type]

r e m
Because I have alread y di scuss ed some of the is sues in "Porting Your 16-Bit Office -Based Solution s to 32 -Bit Office"
an d derived a met hodology above, I wil l cut to a sample funct ion and then show the st eps of th e Declare s olution .

ui
Basic Engine32 function

q
The Engine32 fun ction return s True i f 3 2-bit API calls wil l work (and 16 -bit API calls wil l fai l), and i t returns False
if 1 6 -bit API calls wil l work (and 32 -bit API calls wil l fai l). Th e Engine32 fu nctions given in "Porting Your 16-Bit

R e
http://msdn.microsoft.com/archive/en-us/dnaroffdev/html/msdn_off95api.asp?frame=true 10/20/2007
Corporate Developer's Guide to Office 95 API Issues Page 6 of 12

Office -Based Solution s to 32 -Bit Office" were desi gned to show h ow they differ i n eac h Office produ ct. The version s
given bel ow are modifi ed for bet ter performance and to work wit h more version s.

Performance was improved by init ializi ng stati c variables on t he fi rst fun ction cal l. All subsequent c alls us e t he s tatic
variables in stead of repeating addit ional funct ion calls . Anoth er met hod for improvi ng performanc e i s to init ialize a
Global or Public variable; h owever, thi s has the drawback that i f a Reset oc curs in some produc ts, all su bsequent
API c al ls may fai l until t he Global variable is rei niti al ized.

Microsoft Excel 5 or higher and Project 4 or higher

The Application .OperatingSystem propert y in Mi crosoft E xc el an d Mi cros oft Projec t always c ontains 32 i f t he
product is a 32 -bit produ ct. The produ ct versi on number is n ot sufficient bec ause Microsoft Excel 5.0 is availabl e as
both a 16 -bit and a 32 -bit produ ct.

Function Engine32%()
Static sEngine32%,SEval% 'Statics are used to improve performance.
If SEval% Then Engine32%=sEngine32%: Exit Function
If instr(Application.OperatingSystem,"32") then sEngine32%=True
Seval%=True
Engine32%=sEngine32%
End Function

Microsoft Access 1.1 or higher

Mi cros oft Acces s has no produ ct versi on that is both 1 6 -bit and 32 -bit. Checking the versi on number by call ing
SysCmd det ermines whic h version of Microsoft Ac cess is b ei ng used. Microsoft Ac cess 1.1 does not h ave a version
number cons tant buil t in, so we al ways use 7 to ens ure that th e c ode will work with Mi cros oft Acces s 1.1. This
meth od should be used to s ee whether your soluti on code should make a 1 6 -bit or 32 -bit API call.

Function Engine32% ()
Static sEngine32%,SEval%
If SEval% Then Engine32%=sEngine32%: Exit Function
If SysCmd(7) > 2 Then sEngine32% = True

8
Seval%=True

/1
End Function

Word for Windows 2.0 or higher

11
Word must evaluate th e Engine32 fun ction each ti me bec au se Word does not su pport st atic vari ab les. Fi rst we

/
check to see if the produc t version nu mber is hi gh enough to indi cate it may be a 32 -bit versi on , an d t hen we ch ec k

4
the version of the operati ng system to see wh ether it i s a 32 -bit one. This two -stage proces s is needed because
GetSystemInfo i s not available on versi on s before Word 6.0, and Word 6.0 is availabl e i n both 16 -bit and 32 -bit
versions.

00
2
Function Engine32
Engine32 = 0
If Val(AppInfo$(2)) > 5 Then

s
OS$ = GetSystemInfo$(23)

t
If Val(OS$) > 6.3 Or Len(OS$) = 0 Then Engine32 = - 1

n
End If
End Function

me
Visual Basic

Alt hough Vi sual Basic does not u se s oluti on c ode, Basic code is often exc hanged wi th the products c overed above.

i e
Vis ual Basi c 4.0 does not have the Application.OperatingSystem propert y (thi s is not tech nicall y a part of Visu al

r
Basic for Appl icati on s), bu t uses cond itional c omp ilation with #IF and #ELSE. If you int en d to share your c ode with
ot her Microsoft prod ucts, you shou ld create the foll owin g fun ction (and not us e c ondition al compilati on el sewh ere):

q u
Function Engine32%()

e
'This is for VB4 only.

Rhttp://msdn.microsoft.com/archive/en-us/dnaroffdev/html/msdn_off95api.asp?frame=true 10/20/2007
Corporate Developer's Guide to Office 95 API Issues Page 7 of 12

#IF WIN16
Engine32% = False
#ELSE
Engine32% = True
#ENDIF
End Function

For earlier versions of Vi sual Basic, us e t he foll owi ng:

Function Engine32%()
'This is for VB1 - VB3; #IF is not supported.
Engine32% = False
End Function

Sample Declare-method solution


The foll owi ng code demon strat es the Declare-meth od soluti on, except for Word:

Declare Function GetTickCount32 Lib "Kernel32" Alias "GetTickCount" () As Long


Declare Function GetTickCount16 Lib "USER" Alias "GetTickCount" () As Long

Function GetTickCount() As Long


If Engine32() Then
GetTickCount = GetTickCount32()
Else
GetTickCount = GetTickCount16()
End If
End Function

The Engine32 fun ction is used to determin e whic h API c all to make. The Declare s tatements ind icate the actual
API func tion name as an Alias t o avoid acc idental ch an ges of c as e (32 -bit API calls are case -sensiti ve) and then
indicat e th e bi tness of the API functi on by add ing 16 or 32 t o t he end of the func tion.

This code can be copi ed and pas ted in all Offic e product s exc ep t Word. WordBasi c exi sted before the Bas ic used in

8
the other Offic e p roduct s and is different. We will examine the Word soluti on later.

Steps of the Declar e-method solution

1
If you are converti ng soluti ons to ru n on 16 -bit and 32 -bit produ cts, I s uggest th e foll owin g steps:
/1
1.

2.
3.
Creat e a new module called APICalls.

Creat e t he Engine32 fun ction in APICalls.


Locate all t he Declare fun ctions i n the solut ion and move th em to APICalls .

4/ 1
4.

a.

00
Us ing the code in t he "Sample Declare-meth od soluti on" above as a template, create funct ions for each API.

The arguments s hould match th e Wi ndows version 3.1 API cal ls.

b.

2
The result s should be Win 32 API call res ults . (Visual Basic wil l automatical ly c onvert to Win dows vers ion
3.1 if needed .)

s
c.
d.

n t
Add the 16 -bit API Declare l ine in the Decl aration s; append 1 6 to the functi on name .
Add the 32 -bit API Declare l ine in the Decl aration s; append 3 2 to the functi on name.

e.

f.

g.

me Do an y needed data manipul ation.

Add an If li ne to call the appropriat e API.

Return the ret urn value (if any).

5.

ir e
Tes t the functi on.

u
This process allows existi ng calls in other modul es to be l eft u ntouch ed. Once the developer defines and tes ts the
APICalls modu le, she or he may i mport and reu se the module in other soluti ons, thus c uttin g conversi on time. The

eq
content of this mod ule is solu tion -independent and the developer may use the module again. Thi s module,

Rhttp://msdn.microsoft.com/archive/en-us/dnaroffdev/html/msdn_off95api.asp?frame=true 10/20/2007
Corporate Developer's Guide to Office 95 API Issues Page 8 of 12

APICalls , bec omes a component for future 16 -bit/32-bit solut ion development in Visual Basic for Appli cations .

API-wr apper alternative solution

I usuall y p ut a wrapper aroun d API c al ls ins tead of exposin g the API c all in th e cod e. For example, I will wrap cod e
around GetProfileString t o c reate a fu nction c alled vbGetWinIni t hat takes the same arguments bu t returns th e
stri ng inst ead.

If you c ode i n this s tyle, you may wish to modify the API -wrapper funct ion to call th e appropri ate API in stead of
creatin g ad ditional fun ctions .

Word sample Declare-method solution


Word has a different Declare format an d syntax. The W ord solut ion is more c omp lex becau se you c an not place both
the 16 -bit and 32 -bit Declare s tatements in t he s ame macro. The s olution i s to creat e th ree mac ro lib rari es:
APICAL L16 and APICAL L32, t hat contain t he Declare s tatements for each operat ing environmen t, and a 1 6-
bit/32 -bit int eroperabil ity macro, APICAL LS. Th is may s ound very confusi ng, so let us go through it s tep by s tep.

First, we create a macro li brary cal led APICAL L16. Th is macro con tains all t he 16 -bit API Declare s tatements.

'This is APICALL16 -- all 16-bit Declare statements are placed here.


Declare Function GetTickCount16 Lib "USER" Alias "GetTickCount"() As Long
Function GetTickCount
GetTickCount = GetTickCount16
End Function

Second, we create a macro li brary cal led APICAL L32. Th is macro con tains all t he 32 -bit API Declare s tatements.

'This is APICALL32 -- all 32-bit Declare statements are placed here.


Declare Function GetTickCount32 Lib "KERNEL32"() Alias "GetTickCount" As Long
Function GetTickCount
GetTickCount = GetTickCount32
End Function

Third, we create a macro li brary c al led APICAL LS. Th is macro con tains Engine32 and t he p rocedures your solu tion
code wil l call.

/1 8
'This is APICALLS -- no Declare statements may be in this macro.
Function Engine32
Engine32 = 0
If Val(AppInfo$(2)) > 5 Then

/ 11
OS$ = GetSystemInfo$(23)
If Val(OS$) > 6.3 Or Len(OS$) = 0 Then Engine32 = - 1
End If

0 4
0
End Function

2
Function GetTickCount
If Engine32 Then

s
GetTickCount = APICall32.GetTickCount

t
Else
GetTickCount = APICall16.GetTickCount

n
End If
End Function

e
'Other API function calls are placed here.

m
You c an now c al l this fu nction from your solut ion code. You must prefac e your calls with APICAL LS, for example:

ir e
Sub MAIN
MsgBox Str$(APICalls.GetTickCount)
End Sub

q u
Steps of the Word Declare-method solution

R e
http://msdn.microsoft.com/archive/en-us/dnaroffdev/html/msdn_off95api.asp?frame=true 10/20/2007
Corporate Developer's Guide to Office 95 API Issues Page 9 of 12

If you are converti ng Word solu tions t o run on 16 -bit and 32 -bit produ cts, I s uggest th e foll owin g steps:

1. Creat e a new module called APICAL LS.

2. Creat e t he Engine32 fun ction in APICAL LS.

3. Creat e a new module called APICAL L16.

4. Locate all t he 1 6-bit Declare s tatements in t he s olution and move them to APICAL L16.

5. Creat e a new module called APICAL L32.

6. Creat e t he equ ivalent 32 -bit Declare s tatements and put t hem t o APICAL L32.

7. Us ing the templat e above, create func tions for each API in each of th e th ree macro li brari es.

8. Add APICAL LS before all calls to t he API in your solut ion code.

9. Tes t eac h functi on.

This process allows existi ng calls in other modul es to be l eft u ntouch ed. Once the developer defines and tes ts these
mac ros, she or h e may add them to the NORMAL.DOT template and reus e th e macros in oth er solut ions so as to cu t
conversion t ime.

Type Library Method


The t yp e li brary method of making API calls is new t o mos t devel op ers. Bruce McKinn ey's forthcomi ng Mi cros oft
Press ® book, Hard Core Vi sual Bas ic, i ncludes t he Windows API Fun ctions t ype l ibrary for 16 -bit API calls
(WIN16.TLB) and a mat ching t ype li brary for 3 2 -bit API calls (WIN32.TLB). Onc e th es e t yp e l ibraries are regist ered,
the ap propriate type library is loaded for the 16 -bit or 32 -bit versi on of th e Offi ce produc t.

A type library provid es easy acc es s to the API calls wit h the produc ts Microsoft Excel 5.0 or higher, Mic rosoft Projec t
4.0 or high er, Vi sual Bas ic 4.0 or higher, and Mic rosoft Access 95 or hig her. Al l the Windows API call s become n at ive
fu nctions . Becaus e th is approac h is new to man y of my readers, I wil l go throu gh the steps i n detail.

Registering the type library


The foll owi ng steps will add a type library call ed Wi ndows API Func tions t o your regis try. This makes thi s type library
avail ab le for al l product s that use Visual Basic for Appl ication s, not only the produ ct in whic h you registered it.

1. Open any Vis ual Bas ic for Applic at ions product (for example, Mi cros oft E xc el 5.0).

/1 8
2.

3.
To c reate a modu le, choose Macro Module from th e Insert men u.

/ 1
Choose References from th e Tool s men u. The References di alog box i s displ ayed as in Figu re 1.
1
0 4
20
n t s
me
ir e
u
Figure 1. The References dialog box

eq
4. Ass uming that you have not regi stered th e Wi ndows API Func tions type li brary, cl ick the Browse button and

Rhttp://msdn.microsoft.com/archive/en-us/dnaroffdev/html/msdn_off95api.asp?frame=true 10/20/2007
Corporate Developer's Guide to Office 95 API Issues Page 10 of 12

locate WIN3 2.TLB, and clic k OK. Repeat wi th WIN1 6.TLB. Both W IN16.TLB and WIN32.TLB will be regist ered.
See Fi gure 2.

Figure 2. Selecting the type library

5. After t he Browse dial og c loses, sc roll t o t he b ot tom of t he References di alog box, and you will s ee th e Wi ndows
API Func tions type li brary in t he l ist (Figure 3 ).

/1 8
/ 11
0 4
6.
Figure 3. Registering Windows API F unctions

Clos e t he References di alog box.

20
7.

8.
From t he View men u, choose Object Browser.

n t s
In the Ob ject Browser dialog box, select Wi n (Wi ndows API Func tions type li brary) in the Libraries/Workbooks
drop -down li st box. Al l the avai lable functi ons wi ll appear in t he Objects/Modules and Meth od s/Properti es list

e
boxes below (Fi gure 4).

r e m
ui
eq
Rhttp://msdn.microsoft.com/archive/en-us/dnaroffdev/html/msdn_off95api.asp?frame=true 10/20/2007
Corporate Developer's Guide to Office 95 API Issues Page 11 of 12

Figure 4. Microsoft Excel 5.0 Object Browser showing objects and methods available in Bruce
McKinney's Windows API Functions type library

9. In the Ob ject s/Mod ules lis t box, s el ect Kernel; then in th e Methods /Properti es list box, select GetTickCount.

10. Cli ck the Paste button. G et TickCount appears i n the Module.

The foll owi ng shows the code to disp lay t he valu e return ed b y th e GetTickCount API cal l in a mes sage box.

Sub Demo()
MsgBox Str$(GetTickCount)

/1 8
1
End Sub

No REGISTER comman d nor Declare s tatements are needed. The code above is al l the code you need.

Steps of the type libr ary-method solution

4/ 1
Windows API Fun ctions t ype l ibrary i s regis tered:

00
If you are converti ng soluti ons to ru n on 16 -bit and 32 -bit produ cts, I s uggest th e foll owin g steps —as suming t he

2
1. Remove al l you r Declare s tatements, assu mi ng you are usin g on ly standard 1 6-bit API calls .
2. Check th e Wi ndows API Func tions ch ec k box in t he References d ialog box.

That is all. You have done a c omplete porti ng.

n t s
e
Type library issues
Type li braries are a develop ing technol ogy—there are very few type lib rari es commercial ly avail able. Bruce did an

m
excel lent job in designi ng the type library to work wi th both 16 -bit and 32 -bit produ cts us ing the Win dows 3.1 API
call names. Nonetheles s, type l ibraries have some i ssues t hat should be revi ewed:

ir e
 The Wi ndows API Func tions t ype li brary adds more than a thousand new reserved words to Vi sual Bas ic for
Appli cations . All t he API c al ls cont ai ned in the type library bec ome reserved words in t he l anguage. If you have

q u an exis ting func tion call ed ordShell, you must change its n ame so that i t does not confli ct with the ordShell
fu nction already d efi ned i n the type library.

R e
http://msdn.microsoft.com/archive/en-us/dnaroffdev/html/msdn_off95api.asp?frame=true 10/20/2007
Corporate Developer's Guide to Office 95 API Issues Page 12 of 12

 The Wi ndows API Func tions t ype li brary does not i nclu de API call s that require a user-defi ned t ype (UDT). This
may chang e i n fu ture vers ions.

 The Wi ndows API Func tions t ype li brary methods are not available on spread sheets in Mic rosoft Excel.

This tech nology is very promis ing and will s implify the use of API c alls in d evelopi ng Office solu tions.

Manage Your Profile | Legal | Contact Us | MSDN Flash New sletter

© 2007 M icrosoft Corpora tion. All rights reserv ed. Terms of Use | Tra demark s | Priva cy Statem ent

/1 8
/ 11
0 4
20
n t s
me
ir e
q u
R e
http://msdn.microsoft.com/archive/en-us/dnaroffdev/html/msdn_off95api.asp?frame=true 10/20/2007

You might also like