Closing the active document in the Visio DrawingControl

R

Robert

Hi all,
I'm encountering an error that has been reported before, all be it in
a different scenario, the error is "This operation cannot be performed
while doing in place editing." and I'm getting it when trying to close
the default document that gets created when initialising the
DrawingControl. Simply put, I slap the control on a form, fire up the
app, and try to run the following code :

AxDrawingControl1.Document.Close(),

and any number of variants of this command

(using AxDrawingControl1.Document.Application.ActiveDocument.close(),
AxDrawingControl1.window.document.close(), etc, etc) all of which give
me the same error.

In case anyone's wondering, I want to do this so that I can add a new
document based on a template, and this new document, while actually
being created, is not set as the active document by default, so I'm
trying to close the current (default) active document.

Thanks in advance
Robert
 
C

Chris Roth [ Visio MVP ]

Have you read this?

Programming with the Microsoft Office Visio 2003 ActiveX Control
http://msdn.microsoft.com/library/d.../odc_vsprogrammingwithvisioactivexcontrol.asp

A pertinent excerpt follows the signature.

I think that the bottom line is that you simply need to set the SRC property
of the control to the document that you want. As far as I can see, this
doesn't load *another* document, it replaces the existing document. If you
want to have more documents open, then perhaps you use the Visio object's
various Open methods. (VisApp = VisControl.Document.Application, for
example) I haven't double-checked this all, but give it a shot!

--

Hope this helps,

Chris Roth
Visio MVP

Managing Documents
When the Visio drawing control is loaded, it displays a blank Visio drawing.
If you want to display an existing Visio document, the drawing control
exposes an Src property for loading a document into the control. You can
then use the document's SaveAs method to save any changes.

Loading a Visio Document
Use the Visio drawing control's Src property to load a document into the
control. For example, the following C# example shows how to load a Visio
drawing:

drawingControl.Src = "C:\\Drawing.vsd";
You can load any Visio file type using the Src property (for example, .vsd,
..vdx, .vst, or .svg). The file can be stored locally or on a remote file
server.

The Visio control's Src property loads a copy of the file specified in the
Src value. To persist the changes in the control's document, you must save
the Visio document using the SaveAs method. Alternatively, you can persist
changes in-stream. In both cases, however, you do not modify the original
document loaded by the Src property.

Saving a Visio Document to File
The control loads a copy of the file specified by the SRC property. The file
loaded through SRC is not opened for read/write operations, and therefore
cannot be saved using the Save method. To save changes to the document
loaded in the Visio drawing control, call the document's SaveAs method. The
following C# example shows how you can use the drawing control Document
property to call the SaveAs method:

Visio.Document document = drawingControl.Document;
document.SaveAs("C:\\Drawing.vsd");
You cannot use the SaveAsEx method to save Visio 2003 documents to Visio
2002 format in the Visio ActiveX control. To save a drawing loaded in the
Visio drawing control to Visio 2002 format, launch an invisible instance of
Visio and call the SaveAsEx method in your Visio application instance, as
demonstrated in this Visual Basic 6.0 code that saves a Visio 2003 drawing
into the Visio 2003 file format:

Application.Documents(1).SaveAsEx("C:\Documents and Settings" & _
"\myusername\My Documents\Visio2002 file.vsd", visSaveAsWS + _
visSaveAsListInMRU)
Saving a Visio Document in the Control to Stream
Developers may want to persist changes to a drawing in the Visio drawing
control without saving the drawing to disk. For example, if a user modifies
a Visio document in an embedded control in a Word document, the changes are
lost when the user forwards that Word document in an e-mail message. When
another user opens the document, the control loads the file specified by the
Src property, overwriting any modifications.

To persist changes in-stream to a Visio document in the control

1.. Load the original document using the Visio control's Src property.
2.. After the document loads, set the Src property to an empty string.
When the control is activated in its container document after the first
initialization, the control displays the last in-stream image rather than
the original document specified by the Src property.

Using Blank Documents in the Control
To load a blank drawing in the control when the control is initialized for
the first time, set the Src property to an empty string. However, if you
want to emulate creating a new blank drawing in the control after loading an
existing drawing document, you have three choices:

a.. Select all and delete all shapes in the drawings. This approach allows
the developer to preserve any existing styles in the currently loaded
document.
b.. Load a new, blank document using the Src property.
c.. Dynamically destroy and reinitialize the Visio drawing control in the
document.
 
R

Robert

Thanks Chris,

Together with you suggestions, as well as the excerpt from the article
below, it becomes quite clear that I wasn't using the control
correctly. Have rectified this, and am now full steam ahead !

Robert

"Understanding the Visio Drawing Control's SDI
---------------------------------------------
When designing an application that uses the Visio drawing control,
it's important to understand that the Visio control supports a single
document in a single window. The control's single document interface
(SDI) architecture results in the following considerations when
designing the Visio drawing control integration with your application:

Use multiple instances of the Visio drawing control to display
multiple Visio documents in your application. Unlike the Visio client
application, which can display multiple documents and windows at a
time, the Visio drawing control can only display a single document per
instance of the control. If the developer wants to display multiple
Visio documents, the developer can embed multiple instances of the
control in the application, with each instance loading a separate
Visio document."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top