Moding HTML Files

J

Jim Williams

I have a whole load of routines in OUTLOOK that do different things
depending on the attachment.
I mess around with excel spreadsheets and word documents - what i need
to do now is receive a word document and then save it as a HTML page.
I then need to alter the BODY tag of the saved HTML document to insert
some "onload" events.

How do i edit the html document in the vba. When i was messing with
excel and word i did this

Dim objword As Word.Application (or excel)
Set objword = New Word.Application(or Excel)

objword.Documents.Open (worddocument)
objExcl.Workbooks.Open (exceldocument)

how do I open the html document - i was looking for something simple
like
Dim objNotepad as notepad.application - but that doesnt work

any ideas

thanks
 
E

Eric Legault [MVP - Outlook]

Your post is more suited to the Word programming newsgroups.

Try setting the FileName parameter of the Open method to the path of your
..html document.
 
M

Michael Bauer

Hi Jim,

do you really want to edit the file by VBA? For that you wouldn´t need
Excel or Word.

For just adding a few characters into the body tag I would use the InStr
function.

Methods for reading/writing a file:

Public Function ReadFile(sPath As String) As String
On Error GoTo AUSGANG
Dim lFileNr As Long
Dim sText As String

lFileNr = FreeFile

Open sPath For Binary As #lFileNr
sText = Space$(LOF(lFileNr))
Get #lFileNr, , sText

AUSGANG:
If lFileNr Then
Close #lFileNr
End If

If Err.Number Then Err.Raise &H800A0000 Or Err.Number, _
Err.Source, Err.Description, Err.HelpFile, Err.HelpContext

ReadFile = sText
End Function

Public Sub WriteFile(sPath As String, _
sText As String, _
Optional ByVal bAppend As Boolean _
)
On Error GoTo AUSGANG
Dim lFileNr As Long

lFileNr = FreeFile

Select Case bAppend
Case False
Open sPath For Output As #lFileNr
Case Else
Open sPath For Append As #lFileNr
End Select

Print #lFileNr, sText;

AUSGANG:
If lFileNr Then
Close #lFileNr
End If

If Err.Number Then Err.Raise &H800A0000 Or Err.Number, _
Err.Source, Err.Description, Err.HelpFile, Err.HelpConsText
End Sub

If you´ve got the file content then search in it for the body tag:

lStart = InStr(1, sFileContent, "<body")
lEnd = InStr(lStart+1, sFileContent, ">")
sBody = Mid$(sFileContent, posStart, (lEnd-lStart)+1)

Write your characters at the beginnning:
sYourString = " YourString"
sBody = Left$(sBody, 5) & sYourString & Mid$(sBody, 6)

Insert the new body:

sNewFileContent = left$(sFileContent, lStart-1) & _
sBody & Mid$(sFileContent, lEnd+1+Len(sYourString)


HTH. Written in the morning before the first coffee and without tests...
 
J

Jim Williams

fantastic - thanks for the code - works spot on

exactly what i needed to do

cheers
 
Top