PDA

View Full Version : [SOLVED:] SaveAs for embedded objects



robaxeman
04-18-2011, 07:34 AM
Hi I'm new to VBA but trying to write some code that will save to disk all the embedded visio files in a WORD (2003) document. I want to use the SaveAs method but this is not available with the .OLEFormat object. Is there a way of saving such embedded objects?

What I've done instead is to open all the embedded visio files, and then save them all manually - hardly automation is it! The problem is that each Visio file is opened in a seperate Visio application instance, so there is no way of manipulating each files from a single instance. Does anyone know how I can open each embedded visio but in a single Application instance?

Many thanks for reading.:thumb

Robin

(Visio is also 2003, SP2).

robaxeman
04-19-2011, 02:03 AM
OK - making progress, but I keep hitting brick walls! So far I have worked out how to open the embedded visio files. Further, I worked out how I can find an open visio file and save it with a new file name. What I can't do is combine these two operations!

See the following code.....


Dim MyObj As Object
Dim shape As InlineShape
' This opens the embedded visio documents, each in their own instance of visio
For Each shape In ThisDocument.InlineShapes
If (shape.Type = wdInlineShapeEmbeddedOLEObject) And (InStr(shape.OLEFormat.ClassType, "Visio") > 0) Then
shape.OLEFormat.DoVerb (wdOLEVerbOpen) ' Open the first embedded visio
' Now I want to save it
Set MyObj = GetObject(, "Visio.Application")
Debug.Print MyObj.Application.Documents.Item(1) ' Prints the filename
MyObj.Application.Documents.Item(1).SaveAs FileName:="Diag" & count & ".vsd
End If
Next shape

But the SaveAs doesn't work - I get an error "Access has been denied"
Now, the 'SaveAs' line above works fine on a visio that I have opened manually, but not when the visio is opened from an embedded file in WORD.
This is driving me crazy! How can I get permission to save that file?
:banghead:

Thanks for any help!

Frosty
04-19-2011, 09:54 AM
I don't have visio installed, so I can't test exactly... but why don't you try a fully realized filepath, rather than just a file name and see if you get the same permissions error. I also don't see you dimming "count" anywhere... but I assume it's some kind of iteration number. I would convert that to a string rather than rely on the ole object to do it for you.

"C:\Temp\Diag" & cstr(count) & ".vsd"

Remember, you're talking to the application through a very thin straw... don't trust that all of the things that happen in the fully realized version work exactly the same (explicit conversions, last file path used, etc).

Frosty
04-19-2011, 10:24 AM
Just a little update, I'm getting the same error with an embedded Excel spreadsheet, although my error message is a little more robust.

It may help you to look at your Locals window, and start poking around to see what you can see about the various properties of the objects you're using. I was playing around with a sort of proof of concept where I would use CreateObject to give me my own version of Excel and try to copy the contents of the embedded object to the clipboard, then paste it into a workbook I created in the non-embedded version.

It's pretty hokey, but there may be multiple ways to play around with this in order to get your solution. Just trying to brainstorm with you.

robaxeman
04-19-2011, 12:00 PM
Hi Frosty,

Many thanks for taking the time to help - I'm not in the office right now so there's not a lot I can do, but I'll try your suggestions and keep you posted.

When you refer to the 'Locals' window - can you explain what that is? When I'm in the VB Editor I open up the object browser (right click anywhere in the code and it's a menu option). This tells me what methods and properties are available - but to be honest it's not always accurate (unless I'm reading it wrong - quite possible!).

Thanks again for your help,

R.

Frosty
04-19-2011, 12:26 PM
Right below that object browser function is the Add Watch function.

If you aren't familiar with that or the View > Locals Window functionality when stepping through code, try it out. it's a runtime tool which will help you a good bit at times

robaxeman
04-20-2011, 04:16 AM
Hi Frosty,

I tried using the full pathname and it worked! Can't believe I didn't think of that one myself - but many thanks for your help - I really appreciate it. As it happens, I continue to pass 'count' as an integer and it accepts that with no problems.

Had a look at the Locals view - I see there is lot of information there which could be useful as I move forward.

Thanks again,

R.:bow: