How to find hyperlinks in a word document? - vb.net

I'm working on a VB 2015 and I have a problem.
I want to find hyperlinks in a word document containing a paragraph with several words containing hyperlinks. How can I find all the hyperlinks and list them in a text file or textbox?
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim wa As Microsoft.Office.Interop.Word.Application
Dim wd As Microsoft.Office.Interop.Word.Document
Dim wp As Microsoft.Office.Interop.Word.Paragraph
wa = CreateObject("Word.Application")
wa.Visible = False
wd = wa.Documents.Add
wp = wd.Content.Paragraphs.Add
wp.Range.Paste()
wd.SaveAs("F:\sample.docx")
Dim colHyperlinks As String = wd.Hyperlinks.ToString
For Each objHyperlink In colHyperlinks
TextBox1.Text = objHyperlink.TextToDisplay
Next
wa.Quit()
End Sub
End Class

As mentioned in the comment's above, you are declaring a string type on a Word.Hyperlinks collections object. Therefore you would only ever get one string and not any others. Please see code below, comment's on what it does...
Note: Code tried and tested
'returns a collection of links, not a string
Dim colHyperlinks As Word.Hyperlinks = wd.Hyperlinks()
This code below is just a LINQ statement to get all hyperlinks into a List(Of String). You can loop through if you want and then add them to the Textbox if you wish...
'get all the hyperlinks
Dim arr As List(Of String) = (From hl As Word.Hyperlink In colHyperlinks Select hl.TextToDisplay).ToList()
'show the url's
TextBox1.Text = String.Join(Environment.NewLine, arr)
Or in just one line if you wish...
TextBox1.Text = String.Join(Environment.NewLine, (From hl As Word.Hyperlink In colHyperlinks Select hl.TextToDisplay).ToList())

Related

I am trying to use clipboard object in class project of vb.net but I can't seem to be able to use it

I am trying to get clipboard text in vb.net class project but the clipboard object is not working
Dim clipText As String = String.Empty
If Clipboard.ContainsText Then
clipText = Clipboard.GetText()
End If
I am using this code but its giving me error
Dim clipboardText As String = System.Windows.Forms.Clipboard.GetText()
I am trying to use this but system. Windows doesn't have .forms but it has .input and .others what am I missing I tried referencing it from the references but it doesn't seem to work either
This is from a little project where i load code from a SQLite DB into a RichTextBox then click a button to copy that code to the clip board.
Private Sub btnCopySelCode_Click(sender As Object, e As EventArgs) Handles btnCopySelCode.Click
CopySelected()
End Sub
Public Sub CopySelected()
Dim start = rtbViewCode.SelectionStart
Dim substring = rtbViewCode.Text.Substring(0, start)
Dim words = substring.Split(New String() {" ", vbCrLf}, StringSplitOptions.None)
Dim gvW = rtbViewCode.SelectedText
Dim count = gvW.Length
If count <= 0 Then
gvalertType = "14"
frmAlert.ShowDialog()
rtbViewCode.Focus()
Return
End If
Clipboard.SetText(gvW)
ansQ()
End Sub

Trouble with getting value from a website - vb.net

