Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Charting / December 2003

Tip: Looking for answers? Try searching our database.

'Closing' chart window *and* removing chart using VBA

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Marc R. Bertrand - 16 Dec 2003 07:52 GMT
Hello,

When an embedded chart is in its window, clicking the 'X' of the
window merely removes the window but does not remove the chart (chart
object). Is there a way to remove/delete the chart when clicking on
the 'X'?

In other words, can an an event of some sort that recognizes the
Window becoming invisible, be used delete the chart? Would this sort
of this be solved with Windows handles and APIs...?

Thanks/Cheers.

Marc R. Bertrand
Tushar Mehta - 16 Dec 2003 15:53 GMT
I suspect that you can probably do what you want with some amount of
event / Win API programming.  However, as pointed out in the response
to the same question from yesterday, a non-programmatic solution
requires a single keystroke -- use of the Delete button.

Signature

Regards,

Tushar Mehta, MS MVP -- Excel
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

> Hello,
>
[quoted text clipped - 10 lines]
>
> Marc R. Bertrand
Jon Peltier - 16 Dec 2003 15:58 GMT
Marc -

Without blowing up my machine with APIs, I thought some kind of
application or chart event might help.

Application events: there are WindowActivate and WindowDeactivate
events, but the Chart Window is not recognized as a window by the event
handler.

Chart events: the Chart_Activate event fires when you click on the chart
(which you must do before activating the chart window, or non-window),
then nothing when the chart window is opened.  When the chart window is
closed (by the X or by clicking somewhere else on the screen), you get a
Chart_Deactivate event followed immediately by a Chart_Activate event.
If you do something clever like Ctrl-Tabbing or selecting another
workbook from the Windows menu, you get a Chart_Deactivate, followed by
the other workbook's window activating, but when you switch back to the
workbook with the chart, you get another Chart_Activate.  It seems hard
to me to distinguish between a fatal window close (using the X) and a
benign window close (just clicking somewhere in the worksheet).  If you
always want to kill the chart when the chart window is closed, then it
doesn't matter, just set a dummy variable when the Chart_Deactivate
happens, and if it's followed right away by the same chart activating,
then kill the chart.

When the X is clicked (or the chart window closes), the chart is still
selected.  Couldn't the user just delete the chart?  What is the purpose
of using the chart window to show the chart?  If the window is shown in
code, maybe you could at the same time create a dummy command bar with
one button, which when pressed kills the chart, then removes the command
bar.

- Jon
-------
Jon Peltier, Microsoft Excel MVP
http://www.geocities.com/jonpeltier/Excel/index.html
_______

> Hello,
>
[quoted text clipped - 10 lines]
>
> Marc R. Bertrand
Marc R. Bertrand - 16 Dec 2003 16:34 GMT
I have figured out what I wanted to do. I needed to trap an event for
the chart: the Deactivate event. This works for me. The chart will be
deleted if anything but the chart is clicked. Clicking on the chart
window's 'X' button does it, and so does clicking outside the chart,
ie., a cell.

In a class module that I named clsChartEvents, I have placed the
following:
==========================================================================
Option Explicit
Public WithEvents Embedded_Chart As Chart
Private Sub Embedded_Chart_Deactivate()
   ActiveSheet.ChartObjects.Delete
End Sub
==========================================================================
In the regular module that contains the chart making code, I placed
this (the Sub is called from the chart making code):
==========================================================================
Public Click As New clsChartEvents
Sub Enable_chart_events()
   Set Click.Embedded_Chart =
ThisWorkbook.Worksheets("Sheet2").ChartObjects(1).Chart
End Sub
==========================================================================

=====================================================================
> Hello,
>
[quoted text clipped - 10 lines]
>
> Marc R. Bertrand
Jon Peltier - 16 Dec 2003 16:49 GMT
Marc -

This command will remove all charts on the active sheet:

    ActiveSheet.ChartObjects.Delete

You can set up your sheet so all charts on it are enabled for chart
events.  In the regular code module, put this:

Dim mycharts() As New clsChartEvent

