Detect Ms-Word contains image (using VB 6)

S

Sandra

Hi,

Can I detect if a MS-Word document contains images. (using VB 6)
I plan to create an application that walks through all my Ms-Word documents
and detects if they contain one or more images.
After that i want to extract these images.

Any suggestions.

Sandra
 
M

mike w.

save the doc in HTML format ,format=101, and it will put all images into a seperate folder
no files in the folder no images

mike w.
 
G

Gale Green

Can I detect if a MS-Word document contains images. (using VB 6)
I plan to create an application that walks through all my Ms-Word documents
and detects if they contain one or more images.
After that i want to extract these images.

Assuming that you already have a Word Application object called
WordApp:

<air code>

Dim MyShape As Shape

For Each MyShape In WordApp.ActiveDocument.Shapes

If MyShape.Type = msoPicture Then

' Do what you like with MyShape here.
End If
Next

Gale.
 
S

Sandra

Gale,
Assuming that you already have a Word Application object called
WordApp:
I presume that you mean the Microsoft Word xx Object Library?
For Each MyShape In WordApp.ActiveDocument.Shapes
How do i set the ActiveDocument?

Sandra
 
G

Gale Green

Hi Sandra.
How do i set the ActiveDocument?

Having looked at this further, I think this may not be the solution
you are looking for. I cannot find a way of extracting the Shape once
you've found it, other than to select it and then copy it to the
clipboard. There is very likely a way to extract it to a file, but I
can't find it. You may get a better answer in a Word or Office
newsgroup.

If you do want to proceed with this method, here is how you process
each of a set of documents:

First, set a reference to the Microsoft Word Object Library, and to
the Microsoft Office Object Library. You only need the Office library
because the msoPicture constant is in Office (I thought it was in
Word) but you can't redistribute Office libraries so the number of
references doesn't really matter.

Dim WordApp As Word.Application, _
WordDoc As Word.Document, _
MyShape As Word.Shape, _
CurrentFile As Long, _
NameArray() As String

' The code assumes that there is a set of file names in
' NameArray, above.

Private Sub Command1_Click()

Set WordApp = New Word.Application

For CurrentFile = LBound(NameArray) To UBound(NameArray)

Set WordDoc = WordApp.Documents.Open( _
NameArray(CurrentFile))

For Each MyShape In WordDoc.Shapes

If MyShape.Type = Office.msoPicture Then

MyShape.Select

WordApp.Selection.Copy

' Here, you would have to take the picture off the
' clipboard and get it into a Picture Box or a memory DC,
' or similar, so that it could be saved to a file. Answers
' to how you do that can be obtained at
' microsoft.public.vb.winapi.graphics.

End If

Next

WordDoc.Close wdDoNotSaveChanges
'WordDoc.Close wdSaveChanges

Next

Set WordDoc = Nothing

WordApp.Quit

Set WordApp = Nothing

End Sub
 
D

Doug Robbins - Word MVP

See http://www.lebans.com/msword.htm

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
G

Gale Green

Hi Sandra.


Having looked at this further, I think this may not be the solution
you are looking for. I cannot find a way of extracting the Shape once
you've found it, other than to select it and then copy it to the
clipboard. There is very likely a way to extract it to a file, but I
can't find it. You may get a better answer in a Word or Office
newsgroup.

If you do want to proceed with this method, here is how you process
each of a set of documents:

If you use the code I gave earlier to copy each image to the
clipboard, you can then use the code pointed to by Doug Robbins's post
to convert the clipboard data to either an Enhanced Metafile or to a
bitmap in a DIB section (from where you can save it as other file
types).

Gale.
 
P

Peter Hewett

Hi Gale Green

Just a couple of comments on your code. Firstly it only iterates the Shapes collection,
you also need to iterate the InlineShapes collection. Also depending on what the OP is
really trying to achieve you may need to iterate other StoryRanges as well. For instance
if the document contains logos in the Header/Footer of the document you need to iterate
the Headers/Footers as well. The statement:

For Each MyShape In WordDoc.Shapes

Only applies to the Body Text of the document. To be truly comprehensive you'd need to
iterate all StoryRanges as you can even insert pictures in Footnotes!

As mike.w suggested earlier in the thread exporting as HTML has merits (albeit sloooow).

HTH + Cheers - Peter
 
G

Gale Green

Hi Gale Green

Just a couple of comments on your code. Firstly it only iterates the Shapes collection,
you also need to iterate the InlineShapes collection. Also depending on what the OP is
really trying to achieve you may need to iterate other StoryRanges as well. For instance
if the document contains logos in the Header/Footer of the document you need to iterate
the Headers/Footers as well. The statement:

For Each MyShape In WordDoc.Shapes

Only applies to the Body Text of the document. To be truly comprehensive you'd need to
iterate all StoryRanges as you can even insert pictures in Footnotes!

Yes, all good points.

Gale.
 

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