auto "save-as" multiple docs at the same time

G

Guitaro

Hello, is there a way to select an entire set of Word docs, and save them all
as web pages (as html docs) without having to do it manually one-at-a-time?
For instance, I need to periodically update a directory of Word docs and
republish them to a website. Currently, I have to "save-as web page" one doc
at a time.

Any help is appreciated
 
G

Graham Mayor

Maybe with a macro - something like the following, which will convert all
docs to html using the filtered html converter? This version of the macro
uses the period between the filename and extension to remove the old
extension so the macro will not work correctly if there are additional
periods in the filename, but will instead crop the filename to the first
such period. The files are saved in the same folder as the original
documents.

Sub SaveAllAsHTM()
Dim strFileName As String
Dim strDocName() As String
Dim strPath As String
Dim oDoc As Document
Dim Response As Long
Dim fDialog As FileDialog
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
With fDialog
.Title = "Select folder and click OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User", , "Save all as HTML"
Exit Sub
End If
strPath = fDialog.SelectedItems.Item(1)
If Right(strPath, 1) <> "\" Then strPath = strPath + "\"
End With
If Documents.Count > 0 Then
Documents.Close SaveChanges:=wdPromptToSaveChanges
End If
strFileName = Dir$(strPath & "*.doc")
While Len(strFileName) <> 0
Set oDoc = Documents.Open(strPath & strFileName)
strDocName = Split(ActiveDocument.FullName, ".")
If ActiveDocument.SaveFormat = wdFormatDocument Then
ActiveDocument.SaveAs _
FileName:=strDocName(0) & ".htm", _
FileFormat:=wdFormatFilteredHTML
End If
oDoc.Close SaveChanges:=wdDoNotSaveChanges
strFileName = Dir$()
Wend
End Sub

http://www.gmayor.com/installing_macro.htm


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Guitaro

Thanks Graham. Looks promising. I created the macro, but I am not sure how it
knows what the appropriate starting directory is for apply the "save-as". If
I were to execute the macro, how does it know which directory to operate on?
thanks, Tim
 
G

Guitaro

Hi Graham, what you gave me worked great. Thanks alot (so you can disregard
my initial reply).

Two further questions:
1) would you know how to make it execute the same thing, only recursively
through all directories below the current one?
2) would you know why the macro you gave me does not convert Word 2007 docs
(I ahad to convert mine to Word 97-2003 docs first to get your macros to
work).
 
G

Graham Mayor

Guitaro said:
Hi Graham, what you gave me worked great. Thanks alot (so you can
disregard my initial reply).

You are welcome.
Two further questions:
1) would you know how to make it execute the same thing, only
recursively through all directories below the current one?

That's not quite as straightforward (though certainly possible). The method
is demonstrated at http://msdn.microsoft.com/en-us/library/aa164475.aspx. If
I have time I will adapt the macro to use this function.
2) would you know why the macro you gave me does not convert Word
2007 docs (I ahad to convert mine to Word 97-2003 docs first to get
your macros to work).

It was programmed only to save Word Document format. If you want it to work
on docx also then either remove the line
If ActiveDocument.SaveFormat = wdFormatDocument Then
and its associated 'end if' line
OR
Replace it with
If ActiveDocument.SaveFormat = 0 Or _
ActiveDocument.SaveFormat = 12 Then
 
G

Guitaro

Thanks Graham. I tried the suggestion for save-as for both Word 2007 and Word
97-2003, and it worked like a charm.

As far as the recursive folder suggested link, I have not tried that yet as
I am a VB novice, but will see if I can make sense of it. Thanks again.
 
G

Graham Mayor

The link I posted was one I saw in a recent thread in another forum. Having
examined it more thoroughly, I am fairly sure that it will not adapt to use
with Word 2007. I have therefore cross-posted this reply to the vba forum
with a view to getting some input from the programmers who lurk there :)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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