Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

FP2000: How to Add HTML to a File by Using Visual Basic for Applications


View products that this article applies to.

This article was previously published under Q238423

↑ Back to the top


Summary

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

↑ Back to the top


Keywords: KB238423, kbprogramming, kbhowto

↑ Back to the top

Article Info
Article ID : 238423
Revision : 4
Created on : 10/11/2006
Published on : 10/11/2006
Exists online : False
Views : 231