Microsoft warning about disabled Excel macros when loading Excel file in Delphi TOleContainer?

290 Views Asked by At

I am building a Delphi (Delphi 6) program using the TOleContainer component, which loads an Excel file:

MyOleContainer.CreateObjectFromFile('MyFilePath',False);

I have added MyFilePath to Excel's trusted locations and that is why I can open this Excel file with a double click in the Windows Explorer without any security warnings although the file contains macros. But CreateObjectFromFile issues Excel's scurity dialog with the message:

Microsoft Excel Security Notice

Microsoft Office has identified a potential security concern.
Microsoft has blocked macros from running because the source of this file is untrusted.
File path: Book1

What can I do? I am reading Steps to take but I feel that there is not described the scenario about loading in TOleContainer.

The Book1 text is suggesting that TOleContainer may copy the original Excel file to some temporary location. But I included the whole C: and D: drives (with subdirectories) to my trusted locations, but the security warning when loading in TOleContainer remains, while no warning occurs when opening with a double click.

Is there any way to avoid this security warning while loading with TOleContainer?

procedure TOleContainer.CreateObjectFromFile(const FileName: string;
  Iconic: Boolean);
var
  CreateInfo: TCreateInfo;
begin
  CreateInfo.CreateType := ctFromFile;
  CreateInfo.ShowAsIcon := Iconic;
  CreateInfo.IconMetaPict := 0;
  CreateInfo.FileName := FileName;
  CreateObjectFromInfo(CreateInfo);
end;

procedure TOleContainer.CreateObjectFromInfo(const CreateInfo: TCreateInfo);
begin
  DestroyObject;
  try
    CreateStorage;
    with CreateInfo do
    begin
      case CreateType of
        ctNewObject:
          OleCheck(OleCreate(ClassID, IOleObject, OLERENDER_DRAW, nil,
            Self, FStorage, FOleObject));
        ctFromFile:
          OleCheck(OleCreateFromFile(GUID_NULL, PWideChar(FileName), IOleObject,
            OLERENDER_DRAW, nil, Self, FStorage, FOleObject));
        ctLinkToFile:
          OleCheck(OleCreateLinkToFile(PWideChar(FileName), IOleObject,
            OLERENDER_DRAW, nil, Self, FStorage, FOleObject));
        ctFromData:
          OleCheck(OleCreateFromData(DataObject, IOleObject,
            OLERENDER_DRAW, nil, Self, FStorage, FOleObject));
        ctLinkFromData:
          OleCheck(OleCreateLinkFromData(DataObject, IOleObject,
            OLERENDER_DRAW, nil, Self, FStorage, FOleObject));
      end;
      FDrawAspect := DVASPECT_CONTENT;
      InitObject;
      FOleObject.SetExtent(DVASPECT_CONTENT, PixelsToHimetric(
        Point(ClientWidth, ClientHeight)));
      SetDrawAspect(ShowAsIcon, IconMetaPict);
      UpdateView;
    end;
  except
    DestroyObject;
    raise;
  end;
end;

This code shows that the Excel file is loaded via:

OleCreateFromFile(GUID_NULL, PWideChar(FileName), IOleObject, OLERENDER_DRAW, nil, Self, FStorage, FOleObject)

...using some kind of temporary storage FStorage (there is separate code for this). So, it may be possible that loading from this temporary storage has a very special handling of security warnings.

1

There are 1 best solutions below

0
user22158110 On

I have had a very similar problem with a custom written app that calls Excel via OLE and was giving me the exact same error dialog as you are quoting above.

A work around I found, on another post, is to open Excel first with a separate macro enabled spreadsheet before starting my custom app. The OLE Container created spreadsheet then activates smoothly as macros are already enabled since a macro enabled SpreadSheet is already running.

Another tip is the OLE Container worksheet initially appears both unreadable and uneditable. However, a right click in the sheet gives the choice to "Open" the worksheet. A fully useable Excel version appears for me at that point.