Saving Visio objects in Word documents as Visio files

C

Chris

I posted the following on microsoft.public.word.vba.general.
---------------------------------
Our Word documents include Visio objects whose original files are now
scattered. I need VBA to do the equivalent of double-clicking to open
the object in Visio, saving as a file, and then returning to Word to
find the next Visio object. I've been looking at examples of calling
other apps, but I don't understand how to move between Word and Visio
as I described.

I got the following response. I'm having trouble even running Ms
Meister's example, but I'd appreciate any light you can shed on how I
handle Visio if I can activate it.


Cindy Meister responded:

Well, I can only give you the Word side of things. You'll need to ask
in a Visio group to get help with automating Visio.

Any OLE object embedded in a Word document is - as far as Word is
concerned - a graphic. It's either an InlineShape (Word treats it like
a text character) or a Shape (text wrap formatting is applied).


Both InlineShapes and Shapes have an OLEFormat.Object property that
lets you activate and access the automation interface of an OLE object.

Mainly, I do this with Excel, so here's an example for that you can use

as a basis:


Sub ExcelObject()
Dim of As Word.OLEFormat
Dim oXL As Excel.Workbook


Set of = ActiveDocument.InlineShapes(1).OLEFormat
of.Activate
Set oXL = of.Object
Debug.Print oXL.Name
End Sub


Note that you have to set a reference to the object library of what you

want to manipulate if you want to use intellisense. Otherwise, you have

to Dim oXL As Object (late-binding). I recommend you at least start
with a reference and Intellisense if you don't know Visio's object
model at all. At a later point you can remove it and change any
declarations to use the Object type.


Now you have to work with the Visio folks to find out


1. What type of object is stored in a Word document
2. How to declare and refer to that in your code
3. The syntax to save it to a separate file.


Cindy Meister
INTER-Solutions, Switzerland
 
J

John

Hello Chris,

Cindy's example worked ok for me (obviously you need to change the excel
elements for Visio), so have a go with the code below:

Sub SaveEmbeddedVisioObjects()
Dim of As Word.OLEFormat
Dim vDoc As Visio.Document
Dim sNewFileName As String
Dim i As Integer

For i = 1 To ActiveDocument.InlineShapes.Count
Debug.Print ActiveDocument.InlineShapes(i).OLEFormat.ProgID
If ActiveDocument.InlineShapes(i).OLEFormat.ProgID =
"Visio.Drawing.11" Then
Set of = ActiveDocument.InlineShapes(i).OLEFormat
of.Activate
Set vDoc = of.Object
Debug.Print vDoc.Name
sNewFileName = "C:\NewFile" & i & "_" & Format(Date, "Long
Date") & ".vsd"
vDoc.SaveAs (sNewFileName)
End
Next i

End Sub

Bear in mind what Cindy says about the two types of shapes in Word. The
above code only picks up InlineShapes, but you can easily add the 'Shapes'
part if you need to. Also, as she mentions, make sure you've set a
reference to the Visio library in your Word project
(Tools/References.../Microsoft Visio 11.0 Type Library).

Anyway, probably not bullet proof but seems to work.

Hope it helps

Best regards

John

PS - Visio "11.0" is the 2003 edition and you'll need to change this if
you're using an earlier, or later, version.
 
C

Chris

John, thanks very much for your help. As you can tell, I'm new to some
of this. I have three books about macros and VBA, but I can't find an
example showing how to do what you suggest at the end, "make sure
you've set a reference to the Visio library in your Word project..."
What would this statement look like?
 
J

John

Hello Chris,

What we're doing here is telling Excel where to look for the library that
contains all of the objects and method for Visio and you do this from within
the VBE (the place where you write your code). So from there, make sure
your code is visible and then click on the "Tools" menu and select the
"References..." item. A new dialog will appear with a long list possible
references on your machine, some of which will already have a tick in their
respective check boxes. You need to scroll down the list to the Visio one,
which for Visio 2003 is "Microsoft Visio 11.0 Type Library", check its box
and click OK. (If your version of Visio is older it may have a different
name. For example Visio 2002 is "Microsoft Visio 11.0 Type Library", I
think.)

Anyway, once you've set the reference you should find Intellisense for the
Visio objects and you project should compile properly.

Have a go a let us know how you get on.

Best regards

John
 
J

John

Sorry.......typo in the first paragraph, should read:

.......For example Visio 2002 is "Microsoft Visio 10.0 Type Library", I
think.)

John
 
C

Chris

Thanks again for your help. I set "References" to Visio but I'm still
hitting a snag with the following object:

ActiveDocument.InlineShapes(i).OLEFormat.ProgID

Each statement that includes that object gives me the message:

Object variable or With block variable not set

The only other VBA user here couldn't see the problem, but we're hoping
there's an easy explanation.
 
J

John

Are you talking about the Debug line, or the If line?

Do you want to post your code so we can check the details?

Best regards

John
 
C

Chris

Here's my code, taken pretty straight from what you suggested. I made
local changes that shouldn't affect the logic. These are annotated.
Thanks for looking at this.
----------------------------------------------------------------

Sub SaveEmbeddedVisioObjects()

Dim of As Word.OLEFormat
Dim vDoc As Visio.Document
Dim sNewFileName As String
Dim i As Integer

pathName = ActiveDocument.Path
' Get document name
longDocName = ActiveDocument.Name ' Full document name
lenDocName = Len(longDocName) - 4
argDocName = Left(longDocName, lenDocName) ' Name without
extension


For i = 1 To ActiveDocument.InlineShapes.Count

Debug.Print ActiveDocument.InlineShapes(i).OLEFormat.ProgID

' Original said "Visio.Drawing.11"
If ActiveDocument.InlineShapes(i).OLEFormat.ProgID =
"Visio.Drawing.6" Then
Set of = ActiveDocument.InlineShapes(i).OLEFormat
of.Activate
Set vDoc = of.Object
' Debug.Print vDoc.Name

' original:
' sNewFileName = "C:\NewFile" & i & "_" & Format(Date,
"Long Date") & ".vsd"

' For first Visio file in file s2.doc, constructing
'
....\FAU_Conversion\word\CND\s2_files\Vis_s2_1.vsd

sNewFileName = pathName & "\" & argDocName & "_files" & "\"
& "Vis_" & argDocName & "_" & i & ".vsd"
vDoc.SaveAs (sNewFileName)
End If
Next i

End Sub
 
J

John

Hello Chris,

I'm afraid this has me stumped as I can't recreate your error. I think the
problem is a Word one as if you can't get a reference to the embedded object
it doesn't really matter whether the object is Excel, Visio or anything else
(as you haven't got to the Visio part). My suggestion is to get back to the
original code for Word that Cindy was suggesting (see below) and once that
works then you can deal with the Visio side of it. So try this in Word (no
Visio references needed) and if you don't get any joy from that, post a
question back to the Word newsgroup. Sorry it's not a solution but I think
this is your best path for now.

Sub CheckShapesInWord()
Dim i As Integer

MsgBox ("Inlineshapes count = " & _
ActiveDocument.InlineShapes.Count & vbCr & _
"Shapes count = " & ActiveDocument.Shapes.Count)

For i = 1 To ActiveDocument.InlineShapes.Count
'Debug.Print ActiveDocument.InlineShapes(i).OLEFormat.ProgID
Set of = ActiveDocument.InlineShapes(i).OLEFormat
of.Activate
Next i
End Sub

Best regards

John
 

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