472,951 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,951 software developers and data experts.

convert to VB .Net code - Highlight text using mshtml.IMarkupServices

I am trying to convert the following code to VB .Net, I still have some
gaps (the lines that are marked with (*))
and also I need an ending condition for the while loop.

any help would be appreciated.

Thanks. Atara.

-------------------------
Original code:
-------------------------

void highLight(IHTMLDocumentPtr doc, const CString& highlightText, bool
bHighLight, const CString& highlightAttr)
{
//Usage: highLight(pHTMLDoc, _T("google"), _T("COLOR=#0000ff "));

CComQIPtr<IMarkupServices> pMS;
CComQIPtr<IMarkupContainer> pMC;
CComPtr<IMarkupPointer> ptrBegin, ptrEnd;

_bstr_t attr(highlightAttr);
_bstr_t textToFind(highlightText);

pMS = doc;
pMC = doc;
if(pMS && pMC)
{
pMS->CreateMarkupPointer(&ptrBegin);
pMS->CreateMarkupPointer(&ptrEnd);

ptrBegin->SetGravity(POINTER_GRAVITY_Right);
ptrEnd->SetGravity(POINTER_GRAVITY_Left); // by default

ptrBegin->MoveToContainer(pMC, TRUE);
ptrEnd->MoveToContainer(pMC, FALSE);

while(TRUE)
{ // Find text
HRESULT hr = ptrBegin->FindText(textToFind, 0, ptrEnd, NULL);

if (S_FALSE == hr) break; // did not find the text

IHTMLElementPtr pFontEl;
hr = pMS->CreateElement(TAGID_FONT, attr, &pFontEl); // create
FONT element with attributes for selection
hr = pMS->InsertElement(pFontEl, ptrBegin, ptrEnd); // Insert
created element to context
ptrBegin->MoveToPointer(ptrEnd); // Continue
searching
}
}
}

-------------------------
Original code till here.
-------------------------

-------------------------
VB .Net code:
-------------------------

Dim pMS As mshtml.IMarkupServices = CType(Me.Document,
mshtml.IMarkupServices)
Dim pMC As mshtml.IMarkupContainer = CType(Me.Document,
mshtml.IMarkupContainer)
Dim ptrBegin, ptrEnd As mshtml.IMarkupPointer

Dim flags As UInt32 = Convert.ToUInt32(0)
Dim newAttr As String = " COLOR=#0000ff "

If (Not (pMS Is Nothing) And Not (pMC Is Nothing)) Then
pMS.CreateMarkupPointer(ptrBegin)
pMS.CreateMarkupPointer(ptrEnd)
ptrBegin.SetGravity(mshtml._POINTER_GRAVITY.POINTE R_GRAVITY_Right)
ptrEnd.SetGravity(mshtml._POINTER_GRAVITY.POINTER_ GRAVITY_Left)

ptrBegin.MoveToContainer(pMC, True) ' (*) Option Strict On
disallows implicit conversions from 'Boolean' to 'Integer'.
ptrEnd.MoveToContainer(pMC, False) ' (*) Option Strict On
disallows implicit conversions from 'Boolean' to 'Integer'.

Dim hr As IntPtr ' HRESULT
Dim pNewElem As mshtml.IHTMLElement ' IHTMLElementPtr

While (True) ' (*) needs break condition instead of hr, since
findText does not return value!
hr = ptrBegin.findText(searchStr, flags, ptrEnd, Nothing)
' (*) searchStr: Value of type 'String' cannot be converted to
'System.UInt16'

If (hr = S_FALSE) Then Exit While
' (*) S_FALSE is not declared,

hr = pMS.createElement(mshtml._ELEMENT_TAG_ID.TAGID_FON T,
newAttr, pNewElem) ' (*) newAttr: Value of type 'String' cannot be
converted to 'System.UInt16'
pMS.InsertElement(pNewElem, ptrBegin, ptrEnd)
ptrBegin.MoveToPointer(ptrEnd)
End While
End If
-------------------------
VB .Net code till here.
-------------------------
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #1
5 10821
Hello...
If you are trying to do highlighting of words in an mshtml document such
as find and highlight all instances of "VB.NET" or just highlight the
selected text then I have code I have written to do this, along with the
ability to remove all the current highlights. Asking because I am not
sure what the uses are of IMarkupServices.

If this is what you are referring to let me know and I'll post the code
as a reply to this thread.

Thanks,

Marc Cramer

Atara wrote:
I am trying to convert the following code to VB .Net, I still have some
gaps (the lines that are marked with (*))
and also I need an ending condition for the while loop.

any help would be appreciated.

Thanks. Atara.

-------------------------
Original code:
-------------------------

void highLight(IHTMLDocumentPtr doc, const CString& highlightText, bool
bHighLight, const CString& highlightAttr)
{
//Usage: highLight(pHTMLDoc, _T("google"), _T("COLOR=#0000ff "));

CComQIPtr<IMarkupServices> pMS;
CComQIPtr<IMarkupContainer> pMC;
CComPtr<IMarkupPointer> ptrBegin, ptrEnd;

_bstr_t attr(highlightAttr);
_bstr_t textToFind(highlightText);

pMS = doc;
pMC = doc;
if(pMS && pMC)
{
pMS->CreateMarkupPointer(&ptrBegin);
pMS->CreateMarkupPointer(&ptrEnd);

ptrBegin->SetGravity(POINTER_GRAVITY_Right);
ptrEnd->SetGravity(POINTER_GRAVITY_Left); // by default

ptrBegin->MoveToContainer(pMC, TRUE);
ptrEnd->MoveToContainer(pMC, FALSE);

while(TRUE)
{ // Find text
HRESULT hr = ptrBegin->FindText(textToFind, 0, ptrEnd, NULL);

if (S_FALSE == hr) break; // did not find the text

IHTMLElementPtr pFontEl;
hr = pMS->CreateElement(TAGID_FONT, attr, &pFontEl); // create
FONT element with attributes for selection
hr = pMS->InsertElement(pFontEl, ptrBegin, ptrEnd); // Insert
created element to context
ptrBegin->MoveToPointer(ptrEnd); // Continue
searching
}
}
}

-------------------------
Original code till here.
-------------------------

-------------------------
VB .Net code:
-------------------------

Dim pMS As mshtml.IMarkupServices = CType(Me.Document,
mshtml.IMarkupServices)
Dim pMC As mshtml.IMarkupContainer = CType(Me.Document,
mshtml.IMarkupContainer)
Dim ptrBegin, ptrEnd As mshtml.IMarkupPointer

Dim flags As UInt32 = Convert.ToUInt32(0)
Dim newAttr As String = " COLOR=#0000ff "

If (Not (pMS Is Nothing) And Not (pMC Is Nothing)) Then
pMS.CreateMarkupPointer(ptrBegin)
pMS.CreateMarkupPointer(ptrEnd)
ptrBegin.SetGravity(mshtml._POINTER_GRAVITY.POINTE R_GRAVITY_Right)
ptrEnd.SetGravity(mshtml._POINTER_GRAVITY.POINTER_ GRAVITY_Left)

ptrBegin.MoveToContainer(pMC, True) ' (*) Option Strict On
disallows implicit conversions from 'Boolean' to 'Integer'.
ptrEnd.MoveToContainer(pMC, False) ' (*) Option Strict On
disallows implicit conversions from 'Boolean' to 'Integer'.

Dim hr As IntPtr ' HRESULT
Dim pNewElem As mshtml.IHTMLElement ' IHTMLElementPtr

While (True) ' (*) needs break condition instead of hr, since
findText does not return value!
hr = ptrBegin.findText(searchStr, flags, ptrEnd, Nothing)
' (*) searchStr: Value of type 'String' cannot be converted to
'System.UInt16'

If (hr = S_FALSE) Then Exit While
' (*) S_FALSE is not declared,

