Excel VBA code that searches multiple documents for a text [closed] - vba

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am looking for help, I have a very basic knowledge of VBA and can't perform complex tasks, would someone be able to provide the base of the code and I'll be able to study it and expand on it?
The code should be able to perform the following task:
Using a list of document names in excel, open those documents (word documents) and search the first page for a specific text. If the text exists return TRUE, else FALSE.
Please note the documents are all stored in sub folders.
Thank you,

There are tree different question you have to ask yourself before posting:
I will try to answer it in the way you asked:
1 - Read documents name from the excel file
Dim fileNames as new Collection
For Each name in Range("names")
fileNames.Add name
Next name
2 - Open Word documents
Add the Microsoft Word 12.0 Object Library to your references, and use the Word API to open and read the document.
Dim doc As Word.Document
Set doc = Word.Documents.Open(Filename:=DocumentPath, Visible:=False)
3 - Find text in the active document
Dim myRng as Word.Range
Set myRng = doc.Content
then use the find object for your purpose, take a look at the documentation
https://msdn.microsoft.com/en-us/vba/word-vba/articles/find-object-word

Related

Creating a hyperlink from a file name read from input file to write to a output text file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I would like to convert the text name of each of the several files listed on the input text file (compatible with NotePad) to a hyperlink and would like to use the file name as the text display associated with each hyperlink and write the result with the hyperlinks to an output text file (also compatible with NotePAd).
I would lilke to use either VB Script or VB.Net as the programming language. I cannot find instructions for creating the hyperlink from a character string using either VB Script or VB.Net. Can someone point me to some relevant documentation?
Let's say you have a file named input.txt with this in it
filename1.txt
filename2.txt
filename3.txt
then you could use this code to read the lines and write a new file output.txt
Dim lines = File.ReadAllLines("input.txt")
File.WriteAllLines(
"output.txt",
lines.Select(Function(line) line.Replace(".txt", "") & vbTab & Path.Combine("\\domain\", line)))
which has the filename and a path to the file separated by a tab
filename1 \\domain\filename1.txt
filename2 \\domain\filename2.txt
filename3 \\domain\filename3.txt
This deals with a txt file as input and output. The terms compatible with NotePad and use the file name as the text display associated with each hyperlink are conflicting because notepad will display plain text, not a web page for example. But if you do want something which will render links as titles, you may want to try to write html with HtmlTextWriter
Dim lines = File.ReadAllLines("input.txt")
Using sw = New StringWriter()
Using writer As New HtmlTextWriter(sw)
For Each line In lines
writer.AddAttribute(HtmlTextWriterAttribute.Href, Path.Combine("\\domain\", line))
writer.RenderBeginTag("A")
writer.Write(line.Replace(".txt", ""))
writer.RenderEndTag()
writer.WriteBreak()
Next
End Using
File.WriteAllText("output.html", sw.ToString())
End Using
This would be what you'd see in notepad
filename1<br />
filename2<br />
filename3<br />
and in a web browser

extract data from csv and rename files [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have files in a folder.
Filenames in this folder are available in a .csv file's column B.
Column A contains new file name.
Eg,
Attached pic.
How do I rename files in the folder based on column A using VB.NET ?
First you must read the your CSV.. i suppose that is not too big, so do something like
Dim MyCSV() As String = IO.File.ReadAllLines("c:\my file.csv")
Then go ahead with a "For Each" line in your CSV
For Each Line In MyCSV.ToArray
Next
In this For Each you must split the current line.. and i suppose is a standard CSV so must be the "," (i think)
Dim MySplitLine() As String = Line.Split(","c)
Finally (also in the For Each) you can rename your file with the new name
If IO.File.Exists("c:\folder 1\folder 2\" & MySplitLine(1) & ".extension") Then
FileSystem.Rename("c:\folder 1\folder 2\" & MySplitLine(1) & ".extension",
"c:\folder 9\folder 8\" & MySplitLine(0) & ".extension")
End If
Dont forget to set your extension.

How can I find the destination of a .lnk file in vb.net [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I made a explorer.exe clone, with a treeview and a listview etc.
Now I need to handle clicking a .lnk file, so I need the destination path of a .lnk file..
You can use a WshShell Object:
You create a WshShell object whenever you want to run a program locally, manipulate the contents of the registry, create a shortcut, or access a system folder.
and use its CreateShortcut method:
Creates a new shortcut, or opens an existing shortcut.
The resulting WshShortcut object contains a TargetPath property, which is what you're looking for.
Example:
Dim shell = CreateObject("WScript.Shell")
Dim path = shell.CreateShortcut(path_to_your_link).TargetPath
I'd try;
Public Shared Function GetLnkTarget(lnkPath As String) As String
Dim shl = New Shell32.Shell()
' Move this to class scope
lnkPath = System.IO.Path.GetFullPath(lnkPath)
Dim dir = shl.[NameSpace](System.IO.Path.GetDirectoryName(lnkPath))
Dim itm = dir.Items().Item(System.IO.Path.GetFileName(lnkPath))
Dim lnk = DirectCast(itm.GetLink, Shell32.ShellLinkObject)
Return lnk.Target.Path
End Function
You need to reference a COM object; Microsoft Shell Controls And Automation.

Visual Basic code to detect pendrive

I have this vb code which detects when a pendrive is inserted. Can anyone help me to be able to access the content of the pendrive in my form?? Thank you
Use IO.Directory.GetDirectories() and IO.Directory.GetFiles() from the path of the pendrive. You can see the MSDN documentation here and here, respectively.
Improve your question in order to have more specific answers.
EDIT:
Adding the file names into a ListBox:
Dim allFiles() As String = Directory.GetFiles(penDrivePath)
For Each file As String in allFiles
ListBox1.Items.Add(file)
Next

Read and write to the registry with VB.NET [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 months ago.
Improve this question
I made a game and I would like to store the high score and other values in the windows registry. It's made in VB.NET. Could someone give me a sample code example of simple reading and writing to the registry.
Thanks
You must open Registry sub Key before read or write. Then you can read or write
Dim regKey As RegistryKey
Dim Value As Object
regKey =My.Computer.Registry.CurrentUser.OpenSubKey("HKEY_CURRENT_USER\Software\VB_and_VBA_Program_Settings", True)
'Here u can read value of AppName
Value = regKey.GetValue("AppName", "Default Value")
'Or u can write the value
value=regkey.setValue("AppName", "myApp")
regKey.Close()
Simply...
Imports Microsoft.VisualBasic
Dim s As String
SaveSetting("(AppName)", "(SectionName)", "(Key)", "(Your Value)")
s = GetSetting("(AppName)", "(SectionName)", "(Key)", "(Default Value)")
Replace (AppName), (SectionName), (Key) with appropriate values. The data will be saved in HKEY_CURRENT_USER\Software\VB and VBA Program Settings\(AppName)
http://www.vbdotnetheaven.com/UploadFile/mahesh/WindowsRegistry04262005045814AM/WindowsRegistry.aspx
http://msdn.microsoft.com/en-us/library/microsoft.win32.registry.aspx
I'm more comfortable with C#, but it's pretty straightforward with VB.NET too. Here's an example of how to write to the registry, and another example of how to read from the registry. Don't forget to import the Microsoft.Win32 namespace.
You can use registry.getvalue and registry.setvalue. Here are a couple of examples used for default file types:
Registry.GetValue("HKEY_CURRENT_USER\software\classes" & "\" & fileFormatExt(i), "", "error")
Registry.SetValue("HKEY_CURRENT_USER\software\classes\" & FileType, "", appTag) ' set new value, overwrite any other, creates key if not there.