ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Toggle Design Mode in Excel by AddIn (https://www.excelbanter.com/excel-programming/401414-toggle-design-mode-excel-addin.html)

[email protected]

Toggle Design Mode in Excel by AddIn
 
How could AddIn toggle Design mode in Excel? I'm talking about some
functionality like Workbook.ToggleFormsDesignMode for Excel 2007. How
could it possible for older versions?

Thank you in advance.

[email protected]

Toggle Design Mode in Excel by AddIn
 
On 20 ÎÏÑÂ, 15:04, wrote:
How could AddIn toggle Design mode in Excel? I'm talking about some
functionality like Workbook.ToggleFormsDesignMode for Excel 2007. How
could it possible for older versions?

Thank you in advance.


So, we've found it out. There is an example of code in C++ for our
AddIn. It works fine for Excel 2003 and 2007:


Excel::_ApplicationPtr pApp = NULL;

pApp = m_pParentApp;

if (pApp == NULL)
{
MessageBox(NULL, "Can't get Excel::_Application interface", "Test
Addin", MB_SETFOREGROUND);
}
else
{
try
{
Excel::_WorkbookPtr wb_ptr;

wb_ptr = pApp-GetActiveWorkbook();

wb_ptr-ToggleFormsDesign();
}
catch (_com_error err)
{
MessageBoxW(NULL, err.Description().GetBSTR(), L"Test Addin",
MB_SETFOREGROUND);
}
}



Thanks for attention, we hope it'll be useful for community.

Chip Pearson

Toggle Design Mode in Excel by AddIn
 
It is very simple in VBA. The following procs will turn design mode on or
off.

Sub TurnOffDesignMode()
Const DESIGN_MODE_ID As Long = 1605&
Dim Ctrl As Office.CommandBarButton
Set Ctrl = Application.CommandBars.FindControl(ID:=DESIGN_MOD E_ID)
With Ctrl
If .State = msoButtonDown Then
.Execute
End If
End With
End Sub

Sub TurnOnDesignMode()
Const DESIGN_MODE_ID As Long = 1605&
Dim Ctrl As Office.CommandBarButton
Set Ctrl = Application.CommandBars.FindControl(ID:=DESIGN_MOD E_ID)
With Ctrl
If .State = msoButtonUp Then
.Execute
End If
End With
End Sub


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)


wrote in message
...
On 20 ÎÏÑÂ, 15:04, wrote:
How could AddIn toggle Design mode in Excel? I'm talking about some
functionality like Workbook.ToggleFormsDesignMode for Excel 2007. How
could it possible for older versions?

Thank you in advance.


So, we've found it out. There is an example of code in C++ for our
AddIn. It works fine for Excel 2003 and 2007:


Excel::_ApplicationPtr pApp = NULL;

pApp = m_pParentApp;

if (pApp == NULL)
{
MessageBox(NULL, "Can't get Excel::_Application interface", "Test
Addin", MB_SETFOREGROUND);
}
else
{
try
{
Excel::_WorkbookPtr wb_ptr;

wb_ptr = pApp-GetActiveWorkbook();

wb_ptr-ToggleFormsDesign();
}
catch (_com_error err)
{
MessageBoxW(NULL, err.Description().GetBSTR(), L"Test Addin",
MB_SETFOREGROUND);
}
}



Thanks for attention, we hope it'll be useful for community.


[email protected]

Toggle Design Mode in Excel by AddIn
 
On 28 ÎÏÑÂ, 03:46, "Chip Pearson" wrote:
It is very simple in VBA. The following procs will turn design mode on or
off.


Thanks a lot for your participation!

Yes, we've seen the similar solution in C++


//Application.CommandBars.FindControl(ID:=1605).Exec ute

_variant_t vtOptional(DISP_E_PARAMNOTFOUND, VT_ERROR);

_CommandBars oBars(oApp.GetCommandBars());

CommandBarControl oCtrl(

oBars.FindControl(

vtOptional,

_variant_t(1605L),

vtOptional,

vtOptional));

if (oCtrl)

{

oCtrl.Execute();

}

It isn't real good, IMHO. There are two caveats at least, I think.

The first caveat is connected to "magic numbers" i.e. button's ID.
Did anybody from M$ promise to keep it unchanged forever? ;)

The second one is about ideology of emulation of button click. If we
use it from the macro, which was built in particular workbook, it
works in properly way. But our AddIn could be used without any
relation to the active now workbook (yes, in our example we used the
active workbook, but it was done only for clarification of code).


And one more question for you about Design Mode detection. I
understand, it`s meaningless question for VBA, but, for our case it is
possible.
So, in our AddIn we want to control state of particular workbook - is
it in Design Mode? We've written this code:

Excel::_WorkbookPtr wb_ptr;
......

VBIDE::_VBProjectPtr ptrVBProj;
// ptrVBProj = wb_ptr-GetVBProject();
hr = wb_ptr-get_VBProject(&ptrVBProj);

if(SUCCEEDED(hr))
{
VBIDE::vbext_VBAMode mode;
hr = ptrVBProj-get_Mode(&mode);
if(SUCCEEDED(hr))
{
switch(mode)
{
case VBIDE::vbext_vm_Run:
MessageBox(NULL, "IDE in Run mode", "My Addin", MB_SETFOREGROUND);
break;
case VBIDE::vbext_vm_Break:
MessageBox(NULL, "IDE in Break mode", "My Addin",
MB_SETFOREGROUND);
break;
case VBIDE::vbext_vm_Design:
MessageBox(NULL, "IDE in Design mode", "My Addin",
MB_SETFOREGROUND);
break;
}
}

}
else
{
MessageBox(NULL, "Please Enable \"Trust access to Visual Basic
Project\" option\n under Tools\\Macro\\Security menu.", "My Addin",
MB_SETFOREGROUND);
}


It works, but it always returns VBIDE::vbext_vm_Design without any
relation to the REAL state of the Workbook. :(



All times are GMT +1. The time now is 07:01 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com