ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   On-the-fly determination of object type? (https://www.excelbanter.com/excel-programming/329562-fly-determination-object-type.html)

Ed

On-the-fly determination of object type?
 
I like to use the CopyAsPicture function. Unfortunately (at least in
XL2000), I need to code in the correct object type. I have
Dim rng As Range
Set rng = Selection
rng.CopyPicture xlPrinter, xlPicture
but that won't work for graphics or charts. Then I tried removing "As
Range" to make a general variable, but that still won't work on everything.

Is there any way I can maybe do something like:
Dim var
Set var = Selection
Obj$ = var.Type
Dim obj As Obj$
Set obj = var
obj.CopyPicture xlPrinter, xlPicture

Ed



Robert Bruce[_2_]

On-the-fly determination of object type?
 
Roedd <<Ed wedi ysgrifennu:

I like to use the CopyAsPicture function. Unfortunately (at least in
XL2000), I need to code in the correct object type. I have
Dim rng As Range
Set rng = Selection
rng.CopyPicture xlPrinter, xlPicture
but that won't work for graphics or charts. Then I tried removing "As
Range" to make a general variable, but that still won't work on
everything.


I think your problem is that when you select what looks like a chart, you
are in fact selecting a chartarea object which doesn't have a CopyPicture
method. Try this:

Sub test()
Dim var As Object
Set var = Selection
If TypeName(var.Parent) = "Chart" Then
var.Parent.CopyPicture xlPrinter, xlPicture
Else
var.CopyPicture xlPrinter, xlPicture
End If

End Sub

--
Rob

http://www.asta51.dsl.pipex.com/webcam/

This message is copyright Robert Bruce and intended
for distribution only via NNTP.
Dissemination via third party Web forums with the
exception of Google Groups and Microsoft Communities
is strictly prohibited and may result in legal action.



Ed

On-the-fly determination of object type?
 
Thank you, Rob. When I select the chart, the CopyAsPicture is available on
the Shift+Edit menu, so it looks to me like I've got a good object according
to Excel. But VBA apparently wants the object spelled out, or it won't
approve the Method. I could write a bunch of Ifs based on var.Type or
TypeName or such; I was hoping to avoid that, though. All I want is a macro
that will do anything I'm thinking of on any object I click - that's not too
much to ask, is it?? 8P

Ed

"Robert Bruce" <rob@analytical-dynamicsdotcodotukay wrote in message
...
Roedd <<Ed wedi ysgrifennu:

I like to use the CopyAsPicture function. Unfortunately (at least in
XL2000), I need to code in the correct object type. I have
Dim rng As Range
Set rng = Selection
rng.CopyPicture xlPrinter, xlPicture
but that won't work for graphics or charts. Then I tried removing "As
Range" to make a general variable, but that still won't work on
everything.


I think your problem is that when you select what looks like a chart, you
are in fact selecting a chartarea object which doesn't have a CopyPicture
method. Try this:

Sub test()
Dim var As Object
Set var = Selection
If TypeName(var.Parent) = "Chart" Then
var.Parent.CopyPicture xlPrinter, xlPicture
Else
var.CopyPicture xlPrinter, xlPicture
End If

End Sub

--
Rob

http://www.asta51.dsl.pipex.com/webcam/

This message is copyright Robert Bruce and intended
for distribution only via NNTP.
Dissemination via third party Web forums with the
exception of Google Groups and Microsoft Communities
is strictly prohibited and may result in legal action.






All times are GMT +1. The time now is 05:06 PM.

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