procedure TMainForm.OpenItemClick(Sender: TObject);
var
  pszFilename, pszName: PChar;
  sc: SCODE;
  snbExclude: PStr;
  hr: HResult;
  dummyExclude: IStorage;
  pIStorage: ^IStorage;
  pMyStatStg: TMyStatStg;
  dummyULI: Comp;
begin
  if OpenDialog.Execute then begin

    { Try opening a structured storage document. Filename parsing is done in OpenDialog. }
    try
      pszFilename := StrAlloc(Length(OpenDialog.Filename) + 1);  { + 1 for NULL }
      StrPCopy(pszFilename, OpenDialog.Filename);
      sc := GetSCode(StgIsStorageFile(pszFilename));
      if sc = STG_E_FILENOTFOUND then
        ShowMessage(OpenDialog.Filename + ' was not found.')
      else if sc = S_FALSE then
        ShowMessage(OpenDialog.Filename + ' is not a valid OLE2 Structured Storage file.')
      else if sc = S_OK then begin

        { Now actually open the file, and obtain a pointer to IRootStorage }
        FreeIStorageList;
        snbExclude := nil;
        pIRootStorage := nil;

        if SUCCEEDEDHR(StgOpenStorage(pszFilename, nil, STGM_DIRECT or
          STGM_READWRITE or STGM_SHARE_EXCLUSIVE, snbExclude, Longint(nil),
          IStorage(pIRootStorage))) then begin

          { Create Root of TOutline. This *ALWAYS* has an ItemIndex of 0 }
          Outline1.AddChild(0, 'Root Storage: ' + StrPas(pszFilename));

          { Code to create a list of open storages so that I can open them when
            I need to view their contents later }
          pszName := StrAlloc(100);
          StrCopy(pszName, 'Root Storage: ');
          StrCat(pszName, pszFilename);
          dummyULI := 0;
          pMyStatStg := TMyStatStg.Create(pszName, pIRootStorage, 0, dummyULI);
          m_lstIStorage.Add(pMyStatStg);
          ViewStorage(1, pIRootStorage);    { call recursive outline filler }
        end;
      end;
    finally
      StrDispose(pszFilename);
    end;
  end;