hr = pMS.createElement(mshtml._ELEMENT_TAG_ID.TAGID_FON T,
newAttr, pNewElem) ' (*) newAttr: Value of type 'String' cannot be
converted to 'System.UInt16'
pMS.InsertElement(pNewElem, ptrBegin, ptrEnd)
ptrBegin.MoveToPointer(ptrEnd)
End While
End If
-------------------------
VB .Net code till here.
-------------------------
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 21 '05 #2
Yes, I am trying to highlight certain text strings.

I will appreciate any code sample.
Thanks
Atara

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #3
I would be interested to see this code. Could you post it please?

--

Bob

--------------------------------------
I'll have a B please Bob.

"Marc Cramer" <cr*******@comcast.net> wrote in message
news:7P********************@comcast.com...
Hello...
If you are trying to do highlighting of words in an mshtml document such
as find and highlight all instances of "VB.NET" or just highlight the
selected text then I have code I have written to do this, along with the
ability to remove all the current highlights. Asking because I am not
sure what the uses are of IMarkupServices.

If this is what you are referring to let me know and I'll post the code as
a reply to this thread.

Thanks,

Marc Cramer

Atara wrote:
I am trying to convert the following code to VB .Net, I still have some
gaps (the lines that are marked with (*))
and also I need an ending condition for the while loop.

any help would be appreciated.

Thanks. Atara.

-------------------------
Original code:
-------------------------

void highLight(IHTMLDocumentPtr doc, const CString& highlightText, bool
bHighLight, const CString& highlightAttr)
{
//Usage: highLight(pHTMLDoc, _T("google"), _T("COLOR=#0000ff "));

CComQIPtr<IMarkupServices> pMS;
CComQIPtr<IMarkupContainer> pMC;
CComPtr<IMarkupPointer> ptrBegin, ptrEnd;

_bstr_t attr(highlightAttr);
_bstr_t textToFind(highlightText);

pMS = doc;
pMC = doc;
if(pMS && pMC)
{
pMS->CreateMarkupPointer(&ptrBegin);
pMS->CreateMarkupPointer(&ptrEnd);

ptrBegin->SetGravity(POINTER_GRAVITY_Right);
ptrEnd->SetGravity(POINTER_GRAVITY_Left); // by default

ptrBegin->MoveToContainer(pMC, TRUE);
ptrEnd->MoveToContainer(pMC, FALSE);

while(TRUE)
{ // Find text
HRESULT hr = ptrBegin->FindText(textToFind, 0, ptrEnd, NULL);

if (S_FALSE == hr) break; // did not find the text

IHTMLElementPtr pFontEl;
hr = pMS->CreateElement(TAGID_FONT, attr, &pFontEl); // create
FONT element with attributes for selection
hr = pMS->InsertElement(pFontEl, ptrBegin, ptrEnd); // Insert
created element to context
ptrBegin->MoveToPointer(ptrEnd); // Continue
searching
}
}
}

-------------------------
Original code till here.
-------------------------

-------------------------
VB .Net code:
-------------------------

Dim pMS As mshtml.IMarkupServices = CType(Me.Document,
mshtml.IMarkupServices)
Dim pMC As mshtml.IMarkupContainer = CType(Me.Document,
mshtml.IMarkupContainer)
Dim ptrBegin, ptrEnd As mshtml.IMarkupPointer

Dim flags As UInt32 = Convert.ToUInt32(0) Dim newAttr As String = "
COLOR=#0000ff "

If (Not (pMS Is Nothing) And Not (pMC Is Nothing)) Then
pMS.CreateMarkupPointer(ptrBegin)
pMS.CreateMarkupPointer(ptrEnd)
ptrBegin.SetGravity(mshtml._POINTER_GRAVITY.POINTE R_GRAVITY_Right)
ptrEnd.SetGravity(mshtml._POINTER_GRAVITY.POINTER_ GRAVITY_Left)
ptrBegin.MoveToContainer(pMC, True) ' (*) Option Strict On
disallows implicit conversions from 'Boolean' to 'Integer'.
ptrEnd.MoveToContainer(pMC, False) ' (*) Option Strict On
disallows implicit conversions from 'Boolean' to 'Integer'.
Dim hr As IntPtr ' HRESULT
Dim pNewElem As mshtml.IHTMLElement ' IHTMLElementPtr

While (True) ' (*) needs break condition instead of hr, since
findText does not return value!
hr = ptrBegin.findText(searchStr, flags, ptrEnd, Nothing)
' (*) searchStr: Value of type 'String' cannot be converted to
'System.UInt16'

If (hr = S_FALSE) Then Exit While
' (*) S_FALSE is not declared,

hr = pMS.createElement(mshtml._ELEMENT_TAG_ID.TAGID_FON T,
newAttr, pNewElem) ' (*) newAttr: Value of type 'String' cannot be
converted to 'System.UInt16'
pMS.InsertElement(pNewElem, ptrBegin, ptrEnd)
ptrBegin.MoveToPointer(ptrEnd) End While
End If
-------------------------
VB .Net code till here.
-------------------------
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 21 '05 #4
Here you go...
Let me know if you all have any questions or comments
Friend WithEvents Browser As AxSHDocVw.AxWebBrowser
Private m_HighlightText As ArrayList
'================================================= ===========================================
Friend Sub Highlight(ByVal TextToHighlight As String, Optional ByVal ExactMatchColor As String = "Yellow", Optional ByVal WordMatchColors As String = "lime, aqua, violet, aquamarine, paleturquoise, gold, lavender, wheat, lightgrey, tomato")
' yellow, lime, aqua, violet, aquamarine, paleturquoise, gold, lavender, wheat, lightgrey, tomato
If TextToHighlight.Trim <> "" Then
Dim Bookmark As String = ""
Dim TextRange As mshtml.IHTMLTxtRange = DirectCast(DirectCast(DirectCast(Browser.Document, mshtml.HTMLDocumentClass).body, mshtml.IHTMLBodyElement).createTextRange(), mshtml.IHTMLTxtRange)

' add to highlight list...so we can highlight as we navigate
If ExactMatchColor <> "" Then AddToHighlightList(TextToHighlight)

' if we have multiple words then lets handle one at a time...
If TextToHighlight.Trim.IndexOf(" ") > -1 Then
Dim Counter As Integer = 0
Dim Colors() As String = Split(WordMatchColors.Replace(" ", ""), ",")
Dim Word As String = ""
Dim Words() As String = Split(TextToHighlight.Trim)
For Each Word In Words
If Word.Trim <> "" Then
Do While TextRange.findText(Word.Trim)
' save where we are if needed...
If Bookmark.Trim = "" Then Bookmark = TextRange.getBookmark
' highlight the text
TextRange.execCommand("BackColor", False, Colors(Counter))
' move cursor to end of this selection
TextRange.collapse(False)
Loop
If Bookmark.Trim <> "" Then
' we have a bookmark lets go back to it...
TextRange.moveToBookmark(Bookmark)
' make sure we can see it...
TextRange.scrollIntoView()
End If
' increment the counter to get next color
Counter = Counter + 1
' if we are past the colorlist length start over...
If Counter > Colors.Length - 1 Then Counter = 0
' reset our text range...
TextRange = DirectCast(DirectCast(DirectCast(Browser.Document, mshtml.HTMLDocumentClass).body, mshtml.IHTMLBodyElement).createTextRange(), mshtml.IHTMLTxtRange)
End If
Next Word
End If

' now lets find any exact matches and highlight correctly
Do While TextRange.findText(TextToHighlight.Trim)
' save where we are if needed...
If Bookmark.Trim = "" Then Bookmark = TextRange.getBookmark
' highlight the text
TextRange.execCommand("BackColor", False, ExactMatchColor)
' move cursor to end of this selection
TextRange.collapse(False)
Loop
If Bookmark.Trim <> "" Then
' we have a bookmark lets go back to it...
TextRange.moveToBookmark(Bookmark)
' make sure we can see it...
TextRange.scrollIntoView()
End If
End If
End Sub 'Highlight(ByVal TextToHighlight As String, Optional ByVal ExactMatchColor As String = "Yellow", Optional ByVal WordMatchColors As String = "lime, aqua, violet, aquamarine, paleturquoise, gold, lavender, wheat, lightgrey, tomato")
'================================================= ===========================================
Friend Sub HighlightSelectedText(ByVal HighlightAll As Boolean, Optional ByVal ExactMatchColor As String = "yellow")
Dim TextRange As mshtml.IHTMLTxtRange = DirectCast(DirectCast(DirectCast(Browser.Document, mshtml.HTMLDocumentClass).selection, mshtml.IHTMLSelectionObject).createRange(), mshtml.IHTMLTxtRange)

Do While TextRange.text.EndsWith(" ") = True
TextRange.moveEnd("character", -1)
Loop
Do While TextRange.text.StartsWith(" ") = True
TextRange.moveStart("character", 1)
Loop

If TextRange.text.Trim.Length <> 0 Then
If HighlightAll = False Then
' add to highlight list...so we can highlight as we navigate
AddToHighlightList(TextRange.text)
' highlight it...
TextRange.execCommand("BackColor", False, ExactMatchColor)
Else
Highlight(TextRange.text, ExactMatchColor)
End If
End If
End Sub 'HighlightSelectedText(ByVal HighlightAll As Boolean, Optional ByVal ExactMatchColor As String = "yellow")
'================================================= ===========================================
Private Sub AddToHighlightList(ByVal TextToHighlight As String)
If m_HighlightText.Contains(TextToHighlight) = False Then m_HighlightText.Add(TextToHighlight)
End Sub 'AddToHighlightList(ByVal TextToHighlight As String)
'================================================= ===========================================
Friend Sub RemoveHighlight()
If m_HighlightText.Count > 0 Then
Dim ArrayListEnumerator As IEnumerator = m_HighlightText.GetEnumerator
While ArrayListEnumerator.MoveNext = True
Highlight(DirectCast(ArrayListEnumerator.Current, String), "", "")
End While
m_HighlightText.Clear()
End If
End Sub 'RemoveHighlight()
'================================================= ===========================================
Marc Cramer

Bob Hollness wrote: I would be interested to see this code. Could you post it please?

Nov 21 '05 #5

Thank you for your code sample.
Atara.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
2
by: Phil Stanton | last post by:
When designing a new form or report, the Default ForeColor is often something like -2147483640 which is the colour of Windows text (possibly black) and the default backColor is -2147483643...
4
by: _B | last post by:
Is there any way to 'scrape' the ascii text from an AxSHDocView:AxWebBrowser control? IOW, I'd like to get the text as it is displayed by the browser. Somewhat like selecting all text in IE and...
0
by: Joergen Bech | last post by:
Trying to implement my own webforms designer to be hosted in a WinForms environment. Basically: I know that mshtml is used in the VS.Net 2003 WebForms designer, but the HTML behind the scenes is...
3
by: ddd | last post by:
I am trying to use MSHTML without the webbrowser and I am having a few problems. Right now all I am trying to do is load an URL(html page) and access its innerhtml. What I have is: Dim doc As...
9
by: Marina | last post by:
Hi, Anyone know of a component to turn HTML into nice looking text? So not just stripping out the tags, but formatting lists into text lists, etc. Thanks
6
by: Joerg Battermann | last post by:
Hey there, mmmm does anyone know a library or anything I can use to convert plaintext to html real quick and vice-versa? Regex surely would do the trick to a certain level, but maybe there's...
3
by: Mike | last post by:
Hi, There is anyway to read the text of a HTML page? Final text, not the HTML code. Thanks :)
2
by: Paul Hemans | last post by:
I am very new at .Net. I have a small project where I need to manipulate the contents of a web page. I have a form with a web browser control (webBrowser1) on it. Within the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.