The sample code in this article explains how to programmatically insert HTML into a file by using Visual Basic for Applications.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
The following macro assumes that you have a web open in FrontPage and that there is a file in that web called example.htm. After you run this macro, open the example.htm file to see the text that was inserted.
Sub AddHTML()
' Set up variables.
Dim myDocument As FPHTMLDocument
Dim myFile As WebFile
Dim myPageWindow As PageWindow
Dim myTag As IHTMLElement
Dim myText As String
' Set myFile to equal the example.htm file.
Set myFile = ActiveWeb.RootFolder.Files("example.htm")
' Set myPageWindow to equal myFile.
' We also use the Edit method to change the view to Normal view
' in case the file is opened. In order to insert the HTML, we must
' be in Normal view.
Set myPageWindow = myFile.Edit(fpPageViewNormal)
' Set myDocument to equal the document contained in myPageWindow.
Set myDocument = myPageWindow.Document
' Set myTag to equal the first BODY tag.
' The All collection referenced here contains a reference to all
' HTML tags on the page.
Set myTag = myDocument.all.tags("body").Item(0)
' myText is the text we are going to insert.
' The <b> tag makes this text bold.
myText = "<b>This was inserted with VBA!</b>"
' Call the insertAdjacentHTML method.
' Using this method, we add myText after the beginning BODY tag.
Call myTag.insertAdjacentHTML("AfterBegin", myText)
' Now we save the page. We use the document's original URL.
myPageWindow.SaveAs myPageWindow.Document.Url
' Then we close the page. We append False to this method so that
' the method won't force the page to be saved again.
myPageWindow.Close False
End Sub
For more information about how to use the sample code in this article, click
the article number below to view the article in the Microsoft Knowledge
Base:
212536�
OFF2000: How to Run Sample Code from Knowledge Base Articles