TadxWordTaskPane toggle visibility

Add-in Express™ Support Service
That's what is more important than anything else

TadxWordTaskPane toggle visibility
 
Mark Williams




Posts: 21
Joined: 2020-06-08
I have a word task pane, which I do not want to be visible at start up of word so I have set visible to false in the pane's before show event.

I need the user to be able to toggle the visibility of the pane via a toggle button on a ribbon component.

I have managed to find examples of how to do this for excel on the forums:


 adxExcelTaskPanesManager1.Items[0].Enabled := not adxExcelTaskPanesManager1.Items[0].Enabled; 
  if (adxExcelTaskPanesManager1.Items[0].Enabled) then begin 
    if adxExcelTaskPanesManager1.Items[0].TaskPaneInstance = nil then begin 
      taskPane := adxExcelTaskPanesManager1.Items[0].CreateTaskPaneInstance; 
      if (taskPane <> nil) then begin 
        taskPane.Show; 
      end; 
    end; 
  end; 


But that doesn't work for word task panes (there is no TaskPaneInstance property not CreateTaskPaneInstance method. I have tried the following:


TaskPaneManager.Items[1].CurrentTaskPaneInstance.enabled := not Enabled;
If TaskPaneManager.Items[1].CurrentTaskPaneInstance.enabled then
 TaskPaneManager.Items[1].CurrentTaskPaneInstance.Show;


And


if not TaskPaneManager.Items[1].Enabled then
 TaskPaneManager.Items[1].Enabled := true;
TaskPaneManager.Items[1].CurrentTaskPaneInstance.Show;


Nothing seems to work!
Posted 03 Aug, 2020 09:46:37 Top
Mark Williams




Posts: 21
Joined: 2020-06-08
Have managed to work this out (I think) and thought I'd post my solution here.

In the task pane's beforeshow event set visible to true.

In the TaskPaneManager select the item and set enabled to false.

In the onClick of your toggle button:

TaskPaneManager.Items[1].Enabled := not TaskPaneManager.Items[1].Enabled; 
if TaskPaneManager.Items[1].Enabled then
  begin
    TaskPaneManager.Items[1].ShowTaskPane;
    Pane:=TaskPaneManager.Items[1].CurrentTaskPaneInstance; 
    Pane.Activate;
 end;
Posted 03 Aug, 2020 12:17:25 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
Hello Mark,

unit WordPaneVisibility_IMPL;

interface

uses
  SysUtils, ComObj, ComServ, ActiveX, Variants, Office2000, adxAddIn, WordPaneVisibility_TLB,
  System.Classes, Unit1, adxwdFormsManager, StdVcl;

type
  TcoWordPaneVisibility = class(TadxAddin, IcoWordPaneVisibility)
  end;

  TAddInModule = class(TadxCOMAddInModule)
    adxRibbonTab1: TadxRibbonTab;
    adxWordTaskPanesManager1: TadxWordTaskPanesManager;
    procedure adxRibbonTab1Controls0Controls0Click(Sender: TObject;
      const RibbonControl: IRibbonControl);
    procedure adxRibbonTab1Controls0Controls1Click(Sender: TObject;
      const RibbonControl: IRibbonControl);
  private
  protected
  public
    LetPaneShow: Boolean;
  end;

implementation

{$R *.dfm}


procedure TAddInModule.adxRibbonTab1Controls0Controls0Click(Sender: TObject;
  const RibbonControl: IRibbonControl);
var
  taskPane: TadxWordTaskPane1;
begin
  taskPane := adxWordTaskPanesManager1.Items[0].CurrentTaskPaneInstance as TadxWordTaskPane1;
  taskPane.Visible := not taskPane.Visible;
end;

procedure TAddInModule.adxRibbonTab1Controls0Controls1Click(Sender: TObject;
  const RibbonControl: IRibbonControl);
begin
  LetPaneShow := adxRibbonTab1.Controls[0].AsRibbonGroup.Controls[1].AsRibbonCheckBox.Pressed;
end;

initialization
  TadxFactory.Create(ComServer, TcoWordPaneVisibility, CLASS_coWordPaneVisibility, TAddInModule);

end.


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Word2000, adxwdFormsManager;

type
  TadxWordTaskPane1 = class(TadxWordTaskPane)
    procedure adxWordTaskPaneADXBeforeTaskPaneShow(ASender: TObject;
      Args: TadxBeforeTaskPaneShowEventArgs);
  private
    { Private declarations }
  protected
    { Protected declarations }
  public
    { Public declarations }
  published
    { Published declarations }
  end;

{NOTE: The adxWordTaskPane1 variable is intended for the exclusive use
       by the TadxWordTaskPanesCollectionItem Designer.
       NEVER use this variable for other purposes.}
var
  adxWordTaskPane1 : TadxWordTaskPane1;

implementation

uses WordPaneVisibility_IMPL;

{$R *.DFM}

procedure TadxWordTaskPane1.adxWordTaskPaneADXBeforeTaskPaneShow(
  ASender: TObject; Args: TadxBeforeTaskPaneShowEventArgs);
begin
    self.Visible := (Self.AddinModule as TAddinModule).LetPaneShow;
end;

initialization
  RegisterClass(TPersistentClass(TadxWordTaskPane1));

finalization
end.



Andrei Smolin
Add-in Express Team Leader
Posted 04 Aug, 2020 04:24:09 Top
Mark Williams




Posts: 21
Joined: 2020-06-08
Hi Andrei,

Thanks for the code sample. However, the solution I posted above seems to work without any issues. Is there a problem using the enabled property of the taskpane?
Posted 04 Aug, 2020 05:27:42 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
Hello Mark,

This is 'manipulating the visibility of a pre-created pane' vs. 'creating and destroying a pane instance'.

If the pane initializes fast, the approaches may not differ in terms of time.


Andrei Smolin
Add-in Express Team Leader
Posted 04 Aug, 2020 05:41:19 Top
Mark Williams




Posts: 21
Joined: 2020-06-08
I see. Setting enabled creates/destroys the pane.

That's okay for my purposes in this instance. The pane may not necessarily be needed to be created at all and if created it has no components and |no initialization code. It merely acts a s a container for a form created by another application.
Posted 04 Aug, 2020 06:01:50 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
No problem.


Andrei Smolin
Add-in Express Team Leader
Posted 04 Aug, 2020 07:30:18 Top