i'm not searching to do something complicated , i just want to retrieve certain titles from a website , the first button was just for a test ... The thing is , even the "lala" text isn't showing up which means it doesn't enter in the loop in the first place ...
Public Class Form1
Function ElementsByClass(document As HtmlDocument, classname As String)
Dim coll As New Collection
For Each elem As HtmlElement In document.All
If elem.GetAttribute("appcenter").ToLower.Split(" ").Contains(classname.ToLower) Then
coll.Add(elem)
End If
Next
Return coll
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim wb As New System.Net.WebClient
wb.Headers.Add("user-agent", "Only a test!")
Dim sourceString As String = wb.DownloadString("http://www.ign.com/games/upcoming")
RichTextBox1.Text = sourceString
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim elementss As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("div")
For Each pElem As HtmlElement In elementss
If pElem.GetAttribute("class") = "item-title" Then
RichTextBox1.Text = "lala"
RichTextBox1.Text = pElem.InnerHtml
End If
Next
End Sub
End Class
Ok, from what I can tell you are after the titles of each new upcoming game.Something like this should do the trick for you.I would recommend that for larger web scraps that you use the HTML Agility PackHowever since you are only wanting a few strings, this solution should be ok for you.
Imports System.Net
Imports System.Text.RegularExpressions
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim websiteURL As String = "http://www.ign.com/games/upcoming"
getTiles(websiteURL) 'where you access this is up to you
End Sub
Private Sub getTiles(website As String)
ListBox1.Items.Clear() 'Clear old results or any errors
Dim tempTitles As New List(Of String)()
Dim webClient As New WebClient()
webClient.Headers.Add("user-agent", "null")
Try 'If the website happens to go offline, at least your application wont crash.
Dim content As String = webClient.DownloadString(website)
Dim pattern As String = "alt=""(?<Data>[^>]*)""/>"
For Each title As Match In (New Regex(pattern).Matches(content)) 'Since you are only pulling a few strings, I thought a regex would be better.
tempTitles.Add(title.Groups("Data").Value)
Next
Dim titles = tempTitles.Distinct().ToArray() 'remove duplicate titles
For Each title As String In titles
ListBox1.Items.Add(title) 'what you do with the values from here is up to you.
Next
If titles.Count() = 0 Then
ListBox1.Items.Add("Nothing Found")
End If
Catch ex As Exception
ListBox1.Items.Add(ex.Message)
Return
End Try
End Sub
I have written some comments to go along with the code to answer any questions you might have.If i have missed something out, feel free to leave a comment below, Happy Coding
I'm sorry but I suck at explaining these things so please analyze the regex in the site I mentioned in the comment earlier.
The site you specified lists the games like this:
<a class="product_spot " href="/browse?nav=16k-3-rime,28zu0" data-date="05/06/2017"><img src="/gs/pages/landing/upcoming-video-games/images/223x120_rime.jpg"><p>RiME<br><br><span>05/06/2017</span></p></a>
<a class="product_spot " href="/browse/games?nav=16k-3-the+surge,28zu0,13ffff2418" data-date="05/16/2017"><img src="/gs/pages/landing/upcoming-video-games/images/223x120_thesurge.jpg"><p>The Surge<br><br><span>05/16/2017</span></p></a>
So this regex will match that.
<a class=.product_spot\s.\shref=.(?:.+?)\sdata-date=.(?:.+?)><img\ssrc=(?:.+?)><p>(.+?)<br><br><span>(?:.+?)<\/span><\/p><\/a>
Imports System.Net
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
Dim wc As New WebClient
Dim input As String = wc.DownloadString("http://www.gamestop.com/collection/upcoming-video-games")
Dim games As New List(Of String)
Dim matchCollection As MatchCollection = Regex.Matches(input, "<a class=.product_spot\s.\shref=.(?:.+?)\sdata-date=.(?:.+?)><img\ssrc=(?:.+?)><p>(.+?)<br><br><span>(.+?)<\/span><\/p><\/a>")
For Each item As Match In matchCollection
games.Add(item.Groups(1).Value.ToString)
Next
For Each item As String In games
Console.WriteLine(item)
Next
Console.ReadLine()
End Sub
End Module
Output:
Dead Island 2
Final Fantasy XV
De-Formers
Injustice 2
...
Killing Floor 2
Tales of Berseria
Nintendo Switch
Mass Effect Andromeda
MLB The Show 17
Has-Been Heroes
Ride 2
...
..
.

Advanced Replacement Freeze

Basically I'm creating a tool which while is looking through lines of "file.txt" to replace a word from a textbox's content with that line if the line is containing that word.
Basically if the line is: pizza-cheese-potatoes, all the words containing "pizza" or "cheese" or "potatoes" to be replaced with "pizza-cheese-potatoes"
Here is what I have until now. But it freeze and I don't really know why. Please help me. :)
Dim PATH As String = "C:\test.txt"
Sub Repl(x As String)
For Each line As String In File.ReadLines(PATH)
Dim myList = New List(Of String)(line.Split("|"c))
For Each item As String In myList
If x Is item Then
TextBox1.Text.Replace(x, line)
End If
Next
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each word As String In TextBox1.Text.Split(" "c)
Repl(word)
Next
End Sub
Thank you in advance!
Try doing it like this instead.
Public Sub DoWork()
Dim lines = IO.File.ReadAllLines(PATH)
For Each line In lines
Dim myList = New List(Of String)(line.Split("-"c))
For Each item In myList
If TextBox1.Text.Contains(item) Then
TextBox1.Text = TextBox1.Text.Replace(item, line)
End If
Next
Next
End Sub
Your reader reads the file multiple times which is massively inefficient. This code reads it once, then we just loop over each line with no need to worry about exit conditions etc.
However it's not really doing anything useful as you're not pausing after each line so you'll only be able to review the final one.
There is also no need to test if the textbox contains the text, just simply do a replace then you only search the text once.
For Each item In myList
TextBox1.Text = TextBox1.Text.Replace(item, line)
Next
-------- edit --------
To fix the issue with replacing the word you've already replaced you could try using a replacement placeholder.
For Each item In myList
TextBox1.Text = TextBox1.Text.Replace(item, "#")
Next
TextBox1.Text = TextBox1.Text.Replace("#", line)
---------- edit 2 ------------------
You want to try and build up a new string, word by word, instead of replacing the text in the textbox. This is so that words you've substituted already don't get converted.
Function ReplaceWord(word As String, lines As String())
For Each line As String In File.ReadLines(PATH)
Dim myList = New List(Of String)(line.Split("|"c))
For Each item As String In myList
If word = item Then
Return line
End If
Next
Next
Return word
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim result As New System.Text.StringBuilder
Dim lines = File.ReadLines(PATH)
For Each word As String In TextBox1.Text
result.Append(ReplaceWord(word, lines)).Append(" ")
Next
Textbox1.Text = result.ToString()
End Sub