Sub Set_All_Charts()
    ' Enable events for all charts on a worksheet
    If ActiveSheet.ChartObjects.Count > 0 Then
        ReDim mycharts(ActiveSheet.ChartObjects.Count)
        Dim chtObj As ChartObject
        Dim chtnum As Integer

        chtnum = 1
        For Each chtObj In ActiveSheet.ChartObjects
            Set mycharts(chtnum).EmbChart = chtObj.Chart
            chtnum = chtnum + 1
        Next ' chtObj
    End If
End Sub

This enables all charts in the worksheet for events.  Then in the
_Deactivate event procedure, you can put this to remove the chart object
that contains it, but leave any others intact:

    Embedded_Chart.Parent.Delete

- Jon
-------
Jon Peltier, Microsoft Excel MVP
http://www.geocities.com/jonpeltier/Excel/index.html
_______

> I have figured out what I wanted to do. I needed to trap an event for
> the chart: the Deactivate event. This works for me. The chart will be
[quoted text clipped - 37 lines]
>>
>>Marc R. Bertrand
Marc R. Bertrand - 17 Dec 2003 05:36 GMT
Jon,

Thanks. I know what ActiveSheet.ChartObjects.Delete does, but I don't
care because in this case, it's only one chart I need to delete. But
thanks again, because the code below is a mighty fine technique that
will come in handy in many a situation.

Since I've got your attention on the subject, perhaps you can save me
some browsing and reading time and tell me how to change the caption
of the chart window. I don't want it to show the usual [book.xls]Sheet
Chartname. I read somewhere that this can't be done until the workbook
is saved again...a pain... Is this true? I want a window because 1)
the chart looks nice and 'finished' in a window and 2) I want to take
advantage of the window caption to display the title of the chart. And
no, I don't want a usual title on the chart that will take up space.
If I can't use the window caption, I guess I will have to reconsider.
This window title will change according to what called the chart
making code...

I look forward to opening that bottle of champagne when you will
announce your new and official Jon Peltier web site :)

Thanks Jon.

> Marc -
>
[quoted text clipped - 75 lines]
> >>
> >>Marc R. Bertrand
Jon Peltier - 17 Dec 2003 13:37 GMT
Marc -

The window caption is easy:

if activewindow.type = xlChartAsWindow then
    activewindow.Caption = "Your Message Here"
end if

The if-then makes sure you don't change the workbook or application
window caption.

- Jon
-------
Jon Peltier, Microsoft Excel MVP
http://www.geocities.com/jonpeltier/Excel/index.html
_______

> Jon,
>
[quoted text clipped - 99 lines]
>>>>
>>>>Marc R. Bertrand
Marc R. Bertrand - 17 Dec 2003 16:30 GMT
Hello Jon,

Well, here I go again. I solved my chart window title/caption problem
with some information from a previous post and a great book on APIs
that I just bought: by STEVE BROWN: VISUAL BASIC DEVELOPER'S GUIDE TO
THE WIN32 API.

============================================================================
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
       (ByVal lpClassName As String, ByVal lpWindowName As String) As
Long
Private Declare Function FindWindowEx Lib "user32" Alias
"FindWindowExA" _
       (ByVal hWnd1 As Long, ByVal hWnd2 As Long, _
        ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function SetWindowText Lib "user32" _
   Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As
String) As Long
Sub Change_window_chart_caption()
  'Gets the handle for the embedded chart: hWndXLE
  Dim hWndXL As Long, hWndDesk As Long, hWndXLE As Long
  Dim rc As Long
  Dim strTitle As String

  hWndXL = FindWindow("XLMAIN", Application.Caption)
  hWndDesk = FindWindowEx(hWndXL, 0&, "XLDESK", vbNullString)
  hWndXLE = FindWindowEx(hWndDesk, 0&, "EXCELE", vbNullString)

  strTitle = "THANK YOU MR BULLEN, MR BROWN, AND OTHER PALS..."
  rc = SetWindowText(hWndXLE, strTitle)
End Sub

=============================================================================

> Jon,
>
[quoted text clipped - 99 lines]
> > >>
> > >>Marc R. Bertrand
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.