Word Interop and getting page count - vb.net

I know in VBA, within a document, I can get page count using ActiveDocument.Range.Information(wdNumberOfPagesInDocument), But I can't find an equivalent of it in VB.Net using Microsoft.Office.Interop.Word.
Is there, perhaps another way I can attain the quantity of pages in a document?
Public Class Window
'set form level declarations
Dim appPath As String
Dim objWordApp As New Word.Application
Dim objDoc As Word.Document
Dim errorPosition As String
Private Sub Window_Load(ByVal sender As System.Object, e As System.EventArgs) Handles MyBase.Load
objDoc = objWordApp.ActiveDocument
With objDoc
pages = .ActiveDocument.Range.Information(wdNumberOfPagesInDocument)
End With
objDoc = Nothing
End Sub
objWordApp = Nothing
End Class

A way is to get last page number:
lastPageNumber = objDoc.Words.Last.Information[Wd.WdInformation.wdActiveEndPageNumber]

In the Office.Interop.Word Verion 15.0.0
you can try paginate. like this.
objWordApp.Options.Pagination = true;
objWordApp.ActiveDocument.Repagenate();
And Then
DocumentFormat.OpenXml Version 2.12.3
using (WordprocessingDocument document = WordprocessingDocument.Open(filePath, false))
{ document.ExtentedFilePropertuesPart.Properties.Pages.Text }

When you code in VBA, the namespace of the parent application (Word, Excel, etc.) is obvious, so constants such as wdNumberOfPagesInDocument have definitions. With Microsoft.Office.Interop.Word you need to provide the namespace information; for example:
...
With objDoc
pages = .Range.Information(Word.WdInforma‌​tion.wdNumberOfPagesInDocument)
End With
....

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

How to find hyperlinks in a word document?

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())

Get Source Code from Webview (VB for Metro)

I'm Making a Windows Phone's app that I can, From a webview called "DebWeb", get the ClassRoom of a specific class. The DebWeb load the site where is all the classRooms, but I want to make that my App search just my class.
Before I made an app with almost the same objetive (search the Name of a App from the Source Code), but it was made from VB for PC, now I'm working on VB for Metro (or for App Store) and I can't use the same code.
For example, On VB for PC I can use:
Dim EHTML = DebWeb.Document.All.Item(1)
Dim sourceString As String = EHTML.InnerHtml
'Use Regex Match to search from SourceString"
But on VB for Metro it's shows me the " 'Document' is not a member of 'Windows.UI.XAML.Controls.WebView' " error, so I can't get the Source Code from the page and I can't look for the ClassRoom.
I Looked on the MSDN page about Webview but the most close thing that I can do is to get the "DocumentTittle", but not the content.
This is my code, everything "works" except the "Source" variable:
Dim Source = DebWeb.[Control] 'Here is where I need the Control to get the SourceCode
Dim m As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(Source.ToString, _
"DERECHO CONSTITUCIONAL", _
System.Text.RegularExpressions.RegexOptions.IgnoreCase)
Edited with my Entire code:
Private Sub MainPage_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
Dim URL As String = "http://goo.gl/uqohKw"
Me.DebWeb.Navigate(New Uri(URL))
End Sub
Private Sub DebWeb_LoadCompleted(ByVal sender As Object, ByVal e As WebViewNavigationCompletedEventArgs)
LListo.Text = "Listo!"
Dim html As String = DebWeb.InvokeScriptAsync("eval", New String() {"document.documentElement.outerHTML;"}).ToString
Dim Source = html
Dim m As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(Source.ToString, _
"LECTURA CRÍTICA", _
System.Text.RegularExpressions.RegexOptions.IgnoreCase)
If (m.Success) Then
Dim key As String = m.Groups(1).Value
End If
End Sub
Something like this?
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
Try
Dim html As String = Await myWebView.InvokeScriptAsync("eval", New String() {"document.documentElement.outerHTML;"})
Catch ex As Exception
End Try
End Sub
More Info here

How to open dynamic WebBrowser link address in new window form?

I have found the error at href so please help me
Private Sub WebBrowser1_NewWindow(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
Dim thiselement As HtmlElement = WebBrowser1.Document.ActiveElement
Dim targeturl As String = thiselement.GetAttribute("href")
e.Cancel = True
Dim window As New Form1
window.Show()
window.WebBrowser1.Navigate(targeturl)
End Sub
at "href" i have found error like Object reference not set to an instant of object.
my code is in vb.net 2010.
WebBrowser1.Document.ActiveElement is returning Nothing because there is no active element. Therefore when you attempt to use targeturl, you get this error: Object reference not set to an instant of object
Handle the Navigating event. Example:
webBrowser1.Navigating += Function(source, args)
Dim uriClicked = args.Uri
' Create your new form or do whatever you want to do here
End Function

Can I get information from Excel without Microsoft Interop

So, I'm very limited with what I can do with vb at work. The edition of vb.net they set me up with doesn't have the files to use the microsoft.office things, and they won't give me permissions to download them. Is there any other way to pull all the information in column E of a spreadsheet?
Thank you for any insight you might be able to give me.
You can use late binding. From the linked page:
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim objApp As Object
Dim objBook As Object
Dim objBooks As Object
Dim objSheets As Object
Dim objSheet As Object
Dim range As Object
' Instantiate Excel and start a new workbook.
objApp = CreateObject("Excel.Application")
objBooks = objApp.Workbooks
objBook = objBooks.Add
objSheets = objBook.Worksheets
objSheet = objSheets.Item(1)
range = objSheet.Range("A1")
'Set the range value.
range.Value = "Hello, World!"
'Return control of Excel to the user.
objApp.Visible = True
objApp.UserControl = True
End Sub
#KenWhite gave the right answer in regards to Excel without Interop, but all you really needed to do was to change the target framework to access the libraries.
You can do this like so. First of all, right click on your project in Solution Explorer and go down to Properties. At this point, you should see a Project properties page, with a Target framework dropdown box, change this from .NET Framework x.x (Client Profile) to .NET Framework x.x.
Now, you can simply select the References tab in the Properties window, click on Add and add Microsoft.Office.Interop.Excel.