🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #16786

Re: Checking type in Microsoft Office Document

#16786
From: Damon Chandler (TeamB)
Date: Wed, 23 Apr 2003 19:46
112 lines
3736 bytes
Hi Sara,
> How can I know which type of Microsoft Office Document before openning ?

You might try accessing the document's Application Name property from its
Summary property set; this technique is described in MSDN Q186898...

http://support.microsoft.com/default.aspx?scid=kb%3ben-us%3b186898

Here's a rough code example; the function will return a string that contains
"Microsoft Word", "Microsoft Excel", etc...

#include <ole2.h>
AnsiString GetCmpdDocumentApp(
   WideString filename
  )
{
  // will hold the document's application name
  AnsiString AppName;

  // open the compound document
  IStorage* pStorage = NULL;
  HRESULT hRes = StgOpenStorage(
    filename.c_bstr(), NULL, STGM_READ |
    STGM_SHARE_EXCLUSIVE, NULL, 0, &pStorage
    );
  if (FAILED(hRes))
  {
    throw EWin32Error("Cannot open compound document.");
  }
  
  try // acess the document's property set
  {
    IPropertySetStorage* pPropSet;
    hRes = pStorage->QueryInterface(
      IID_IPropertySetStorage,
      reinterpret_cast<void**>(&pPropSet)
      );
    if(FAILED(hRes))
    {
      throw EWin32Error("Cannot access property set.");
    }
    
    try // access the Summary properties
    {
      IPropertyStorage* pProp;
      hRes = pPropSet->Open(
        FMTID_SummaryInformation, STGM_READ |
        STGM_SHARE_EXCLUSIVE, &pProp
        );
      if(FAILED(hRes))
      {
        throw EWin32Error("Cannot access summary properties.");
      }

      try // read the Application Name property
      {
        // specify the property to retrieve
        PROPSPEC prop_spec;
        std::memset(&prop_spec, 0, sizeof(PROPSPEC));
        prop_spec.ulKind = PRSPEC_PROPID;
        prop_spec.propid = PIDSI_APPNAME; // application name

        // read the property
        PROPVARIANT prop_var;
        hRes = pProp->ReadMultiple(1, &prop_spec, &prop_var);
        if (FAILED(hRes))
        {
          throw EWin32Error("Error reading AppName property.");
        }
        
        // copy the application name
        AppName = prop_var.pszVal;

        // clean up
        pProp->Release();
      }
      catch (...)
      {
        // clean up
        pProp->Release();
        throw;
      }
      // clean up
      pPropSet->Release();
    }
    catch (...)
    {
      // clean up
      pPropSet->Release();
      throw;
    }
    // clean up
    pStorage->Release();
  }
  catch (...)
  {
    // clean up
    pStorage->Release();
    throw;
  }

  // return the retrieved application name
  return AppName;

  // NOTE: You should consider using a smart COM-pointer class 
  // to eliminate all the exception handling in this code.
}

Good luck,
-- 
Damon (TeamB)
BCBCAQ--http://bcbcaq.bytamin-c.com
Technical Details
Message-ID: <3EA6EDA3.8EC13DD3@cornell.edu>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-08!supernews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail
References: <3ea6be1f@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,653135ee9e1a8ca8
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-04-23 12:51:49 PST
Message-ID: <3EA6EDA3.8EC13DD3@cornell.edu>
Date: Wed, 23 Apr 2003 15:46:43 -0400
From: "Damon Chandler (TeamB)" <dmc27@cornell.edu>
X-Mailer: Mozilla 4.8 [en] (Windows NT 5.0; U)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: Checking type in Microsoft Office Document
References: <3ea6be1f@newsgroups.borland.com>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 24.92.234.87
X-Trace: newsgroups.borland.com 1051127129 24.92.234.87 (23 Apr 2003 12:45:29 -0700)
Lines: 112
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-08!supernews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail