How to change of thisdocument project

Started by davidgon, May 02, 2017, 02:49:52 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

davidgon

Hello!

I have a macro stored in a stencil. When I drop a shape, the macro starts working. The problem comes in "ThisDocument.DataRecordsets(1)". The datarecordsets are in the main document, not in the stencil. What do I have to write in order to refer to the main document in the ThisDocument statement?

Thanks!

Yacine

#1
When you drop your shape, you probably know it - assume it's "shp".
The document it is contained in is shp.parent.document.
(I assume you drop the shape on the page, not in a container - otherwise you'd need to iterate over parent till you reach page level).
Yacine

davidgon

But the point is I don't know how to refer to the recordset stored in the document form the code in the stencil

I don't know if I'm explaining myself properly  :(

Yacine

#3
You said that the macro is triggered when the shape is dropped on the drawing.
So the shape has hopefully a formula in the "ondrop" cell in the shapesheet. Something like: "=Callthis("myMacro")

myMacro would look like this:

sub myMacro (shp as shape) 'where shp is the shape object you created by dropping the master on the drawing
... do something.
end sub
Now you can modify this macro as follows
sub myMacro (shp as shape)
dim myDoc as document
dim myRS as datarecordset
set myDoc = shp.parent.document
set myRS = myDoc.DataRecordsets(1)
... do something.
end sub
Yacine

davidgon

That's exactly what I was looking for. It works perfectly.

Thank you so much!!  ;D

Yacine

Keep in mind that the code makes a lot of assumptions.
For a robust code you'll need to ensure that ".parent.document" is really the document and that ".DataRecordset(1)" is the right datarecordset.
Yacine