Results 1 to 2 of 2

Thread: help on macro to replace html tags frontpage

  1. #1
    VBAX Newbie
    Mar 2011

    help on macro to replace html tags frontpage

    trying to create a macro to replace HTML tags in frontpage 2000. Have the following code that works up to some point. I need to look for tags like <td &^&*^> and change them to <td> I also want to look for tags like
    <P ALIGN=
    "LEFT" DIR="LTR"> and take them out. Tags are not alwas the same so I would look for <p &^&*^*> and take it out. Any ideas?

    [vba]Sub changetags()
    lngViewMode = Application.ActivePageWindow.ViewMode
    'Switch to Normal view.
    Application.ActivePageWindow.ViewMode = fpPageViewNormal
    Dim objElement As IHTMLElement
    Dim strTagName As String
    Dim strSearch As String
    Dim strReplace As String
    strPageHTML = ActiveDocument.DocumentHTML

    strReplace = "<td>"
    strSearch = "<td" & "*" & " >"
    strPageHTML = Replace(strPageHTML, strSearch, strReplace)

    'F'ing Microsoft Tags
    strSearch = "</p>"
    strReplace = ""
    strPageHTML = Replace(strPageHTML, strSearch, strReplace)

    strSearch = "<P ALIGN=" & Chr(34) & "LEFT" & Chr(34) & " DIR=" & Chr(34) & "LTR" & Chr(34) & ">"
    strReplace = ""
    strPageHTML = Replace(strPageHTML, strSearch, strReplace)

    ActiveDocument.DocumentHTML = strPageHTML

    Application.ActivePageWindow.ViewMode = lngViewMode
    End Sub[/vba]
    Last edited by Aussiebear; 03-11-2011 at 01:59 PM. Reason: Applied VBA tags to code

  2. #2
    VBAX Regular
    Feb 2011
    I am not an expert in Frontpage, but I have experience in using VBA to manipulate HTML. You will need to modify this code to made the resulting HTML useful to Frontpage (something with which I cannot help you).

    This is how I would modify those tag names:

    [vba]On Error GoTo error_handler
    'The Microsoft HTML Object library must be active (Tools > References)

    Dim webdoc As MSHTML.HTMLDocument
    Set webdoc = webbrowser1.Document
    'Or set this to your document.

    'Variables you will use to refer to the HTML elements
    Dim elem As MSHTML.HTMLBaseElement
    Dim newE As MSHTML.HTMLBaseElement

    Dim myStr As String

    'First the controls that you just want to modify:
    Dim myList As New Collection
    'To this list add the tag names of all the types _
    'of elemets that you want to modify (not delete!).
    myList.Add "TD"
    myList.Add "Etcetera"

    For counter = 1 To myList.Count

    For Each elem In webdoc.getElementsByTagName(myList(counter))

    myStr = Replace(elem.outerHTML, elem.innerHTML, "") 'Returns the tag names without all the stuff between.

    If InStr(1, myStr, " ") Then 'If there is a space in the tag name (indicating a lot of other stuff in the heading tag) then
    'Add more restrictive criteria if you need to, so you do not delete to much.

    Set newE = webdoc.createElement(elem.tagName) 'Create a new element of the same type

    On Error Resume Next 'Temporarily turn off error handler.
    newE.innerHTML = elem.innerHTML 'Transplant the guts from the other element
    On Error GoTo error_handler 'Turn error handling back on.

    webdoc.replaceChild newE, elem

    'Replace the bad element with the new one created above

    Set newE = Nothing 'refresh this variable

    End If
    Next elem

    Next counter

    'Now for the controls that must be deleted. Similar process as above
    Dim myList2 As New Collection
    myList2.Add "P"
    myList2.Add "Et cetera" 'Add the tag names that you want to be deleted.

    For counter = 1 To myList2.Count

    For Each elem In webdoc.getElementsByTagName(myList2(counter))
    myStr = Replace(elem.outerHTML, elem.innerHTML, "")

    If your_criteria Then
    elem.RemoveChild 'This function removes elements
    End If
    Next elem

    Next counter

    Set webdoc = Nothing
    Set elem = Nothing
    Set newE = Nothing
    Exit Sub
    'Error handler here[/vba]

    You will also need to make some modifications to suit your criteria for which tags need modifying.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts