Saving a Range as HTML

E

Edward Thrashcort

I want to programmatically select a range from a document and then save the
contents of that range as a HTML file in MSWord. I want the formatting, and
pictures within the range to be preserved in the HTML.

What sort of object do I have to declare and how do I manipulate it to
create a HTML file?

Eddie
 
D

Doug Robbins - Word MVP

Best way is probably to delete the bits that you don't want and then save
the file as HTML

Dim keep As Range, discstart As Range, discend As Range
Set keep = somerange
With ActiveDocument
Set discstart = .Range
discstart.End = keep.Start - 1
Set discend = .Range
discend.Start = keep.End = 1
discstart.Delete
discend.Delete
.SaveAs "filename", FileFormat = wdFormatHTML
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
E

Edward Thrashcort

Thanks Doug

This solution gives me a slight problem in that, aster saving the HTML, I
then want to select another range (in fact several ranges) from the document
and save those as HTML. I previously tried using the Selection object and
copying them to a new document. The problem with this is that predefined
styles in the Normal template can cause the pasted contents to be displayed
in the style format of the destination document, rather than the source.

Following your solution, how would you suggest that I restore the document
to its original state before selecting the next range? Is it worth creating
a document (or range) object as a copy of the original? Is the "Undo"
object/method an appropriate way to restore the document?

Just one riding question. Is there a way to save as "clean" HTML rather
than as "Office-formatted" HTML. This is not a strict requirement but I
would like to be able to manage the documents in a HTML Editor once I have
extracted the data.

Note that I will eventually be processing about 300 documents and generating
several thousand HTML files, so I don't mind spending some time developing a
robust solution, providing I know how best to approach the problem.

Eddie
 
J

Jean-Guy Marcil

Edward Thrashcort was telling us:
Edward Thrashcort nous racontait que :
Thanks Doug

This solution gives me a slight problem in that, aster saving the
HTML, I then want to select another range (in fact several ranges)
from the document and save those as HTML. I previously tried using
the Selection object and copying them to a new document. The problem
with this is that predefined styles in the Normal template can cause
the pasted contents to be displayed in the style format of the
destination document, rather than the source.

Following your solution, how would you suggest that I restore the
document to its original state before selecting the next range? Is
it worth creating a document (or range) object as a copy of the
original? Is the "Undo" object/method an appropriate way to restore
the document?

Try this instead then:

Dim rgeToHTML As Range
Dim docHTML As Document

Set rgeToHTML = Selection.Range
Set docHTML = Documents.Add

With docHTML
.Range.FormattedText = rgeToHTML.FormattedText
.SaveAs "SomePath\SomenNme", FileFormat = wdFormatHTML
.Close
End With
Just one riding question. Is there a way to save as "clean" HTML
rather than as "Office-formatted" HTML. This is not a strict
requirement but I would like to be able to manage the documents in a
HTML Editor once I have extracted the data.

Try with
.SaveAs "SomePath\SomenNme", FileFormat = wdFormatFilteredHTML
instead of
.SaveAs "SomePath\SomenNme", FileFormat = wdFormatHTML

It will not be 100% clean, but it might be better for what you want to do
afterwards.

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
E

Edward Thrashcort

Thanks Jean-Guy, that looks compact!

Eddie
*From:* "Jean-Guy Marcil" <DontEvenTry@NoSpam>
*Date:* Tue, 27 Feb 2007 10:22:58 -0500

Edward Thrashcort was telling us:
Edward Thrashcort nous racontait que :


Try this instead then:

Dim rgeToHTML As Range
Dim docHTML As Document

Set rgeToHTML = Selection.Range
Set docHTML = Documents.Add

With docHTML
.Range.FormattedText = rgeToHTML.FormattedText
.SaveAs "SomePath\SomenNme", FileFormat = wdFormatHTML
.Close
End With


Try with
.SaveAs "SomePath\SomenNme", FileFormat = wdFormatFilteredHTML
instead of
.SaveAs "SomePath\SomenNme", FileFormat = wdFormatHTML

It will not be 100% clean, but it might be better for what you want to do
afterwards.

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
E

Edward Thrashcort

That worked perfectly except for a very minor syntax error
.SaveAs "SomePath\SomenNme", FileFormat = wdFormatHTML

..SaveAs "SomePath\SomenNme", FileFormat:=wdFormatHTML

Without the colon, it saves as a Word document

Eddie
 
J

Jean-Guy Marcil

Edward Thrashcort was telling us:
Edward Thrashcort nous racontait que :
That worked perfectly except for a very minor syntax error


.SaveAs "SomePath\SomenNme", FileFormat:=wdFormatHTML

Without the colon, it saves as a Word document

Sorry about that... you can tell from "SomePath\SomenNme" that I was typing
a bit too fast and did not check what I wrote very carefully!

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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