Freigeben über


Working with the HTMLProject Object

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

The HTMLProject object is the top-level object representing the HTML code in an Office document. It is the equivalent of the top-level project branch in the Microsoft Script Editor Project Explorer when it contains an Office document. The HTMLProject object has properties you can use to determine the current state of an Office document and to access individual HTMLProjectItem objects, and methods you can use to save the current project or document.

For example, you can tell if a document is currently opened in the Microsoft Script Editor and if the HTML code that exists in the Script Editor is the same as what is contained in the document. If the HTML code is out of sync, you can programmatically synchronize the HTML code before manipulating the document's contents. You can add HTML to a document programmatically or load it from a file saved on disk. You can also use the objects contained within the HTMLProject object and their properties and methods to manipulate the HTML code or add script to the HTML code.

Note   The HTMLProject object is not available in Microsoft Access, Microsoft FrontPage, or Microsoft Outlook. To manipulate the HTML code in an Access DataAccessPage object, you use the object's Document property. To work with the HTML code in a page in FrontPage, you use the HTML tab in the FrontPage design environment.

The HTMLProject object's HTMLProjectItems property returns a collection of all of the HTMLProjectItem objects in the project. The default number of HTMLProjectItem objects in an Office application will depend on the kind of Office document you are working with. The following table shows the default number of HTMLProjectItem objects in a new Office document.

Application Default number of HTMLProjectItem objects
Excel 5 items (Book, Tab, Sheet1, Sheet2, Sheet3)
PowerPoint 2 items (SlideMaster, Slide1)
Word 1 item (Document Web Page)

You reference an HTMLProject object by using the HTMLProject property of an Office document. For example, the following code illustrates how to return a reference to the top-level HTMLProject object in each Office application:

' Create Word reference:
Dim prjWord As Word.HTMLProjectItem
Set prjWord = ActiveDocument.HTMLProject

' Create PowerPoint reference:
Dim prjPPT As PowerPoint.HTMLProjectItem
Set prjPPT = ActivePresentation.HTMLProject

' Create Excel reference:
Dim prjXL As Excel.HTMLProjectItem
Set prjXL = ActiveWorkbook.HTMLProject

Once you have created a reference to the HTMLProject object, you then use the HTMLProjectItems property to access individual HTMLProjectItem objects. In the following example, the IsHTMLProjectDirty procedure can be used to determine if the HTMLProject object in an Office document is "dirty" (contains changes). You use the blnRefreshProject argument to specify whether to refresh, or synchronize, the HTML code with the source Office document.

Function IsHTMLProjectDirty(objOffDoc As Object, _
                            blnRefreshProject As Boolean) As Boolean
   ' This procedure determines if the HTMLProject object
   ' in the document represented by the objOffDoc argument
   ' is dirty and, if so, refreshes the project according
   ' to the value of the blnRefreshProject argument.
   
   Dim prjProject As HTMLProject
   
   On Error GoTo IsHTMLDirty_Err
   
   Set prjProject = objOffDoc.HTMLProject
   With prjProject
      ' The Office document will be locked as soon as any
      ' changes are made to the HTML code in the document. 
      If .State = msoHTMLProjectStateDocumentLocked Then
         IsHTMLProjectDirty = True
         If blnRefreshProject = True Then
            ' Merge the changes to the HTML code with the
            ' underlying Office document.
            .RefreshDocument
         End If
      Else
         IsHTMLProjectDirty = False
      End If
   End With

IsHTMLDirty_End:
   Exit Function

IsHTMLDirty_Err:
   Select Case Err
      Case Is > 0
         IsHTMLProjectDirty = False
         Resume IsHTMLDirty_End
   End Select
End Function

The IsHTMLProjectDirty procedure is available in the modScripting module in HTMLProjectSamples.doc in the ODETools\V9\Samples\OPG\Samples\CH06 subfolder on the Office 2000 Developer CD-ROM.

You could use the preceding procedure to determine the state of any Office document by using the ActiveWorkbook, ActivePresentation, or ActiveDocument property in the first argument.

Once you have a reference to an HTMLProjectItem object, you can work directly with the HTML code in the document by using the object's Text property. For example, you can run the following code from the Immediate window to print all of the HTML code in a Word document:

? ActiveDocument.HTMLProject.HTMLProjectItems(1).Text

You can change the HTML code in an Office document by using the LoadFromFile method or by setting the Text property to the HTML code you want to use. The following example illustrates how to replace the HTML code in a Word document with the HTML code contained in a file on disk:

ActiveDocument.HTMLProject.HTMLProjectItems(1).LoadFromFile = "c:\MyHTMLFile.htm"

In many cases you will want to leave the existing HTML code in a document unchanged, but you will want to insert additional HTML code or script to give the document additional functionality when viewed in a Web browser. In the following example, the AddHTMLAndScriptExample procedure inserts within the first section of a Word document HTML code that includes formatted text, a command button, and script that executes when the command button is clicked. The formatted text and command button are contained in text returned by the GetText procedure and the script that executes when the command button is clicked is returned by the GetScript procedure. The InsertHTMLText procedure inserts the HTML code and script in an existing document just after the location specified by the procedure's second argument.

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, "<div class=""Section1"">", strNewText _
         & vbCrLf & strNewScript)
      .Text = strNewHTML
   End With
End Sub

To see all the code associated with this procedure, see the modScripting module in HTMLProjectSamples.doc in the ODETools\V9\Samples\OPG\Samples\CH06 subfolder on the Office 2000 Developer CD-ROM.