Carry objreader file being read over from form to form

Okay so I'm making a multiple-choice quiz game for an assignment and I have a form for the list of categories and the actual questions. Because I'm quite new at coding and don't really have a whole lot of time to totally restructure my code in a way that I probably wouldn't understand, instead of adding all the buttons to one buttons "Handles" I just made a sub for each click event.
This is an example of one of these subs:
Public Sub btnMusic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMusic.Click
Questions.Show()
Me.Close()
Dim objReader As New System.IO.StreamReader("Music.txt")
End Sub
My text is read by the following structure:
Structure QuizQ
Dim Q As String
Dim A As String
Dim B As String
Dim C As String
Dim D As String
Dim Correct As String
End Structure
I then try to read the lines according to the structure with:
Dim I As Integer
For I = 0 To 5
MyQ(I).Q = objReader.ReadLine
MyQ(I).A = objReader.ReadLine
MyQ(I).B = objReader.ReadLine
MyQ(I).C = objReader.ReadLine
MyQ(I).D = objReader.ReadLine
MyQ(I).Correct = objReader.ReadLine
Next I
and then set the text for all of the buttons to the possible answers like so:
lblQuestion.Text = MyQ(qNum).Q
btnA.Text = MyQ(qNum).A
btnB.Text = MyQ(qNum).B
btnC.Text = MyQ(qNum).C
BtnD.Text = MyQ(qNum).D
But at this point the text isn't displayed on the buttons or where the question should be. I really have no idea what I could do from this point so any help really would be appreciated.
Thank you in advance!

Autocomplete for single word in datagridview

I would need to implement a autocomplete feature in a datagridview cell. I would need it to work on a word by word basis, like it is in the SMS app on android. After I type a whitespace it should start looking for the word I am typing and propose it based on the other words i have already used inside the same Datagridview.
Its more a word suggestion, that if i hit tab autocompletes that word for me.
Is this possible? I know how to do it based on the entire cell, but have no clue on how to do it based on the single word. (like Google)
Thanks
EDIT:
So far I have this. The concept is working, but I need to update the list each time that a key is pressed. Any help on this?
Private Sub DataGridView2_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView2.EditingControlShowing
wrdlst.Add("arabia")
wrdlst.Add("burundi")
wrdlst.Add("closed")
wrdlst.Add("afganistan")
wrdlst.Add("door")
wrdlst.Add("banana")
wrdlst.Add("apple")
Dim basestring As String = Nothing
basestring = CStr(DataGridView2.CurrentCell.Value)
If Not IsNothing(basestring) Then
Dim lastword As String
Dim lastspaceindex As Integer = basestring.LastIndexOf(" ") + 1 '''+1 to get index after whitespace and compensate for -1 result
lastword = basestring.Substring(lastspaceindex)
Dim ItemCode As TextBox = TryCast(e.Control, TextBox)
If ItemCode IsNot Nothing Then
ItemCode.AutoCompleteMode = AutoCompleteMode.SuggestAppend
'ItemCode.AutoCompleteCustomSource = wrdlst
For Each element As String In wrdlst
If element.StartsWith(lastword) Then
ItemCode.AutoCompleteCustomSource.Add(basestring.Substring(0, lastspaceindex) & element)
End If
Next
ItemCode.AutoCompleteSource = AutoCompleteSource.CustomSource
End If
End If
End Sub
Private Sub DataGridView2_KeyUp(sender As Object, e As KeyEventArgs) Handles DataGridView2.KeyUp
??????????????????????????????????????????
End Sub