Visual Studio 2010,
Visual Basic .NET
I have been working on a program that has a JSON generated table of contents and a WebBrowser control that only displays the html we have given them. Now I need to give them the ability to search for strings inside the html. Not just the currently opened html page in the WebBrowser object, but the entire group of html files which are in various folder.
There is a Main folder with several folders in it. Each of those folders only have one folder in it. But inside that one folder is several html files. (Not sure if knowing the folder structure will help at all)
I have no code for this since I have never done anything like this before, just wanting someone to point me in the right direction.
Since html files are just text files you can use this approach.
To create this example I created 2 directories in my c:\temp directory, I named them InHere and ChildofInHere. I put ChildofInHere in InHere obviously. I then added a file called SomeFile.html and just put the word "Cheese" in it. Here is the code I then created and ran against it.
Private _TextFound As Boolean = False
Private Sub Button10_Click(sender As System.Object, e As System.EventArgs) Handles Button10.Click
FindTheText("C:\temp\InHere", "cheese")
MessageBox.Show(_TextFound)
End Sub
Private Sub FindTheText(sDirToLookIn As String, sTextToFind As String)
If IO.Directory.Exists(sDirToLookIn) Then
Dim di As New IO.DirectoryInfo(sDirToLookIn)
For Each dii As IO.DirectoryInfo In di.GetDirectories
FindTheText(dii.FullName, sTextToFind)
Next
If IO.File.Exists(sDirToLookIn & "\SomeFile.html") Then
If IO.File.OpenText(sDirToLookIn & "\SomeFile.html").ReadToEnd.Contains(sTextToFind) Then
_TextFound = True
End If
End If
End If
End Sub
.NET makes working with files and directories so easy. Hope this helps.
Related
I am having trouble with getting the HTML Application (MeowcatSoftware Launcher Demo.HTA on GitHub) to open a target application, for example, MultiToolv0.2.exe. Is there a way to open the target applications such as the MultiTool using VBScript besides using Wscript.shell objects, which has been causing most of my problems?
I have tried the following, which didn't work:
Sub RunProgram
Set objShell = CreateObject(“Wscript.Shell”)
objShell.Run “notepad.exe c:\script\test.txt”
End Sub
(From 'Hey, Scripting Guy" Blog Post)
I played around a little with it but couldn't figure out how to achieve my goal. The blog post also mentioned using a Windows Shell object instead of Wscript.shell, but it looks like from the example that the Windows Shell object method was for opening a file using another program, and I just want it to simply open the target application. How do I open a program using VBScript within an HTA Application?
In vbscript this should work : You should using quote (") not like you posted in your question (“) and (”)
Call RunProgram()
Sub RunProgram
Set objShell = CreateObject("Wscript.Shell")
objShell.Run "notepad.exe c:\script\test.txt"
End Sub
Edit: See my comment below for a partial solution.
Edit 2: I found an adequate solution for closing the VBA editor but just want to see if anyone knows how to make it fully invisible the whole time. What I have found out works for my needs but I will leave this thread open for anyone who wants to elaborate on another method or expand on mine.
Original Post: I have a function that creates tables, queries, and forms. For the forms, it copies a template form and calls a function that replaces the forms VBA code dynamically. The below function I created works great, however, if I do not have the class object form open in the editor, I get the Run-Time error '2516': "Microsoft Access cannot find the module 'SPCInputFormVBA.', where SPCInputFormVBA is a variable for the class objects name. To further elaborate on the behavior, if I have the editor closed, as long as the module or class object is open in the editor, it will still work. I would like to be able to activate the corresponding Form_xyz object in the editor without the editor opening so that I can use this function to do a bunch of stuff to the to it. I am using a template form because there are a lot of things nested in it and has a lot going on. The form I can change the record source and various other things in it just fine, but the VBA portion is elusive to me so far. I thought opening the object would be easy but I am having a lot of trouble and cannot find a way to describe my issue that leads to me finding a solution.
Public Function InputFormVBA(SPCInputFormVBA)
DoCmd.OpenModule (SPCInputFormVBA)
Dim i As Integer
With Application.Modules(SPCInputFormVBA)
For i = 1 To .CountOfLines
If InStr(.Lines(i, 1), "TempTable") > 0 Then
' .ReplaceLine i, " If DCount( ""serial"", """ & tblName & """, _"
End If
'If Instr(.Lines(i, 1), "
Next i
End With
End Function
The closest I have come is trying various things with this function in order to help me understand how Access will react to different potential solutions I find online:
Sub PrintOpenModuleNames()
Dim i As Integer
Dim modOpenModules As Modules
Set modOpenModules = Application.Modules
For i = 0 To modOpenModules.Count - 1
Debug.Print modOpenModules(i).Name
'DoCmd.OpenModule (modOpenModules(i).Name)
Next
End Sub
The 'DoCmd.OpenModule (modOpenModules(i).Name) is commented out in this example and will open things, but it only sees things that are open already which doesn't help me. I do understand that there are different types of Modules, but I am not sure how to distinguish and the documentation online explains a general difference but doesn't reveal any way to reach out to the Class Object unless it is open in the editor already. Hoping someone can help or even correct my terminology if it is off and steer me to an existing solution elsewhere on the site.
You can use the VBE object model to access your form's code module without opening it in the VB Editor. And with that approach, the VB Editor window does not need to be visible or open. If the VB Editor window is not open, accessing the module this way will not open it. So I think that satisfies the main objective of your question.
It's possible to use VBE with late binding but, since you're not familiar with it, you'll likely want to use early binding instead. If so, add Microsoft Visual Basic for Applications Extensibility to your project's references.
Here is a very minimal procedure which only shows you how to reference a code module by name and print 2 of the module's properties.
Public Sub InputFormVBA(ByVal SPCInputFormVBA As String)
Dim objModule As CodeModule
Set objModule = Application.VBE.VBProjects(1).VBComponents(SPCInputFormVBA).CodeModule
With objModule
Debug.Print .CountOfDeclarationLines
Debug.Print .CountOfLines
End With
End Sub
If your database contains only one VBA project, VBProjects(1) will reference it. But if the db contains more than one VBA project, you may need to give VBProjects() a different number. I presume you'll figure that out pretty darn quick. :-)
A CodeModule object has methods you should find useful, including:
DeleteLines; Find; InsertLines; and ReplaceLine. However I don't really know what you want to do with the module's code, so will just leave it at that.
I've been using VBA and available modules to integrate the browse for directory and browse for file functionalities in two different MS Access projects. Here are the example files where I took the functionalities from:
Browse to folder: http://www.lebans.com/callbackbrowser.htm
Browse to file: http://www.access-programmers.co.uk/forums/showthread.php?t=97787
In both my .mdb projects, I have tried to browse for a file/folder, then output the address to a hyperlink textbox, linked to a hyperlink field in a table.
It has mostly worked, but unfortunately, with both these functionalities, the hyperlink stops working... The output to the textbox and to the table seems perfect, but when I click on the hyperlink, it does not take me to Windows Explorer.
To make the hyperlink work, I have to double click on the textbox, (which then lets me edit the field) remove the last letter, then type it back again, and tab out. Then, when I click, Windows Explorer opens normally, as a hyperlink should.
What is the solution? I've already tried:
DoCmd.RunCommand acCmdSaveRecord
Without success...
Thanks for your help
If I understand you correctly, you want to open an explorer window for a specified directory set in the textbox?
If so, you'll need to use the textbox on-click event, something like this:
Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Shell "C:\windows\explorer.exe """ & TextBox1.Value & "", vbNormalFocus
End Sub
Or, you can use the double-click event:
Private Sub TextBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Shell "C:\windows\explorer.exe """ & TextBox1.Value & "", vbNormalFocus
End Sub
By the way, I could only test this in Excel - I don't have Access installed, but it should be the same.
I found the answer to my question on stackoverflow:
Access - Hyperlinks Aren't Linking
Turns out you need to concatenate #'s before and after the hyperlink address for it to work properly as a hyperlink.
Visual Basic 2010
I try this
myWebBrowser1.Navigate(My.Resources.HomePage)
but it fail to execute
it show "Value does not fall within the expected range."
PS : HomePage is a .html file, I add it to the project resources.(embedded)
Please help me ~
Something like this:
VB code:
Private Sub DisplayHtml()
Me.WebBrowser1.Navigate("about:blank")
Me.WebBrowser1.Document.Write(String.Empty)
Me.WebBrowser1.DocumentText = My.Resources._Readme
End Sub
you can use this as reference to your problem:
http://weblogs.asp.net/gunnarpeipman/archive/2009/08/15/displaying-custom-html-in-webbrowser-control.aspx
I am trying to develop a simple tool in VB.NET that, within a loop, navigates to a website and save the page as an HTML document.
I can set up the loop easily as the pages are numbered sequentially.
www.example.com/pages/1.html
www.example.com/pages/2.html
www.example.com/pages/3.html
www.example.com/pages/4.html
Where I am having trouble is with finding a method to save the actual page. I was going to utilize a series of SendKeys to Alt, File, Saves As, Enter, etc.. but I figured there had to be some sort of object/method that could be used to do this more straight forward.
I've enabled the COM Internet Controls Reference and declared and new SHDoc.Vw.InternetExplorer and am able to programatically open the browser in a new window and navigate to the desired page(s). I've search online for a solution to the Save issue but have been unsuccessful. Does anyone have any ideas?
Dim baseUrl As String = "http://www.example.com/pages/{0}.html"
Dim basePath As String = "C:\some\path{0}.html"
Using ws As New System.Net.WebClient()
ForEach i As Integer In Enumerable.Range(1,4)
wc.DownloadFile(String.Format(baseUrl, i), string.Format(basePath, i))
Next i
End Using
If you have a lot of these want to get a little fancy, you can even use the DownloadFileAsync() method to queue up several of thses at once.