• Defining variable problem re: html (2003)

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » Defining variable problem re: html (2003)

    Author
    Topic
    #430169

    I am trying to make global changes to a large set of html files; specifically the font, colour and size. Altering a macro Hans was kind enought to provide me, I am trying to loop through these html files to make changes but I think I’m stuck right at the beginning with the Dim As. How do I declare the html file as a variable? Here’s the line that gives me the error:

    Set doc = html.Open

    The error points to html and says ‘”variable not defined”.

    (Sorry – I’m very new at this!)

    Deb

    The error is pointing to html

    Viewing 0 reply threads
    Author
    Replies
    • #1003736

      Can you post some more of the macro? It’s hard to know what you’re doing from a single line.

      • #1003750

        After a bit of fiddling around, I don’t get the “variable not defined” error any more, but now the code simply doesn’t work. I have de-bugged it but I’m not getting any hints as to what is wrong with it. Here it is…and thanks again Hans.

        Sub ChangeFontProperties()
        ‘ Path – make sure it ends in a backslash
        Const strPath = “Z:DataCC Test”
        ‘ Variables
        Dim strFile As String
        Dim html As Document
        Dim strTitle As String

        On Error GoTo ErrHandler

        ‘ Turn of screen updating (doesn’t help much)
        Application.ScreenUpdating = False
        ‘ First file name
        strFile = Dir(strPath & “*.htm”)
        ‘ Loop through files
        Do While Not strFile = “”
        strTitle = Mid(strFile, 1, InStr(1, strFile, “.”) – 1)
        ‘ Open document
        Set doc = Documents.Open _
        (FileName:=strPath & strFile, AddToRecentFiles:=False)
        Selection.WholeStory
        With Selection.Font
        .Name = “Verdana”
        .Size = 12
        .Color = 4805720
        End With
        ‘ Close and save
        doc.Close SaveChanges:=wdSaveChanges
        ‘ Get next document name
        strFile = Dir
        Loop
        MsgBox “The files have been converted.”
        Exit Sub

        ErrHandler:
        ‘ Display error message
        MsgBox Err.Description, vbExclamation

        End Sub

        • #1003753

          In the first place, the declaration

          Dim html As Document

          should be

          Dim doc As Document

          In the second place, you set a variable strTitle:

          strTitle = Mid(strFile, 1, InStr(1, strFile, “.”) – 1)

          but you don’t do anything with this variable. What was your intention?

          Apart from that, the code works, if you supply a folder that does contain .htm files (are you sure the extension is .htm and not .html?)

          • #1003758

            I’ve change the declaration and the strTitle line was there in error….I missed taking that line out.

            I have checked the file extensions and they are .html so I have altered the macro to reflect that and the path is correct. When I run the macro, no errors come up but the files have not changed. I’m not sure what else to check.

            Could it have anything to do with the folder that these html files reside in having sub files that contain each document’s header information?

            • #1003763

              When you look at the folder after running the macro, has the last modified date of the .html files changed to the date/time you ran the macro?

            • #1003765

              Oh, good thinking Hans. This is interesting…the file folders I mentioned in my previous post all say modified March 8, 2006. The actual html documents don’t. What does that mean?

            • #1003768

              Strange. Try the following: click in the first line of the code (Sub …) and press F8 to start single-stepping through the code. Each time you press F8, you will execute one instruction. You can switch back and forth between Word and the Visual Basic Editor using Alt+Tab or the Windows task bar. Can you see that the HTML files are being opened and modified?

            • #1003770

              The plot thickens. Doing as you instructed, pressing F8, the yellow line jumps from “Do While…” to the Msg Box line and skips all the good bits in between. That being the case, I can’t see how the file folders containing the headers are being modified at all.

              Edit: just a thought. Seeing as I’m very new at this, could not indenting lines properly in the code have anything to do with it?

            • #1003786

              While you’re at it… you may well want to redesign your pages to take advantage of a shared style sheet. In most cases, tags can be completely replaced with one page of style definitions and a few tag pairs. Of course, this might be beyond your brief, in which case, “never mind.” smile

              Edit: Oh… you’re actually editing them as Word documents. I had assumed you were simply editing them as text files. Please ignore this post.

            • #1003789

              Indenting code is merely an aide for viewing and understanding the code. Although I highly recommend indenting code consistently, it has no influence whatsoever on the way the code executes.

              If the code jumps straight from the Do While line to the MsgBox line, it means that there are no files that conform to the pattern Z:DataCC Test*.html. So either the path is not correct – look VERY carefully for spelling errors – even an extra or missing space will cause the path not to be recognized – or there are no .html files in the Z:DataCC Test folder.

            • #1003830

              Thanks for your patience with this Hans. All is well now. The tip off was in your last post about no html files in the folder. That made me wonder at bit and the problem for me was that the that the Type of all these files is HTML Document. The file extension (after un-hidding them in View) is htm. Lesson learned.

              Thanks again.

            • #1003834

              One more reason for always displaying extensions of known file types. smile

    Viewing 0 reply threads
    Reply To: Defining variable problem re: html (2003)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: