Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using VBA to build HTML documents 1

Status
Not open for further replies.

TorrM

Programmer
Oct 23, 2001
3
DE
Hi,

Do any of you have any idea how I can programatically, using VBA, build a HTML file that will be displayed within Microsoft Word where users can then save it.

So far I have not been able to do this unless I load the HTML from an external file and the file convertors operate.

Mark.
 
Hi, have you considered to use DATA ACCESS PAGES?

Automation with HTML is a issue to work on Hard. Above is an example.

Sub HTMLTest()
' This procedure illustrates how to use the IsHTMLProjectDirty
' procedure to save the HTML project associated with a document.
' The procedure also shows how to use VBA code to modify the HTML
' in an Office document. In this example, formatted text and a
' command button that executes VBScript is inserted into the document
' and the document is saved as an .htm file.
Application.Documents.Add
Call IsHTMLProjectDirty(ActiveDocument, True)
Call AddHTMLAndScriptExample(ActiveDocument)
Call IsHTMLProjectDirty(ActiveDocument, True)
ActiveDocument.SaveAs "c:\TestAutoHTML.htm", wdFormatHTML
ActiveDocument.Close
MsgBox "A new file named 'TestAutoHTML.htm' has been " _
& "created and saved to 'c:\TestAutoHTML.htm'. You " _
& "can open this file using your browser to see the " _
& "results of running the HTMLTest procedure."
End Sub Best Regards

---
JoaoTL
mail@jtl.co.pt
MS Access Site: The Page: moved to
 
Thanks for that information.

It appears that there is no code to show me how to add HTML to the active HTML project. Did you miss that part?

Thanks for your help,

Mark.
 
---
Sub AddHTMLAndScriptExample(objOffDoc As Object)
Dim itmPrjItem As HTMLProjectItem
Dim strNewText As String
Dim strNewScript As String
Dim strNewHTML As String

strNewText = GetText()
strNewScript = GetScript()

Set itmPrjItem = objOffDoc.HTMLProject.HTMLProjectItems(1)

With itmPrjItem
strNewHTML = .Text
Call InsertHTMLText(strNewHTML, &quot;<div class=Section1>&quot;, strNewText & vbCrLf & strNewScript)
.Text = strNewHTML
End With
End Sub
---

---
Public Function AddHTMLToPage(oDocument As Object, strHTMLtext As String, bClearPage As Boolean) As Boolean
' This function adds specified HTML content to the page specified in oDocument by creating a text range object
' from the oDocument.body object. If bClearPage is true the the entire content between <body> </body> is replaced
' otherwise the HTML is appended to the bottom of the current content, before the </body> tag

Dim r As Object, b As Object

On Error GoTo ecouldnotinserthtml
' get the textrange object
If bClearPage Then
Set r = oDocument.all.tags(&quot;BODY&quot;).Item(0).createTextRange
' clear out the current document
If bClearPage Then
Call r.pasteHTML(&quot;&quot;)
End If
r.collapse False
Set r = Nothing
End If

Set b = oDocument.all.tags(&quot;BODY&quot;).Item(0)
b.innerHTML = b.innerHTML + strHTMLtext + vbCrLf

AddHTMLToPage = True
Exit Function

ecouldnotinserthtml:
AddHTMLToPage = False
End Function

Sub TestIt()
Dim sHTMLString as String
sHTMLString = &quot;<B> <I> FOOBAR </I> </B>&quot; + vbcr
If AddHTMLToPage(ActivePageWindow.Document, sHTMLString, True) Then
Set myelem = ActivePageWindow.Document.all.tags(&quot;B&quot;).item(0)
End If
End Sub
--- Best Regards

---
JoaoTL
mail@jtl.co.pt
MS Access Site: The Page: moved to
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top