Results 1 to 15 of 15

Thread: [RESOLVED] A Simple One

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    11

    Resolved [RESOLVED] A Simple One

    I'm not too good at this VBA stuff so I'd appreciate some 'gentle' help.

    I have a database made up of .htm pages stored on my computer. They are a part of a complete database of information that enables me to edit them using html in the source code. At present the only way I can do this is to open the page in IE. right click and get up the source code and then enter my comments in html.

    I would like to write something in VBA that enables me to open these 'web pages' in Notepad and then add some html. Then save and close it so that the next time I open it in IE the page has been changed as I need.

    Can anyone tell me how to open the source code of a web page in Notepad from VBA?

    Thanks in advance

    -Paul

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: A Simple One

    One way would be to shell out notepad with the html file as the parameter to open with.
    VB Code:
    1. Shell "C:\Windows\Notepad.exe C:\Test.html", vbNormalFocus
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    11

    Re: A Simple One

    RobDog888 - Yes that worked perfectly, thank you. I know I said it was 'A simple One' but I didn't realise it would be that simple.

    My next problem is to find a specific piece of html on the open .txt page, highlight it, paste some new html from the clipboard in its place and then save the .txt.

    I've looked in Excel help for 'Find' but I keep getting directed to the If...then...Else help page when I request the FindNext info.

    As for the rest of the stuff I need, I cannot find anything on them at all. Maybe I'm looking in the wrong place.

    Any chance of helping me out please?

    Many thanks

    Regards

    -Paul

  4. #4
    Lively Member
    Join Date
    Jun 2005
    Posts
    112

    Re: A Simple One

    Doing that with the open notepad window won't be the easiest task. It shouldn't be too tough with File IO though. Where do you plan on inputting the old and new HTML code? A userform? Is this running from MS Access? I can whip up a sample in a bit.

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: A Simple One

    Yes, its easier to parse the htm file and replace the code you desire and save the file.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6
    Lively Member
    Join Date
    Jun 2005
    Posts
    112

    Re: A Simple One

    A little lenghty but easy to use.

    Paste this in a module. Sample usage is in Sub Usage() at the bottom.

    VB Code:
    1. Sub ReplaceHTML(SourceFile As String, sText As String, rText As String)
    2.    
    3.     Dim TargetFile As String, tLine As String, tString As String
    4.     Dim p As Integer, i As Long, F1 As Integer, F2 As Integer
    5.    
    6.     TargetFile = "tmpfile.tmp"
    7.     If Dir(SourceFile) = "" Then Exit Sub
    8.     If Dir(TargetFile) <> "" Then
    9.         On Error Resume Next
    10.         Kill TargetFile
    11.         On Error GoTo 0
    12.         If Dir(TargetFile) <> "" Then
    13.             MsgBox TargetFile & " already open, close and delete / rename the file and try again.", vbCritical
    14.             Exit Sub
    15.         End If
    16.     End If
    17.     F1 = FreeFile
    18.     Open SourceFile For Input As F1
    19.     F2 = FreeFile
    20.     Open TargetFile For Output As F2
    21.     i = 1
    22.     While Not EOF(F1)
    23.         Line Input #F1, tLine
    24.         If sText <> "" Then
    25.             ReplaceTextInString tLine, sText, rText
    26.         End If
    27.         Print #F2, tLine
    28.         i = i + 1
    29.     Wend
    30.     Close F1
    31.     Close F2
    32.     Kill SourceFile
    33.     Name TargetFile As SourceFile
    34. End Sub
    35.  
    36. Private Sub ReplaceTextInString(SourceString As String, SearchString As String, ReplaceString As String)
    37.     Dim p As Integer, NewString As String
    38.     Do
    39.         p = InStr(p + 1, UCase(SourceString), UCase(SearchString))
    40.         If p > 0 Then
    41.             NewString = ""
    42.             If p > 1 Then NewString = Mid(SourceString, 1, p - 1)
    43.             NewString = NewString + ReplaceString
    44.             NewString = NewString + Mid(SourceString, p + Len(SearchString), Len(SourceString))
    45.             p = p + Len(ReplaceString) - 1
    46.             SourceString = NewString
    47.         End If
    48.         If p >= Len(NewString) Then p = 0
    49.     Loop Until p = 0
    50. End Sub
    51.  
    52. Sub Usage()
    53.     'This example will replace all instances of <h2> with <h1>
    54.     ReplaceHTML "c:\tmp\test.html", "<h2>", "<h1>"
    55. End Sub

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    11

    Re: A Simple One

    mikeyc1204 - I'm amazed at the effort you have put in help me with this - thank you. However, the problem is, he code you have put up totally confuses me and I have no idea what to do with it or if I need to in put my own file names, and if so, where.

    Any chance you can use a bit more of your time and give me some clues as to where to go now.

    Many thanks

    -Paul

  8. #8
    Lively Member
    Join Date
    Jun 2005
    Posts
    112

    Re: A Simple One

    This is the only part of the code you need to change. Tailor it to where your html documents are and what tags or info you want to change.

    VB Code:
    1. Sub Usage()
    2.     'This will replace all instances of <h2> with <h1>
    3.     ReplaceHTML "c:\Test.html", "<h2>", "<h1>"
    4. End Sub

    Just paste the rest of the code into any module. Once you paste that big block in, you can use the above line in any other procedure you already have set up.

    If you want to make 3 changes, say change a copyright line, a bold tag to an italic tag, or a link location, you can just call the ReplaceHTML sub 3 times like so:

    VB Code:
    1. Sub Example()
    2.     ReplaceHTML "c:\Test.html", "copyright 2005", "copyright 2006"
    3.     ReplaceHTML "c:\Test.html", "b>", "i>"
    4.     ReplaceHTML "c:\Test.html", "http://www.yahoo.com", "http://my.yahoo.com"
    5. End Sub

  9. #9

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    11

    Re: A Simple One

    Thanks for that mikeyc1204. Now I understand a bit more.

    OK, the problem that I seem to be coming up against now is that when I ran the code in an Excel VBA module (I presume that is OK) it didn't seem to go anywhere. So I ran it in break mode with a break in the first Sub routine at the line that goes

    TargetFile = "tmpfile.tmp"

    This resulted in the me being able to see that the variable 'TargetFile' had talen on the value which was equal to the path for the .html file I was testing it on. However, the code ignored the value and when it hit

    If Dir(SourceFile) = "" Then Exit Sub

    we exited the sub!

    I'd appreciate any ideas on this.

    Regards

    -Paul

  10. #10
    Lively Member
    Join Date
    Jun 2005
    Posts
    112

    Re: A Simple One

    When you test it, how are you starting the code?

    The value of TargetFile should always be "tmpfile.tmp". If it is taking on the value of your html file, then something was edited incorrectly. The line should stay 'TargetFile = "tmpfile.tmp"'.

    The line where you indicated processing ended,
    If Dir(SourceFile) = "" Then Exit Sub
    implies the the procedure can't find your HTML file.

    If you called the procedure like so:
    ReplaceHTML "c:\Test.html", "copyright 2005", "copyright 2006"
    make sure that c:\Test.html is a valid name and location for your file. Was it misspelled?

    One other thing it could be... are you trying to run ReplaceHTML directly from the code window? If so, the procedure will be called with empty strings for arguments. This will also cause the procedure to bail out at the 'If Dir(SourceFile) = "" Then Exit Sub' line. The procedure has to be called with the 3 string arguments for it to work.

    With all the code pasted exactly as it is listed in my first post, try calling ReplaceHTML with the 3 args
    ReplaceHTML "c:\whatever.html", "<H1>", "<H3>"
    make sure the filename is spelled correctly and it should work.

    If all else fails, attach the Excel sheet to your post, and I'll see what's going wrong.

    Hopefully it'll work for you this time.

  11. #11

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    11

    Re: A Simple One

    mikeyc1204 - Perfect, absolutely bloody perfect. It was the simplest of mistakes. The file is a .htm and I had it down a .html and once I corrected the file extension it worked like a charm - thank you.

    Now all I have to do is to suss out how to pass a variable to a file name and I might be on the road to success.

    If I've got a file name of

    C:\Documents and Settings\Paul\Desktop\New Folder\1.htm

    and I want the "1" to be a value that is passed from cell values on a worksheet, can you give me an idea of the format I would need to use.

    Thanking you in advance again.

    Regards

    -Paul

  12. #12
    Lively Member
    Join Date
    Jun 2005
    Posts
    112

    Re: A Simple One

    If your 1 is in cell A1, you would can call ReplaceHTML in a few ways. Any of these will work:

    VB Code:
    1. Sub Example()
    2.     dim htmlFile as string
    3.    
    4.     htmlFile = Range("A1").Value
    5.     htmlFile = "C:\Documents and Settings\Paul\Desktop\New Folder\" & htmlFile & ".htm"
    6.     ReplaceHTML htmlfile, "<tag1>", "<tag2>"
    7. End Sub
    VB Code:
    1. Sub Example()
    2.     dim htmlFile as string
    3.    
    4.     htmlFile = Cells(1, 1).Value
    5.     htmlFile = "C:\Documents and Settings\Paul\Desktop\New Folder\" & htmlFile & ".htm"
    6.     ReplaceHTML htmlfile, "<tag1>", "<tag2>"
    7. End Sub
    or cut the middle man..
    VB Code:
    1. Sub Example()
    2.     ReplaceHTML "C:\Documents and Settings\Paul\Desktop\New Folder\" & Range("A1").value & ".htm", "<tag1>", "<tag2>"
    3. End Sub

    If you want to edit 100 html files, all listed in column A starting at Row 1, call like this
    VB Code:
    1. Sub Example()
    2.     Dim i as integer
    3.     Dim htmlFile as string
    4.    
    5.     For i = 1 to Range("A1").End(xlDown).Row
    6.         htmlFile = Range("A" & i).Value
    7.         htmlFile = "C:\Documents and Settings\Paul\Desktop\New Folder\" & htmlFile & ".htm"
    8.         ReplaceHTML htmlfile, "<tag1>", "<tag2>"
    9.     Next i
    10. End Sub

  13. #13

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    11

    Re: A Simple One

    Hi mikeyc1204

    I've had a play with the above and it runs like clockwork and I think even I can put together something out of all you hard work.

    I really cannot thank you enough for the help you have given and if you continue to give as much help to other newbies as yoo have to me, you will be an asset to this forum, I am sure.

    My only complaint with the procedure is that the forum only allows me to add one message to your 'Reputation' whereas, if I have the opportunity, I'd have added a lot more - hence this post.

    Thank you, once again, and I'll post again tomorrow once I have put this lot together and made it run.

    Regards

    -Paul

  14. #14
    Lively Member
    Join Date
    Jun 2005
    Posts
    112

    Re: A Simple One

    Glad to be of help sir

  15. #15

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    11

    Re: A Simple One

    mikeyc1204 - Well its taken me a couple of days to get things working how I it to but I'm there now and last night I was able the update over 500 races in about 2 hours that would have taken me about 2 weeks full time before.

    So, frrm that, I'm sure you will realise just how much of a help you have been to me. There is no way I could have written the code that you supplied. The reason I can say that with complete surety is that I still cannot work out how it works!!

    Anyway, thanks again and the next time I get stuck I hope we run into each other.

    Regards

    -Paul

Posting Permissions

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



Click Here to Expand Forum to Full Width