Office Compatible 97—Getting Started

by Ross Comer
Development Lead, Microsoft Office Compatible

June 1996

Abstract

This article provides a detailed description of how to start using Office Compatible.

Description

Office Compatible is part of the Microsoft Office package. Other applications may take advantage of Office Compatible features if Microsoft Office has been installed on the computer. Applications should be written to degrade gracefully if Microsoft Office is not installed.

C Interface

To use Office Compatible, you must first locate and LoadLibrary the msoc.dll. Msoc.dll is installed as part of the Microsoft Office directory tree. The registry value HKEY_LOCAL_MACHINE\SOFTWARE\\Microsoft\\Shared Tools\\msoc.dll\Path contains the full path to the msoc.dll. Using this path, LoadLibrary can be used to load the DLL.

Once the DLL has been loaded, the address of the exported procedure CreateOfficeCompatible can be looked up and the procedure called. CreateOfficeCompatible will create a new IOfficeCompatible object which can then be used to access all other Office Compatible features.

Here is sample code that demonstrates loading the Office Compatible DLL:

IofficeCompatible vpOC;

BOOL FLoadOfficeCompatible()
{
   HKEY hkResult;
   char szDLLPath[MAX_PATH];

   vpOC = NULL;
   szDLLPath[0] = 0;

   if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, 
         "SOFTWARE\\Microsoft\\Shared Tools\\msoc.dll", 
         0, KEY_READ, &hkResult) == ERROR_SUCCESS)
   {
      DWORD cchPath = MAX_PATH;

      if (RegQueryValueEx(hkResult, (LPSTR) "Path", NULL, NULL, 
            (LPBYTE)szDLLPath, &cchPath) != ERROR_SUCCESS)
         szDLLPath[0] = 0;
      
      RegCloseKey(hkResult);
   }

   // Try to load msoc.dll from the directory specified in the registry
   if ((m_hinstOC = LoadLibrary(szDLLPath)) == NULL)
   {
      // Otherwise try to find msoc.dll on the path
      if ((m_hinstOC = LoadLibrary(MSOC97DLL)) == NULL)
         return FALSE; // failed to load library
   }

   // Declare pfnCreateOfficeCompatible to hold CreateOfficeCompatible procedure
   HRESULT (__stdcall *pfnCreateOfficeCompatible)(LPCWSTR, LPCWSTR, IOfficeCompatible**);

   pfnCreateOfficeCompatible = 
      (HRESULT (__stdcall *)(LPCWSTR, LPCWSTR, IOfficeCompatible**)) 
      GetProcAddress(m_hinstOC, "_CreateOfficeCompatible@12");

   if (pfnCreateOfficeCompatible == NULL)
      goto error;
   
   // pszAppName should be the name of the application
   // pszExeName should be name of the application executable
   if (FAILED(pfnCreateOfficeCompatible(pszAppName, pszExeName, &vpOC)))
   {
error:
      FreeLibrary(m_hinstOC);
      m_hinstOC = NULL;
      vpOC = NULL;
      return FALSE;
   }
   
   return TRUE;
}

Once you are done using the IOfficeCompatible object, it should be freed by calling the Release method. For example:

   vpOC->Release(); // Free the IOfficeCompatible object

Automation Interface

You can also access Office Compatible through automation interfaces.

As with the C model, establish the application as an Office Compatible user. Use CreateObject to return an application object, using the following call.

   Set OC = CreateObject("OfficeCompatible.Application")

Initialize the object using the following call.

   OC.Init "AppName", "Application Display Name"

Once you have called Init on the object, you can use the OC object to access all Office Compatible features.