Saturday, May 22, 2010

How to retrieve images of the Word document using VBA / Extract images in

How to Save Word Document as WebPage using VBA

I have some lazy friends who come up with strange requirements. One such guy wanted me to extract images from Word document into a separate folder, which he want to use later. I found that saving the Webpage creates the images in a folder and that would be enough for the sloth friend. Here is the code snippet of that.

This code Saves the given document in Temporary folder (See How to get Temp folder using VBA)

Then it deletes the file and folder from the location (See Different Ways to Delete Files in VBA) to be sure there is no clash.

It then saves the document and loops through the Images folder using Dir function (See Dir Function in VBA) and stores them in an array

Function Save_As_HTML(ByRef oTempWd As Document) As Boolean

Dim sDir
Dim iDir As Integer
Dim oShp As Word.Shape                      ' Word Shape Object

On Error GoTo Err_Save_As_HTML

sTempFolder = GetTempFolder()
sImageFolder = sTempFolder & "Save_As_HTML_files\"

Delete_File sTempFolder & "Save_As_HTML.html"
Delete_File sImageFolder & "*.*"
RmDir sImageFolder

oTempWd.SaveAs sTempFolder & "Save_As_HTML.html", FileFormat:= _
        wdFormatHTML, LockComments:=False, Password:="", AddToRecentFiles:=True, _
        WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
         SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _

oTempWd.Close (False)
Erase arImageFiles

sDir = Dir(sImageFolder & "*.gif") ' You can also use PNG format
Do Until Len(sDir) = 0
    iDir = iDir + 1
    arImageFiles(iDir) = sDir
    sDir = Dir

If Err <> 0 Then
   Debug.Assert Err = 0
   Debug.Print Err.Description
   Resume Next
End If

End Function 

No comments:

Post a Comment

Share on Facebook
Related Posts Plugin for WordPress, Blogger...
Download Windows Live Toolbar and personalize your Web experience! Add custom buttons to get the information you care about most.