473,385 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 10859
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.