Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
554 views
in Technique[技术] by (71.8m points)

delphi - How do I add a menu button to my OLE Contained Office application?

I am using an OLE-Container for my embedded word and excel applications. Im trying to create a custom "File" Button as you'd know it from the Standard word file, except that it has my own code in it. I'm doing this because my customers are more used to saving a document under the "File" tab while still having the benefits of controlled access and its something I would really need to stay there.

I have searched online for various ways of doing this, but I haven't really found much about it.

From what I scratched together, I have these 2 functions:

procedure TForm1.Button1Click(Sender: TObject);
begin
     FormatMenuBar(OleContainer1.OleObject);
end;

procedure TForm1.FormatMenuBar(ADocument: OleVariant);
var
    CBar: CommandBar;
   MenuItem: OLEVariant;
begin
      CBar := ADocument.CommandBars['File'];
      MenuItem := CBar.Controls.Add(msoControlButton, EmptyParam, EmptyParam, EmptyParam, True) as CommandBarButton;
      MenuItem.Caption := 'File';
      MenuItem.DescriptionText := 'Test123';
      MenuItem.OnAction := 'VBMacroName';
end;

Unfortunately, this doesn't work nor does it even compile, shooting the error message:

"Incompatible Types Commandbar and Variant"

at CBar := ADocument.CommandBars['File'];

I haven't found much documentation on the Properties on the OLEVariants, or rather about how they're implementing office features. The same line of code should work with Word.Commandsbars, but I haven't tested that yet.

I tried typecasting the Variant to a Commandbar, but that didn't work either. How do I fix this, or what is a different way to achieve what I need?

NOTE: Since I'm using variant I can't see the properties or methods during development time. I may very well just have expression errors.

question from:https://stackoverflow.com/questions/65939714/how-do-i-add-a-menu-button-to-my-ole-contained-office-application

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...