Solved

IHTML question

Posted on 2002-04-19
8
596 Views
Last Modified: 2013-11-20
Good-day,

I am writing a program in VC 6.0 that will automate input of text on a web page text box.  I am using the CWebBrowser2 control and here is the code that I am using:

MSHTML::IHTMLDocument2Ptr pDoc= m_web.GetDocument();  MSHTML::IHTMLElementCollectionPtr pAllElems= pDoc->all;
MSHTML::IHTMLInputTextElementPtr pTextInput= pAllElems->item("editbox_name");
pTextInput->value = "some text";

This code is working fine; my problem is that I have encountered a web page that has mulitiple frames, and I need to specify which frame to select before I can run this.
Any ideas?
Thanks very much.
0
Comment
Question by:PastorDwayne
  • 4
  • 4
8 Comments
 
LVL 1

Expert Comment

by:Pavlik
ID: 6955122
You should get the frame from IHTMLDocument2.frames.item("frame name"), cast it to IHTMLWindow2 and get document from it. And after that get elements of this document and do with them whatever you want.
0
 

Author Comment

by:PastorDwayne
ID: 6959433
Could you give me an example as to how to use this. Lets say that the form name that I am trying to access is named "form1"

Thanks
0
 
LVL 1

Expert Comment

by:Pavlik
ID: 6960599
Ok. I have 3 files:
1.html - contains frameset
2.html - contains form
IEManip.cpp - does all the work with them.

Here they are:
1.html
--------------------------------------------
<html>
<frameset>
<frame name="frame1" src="file://c:/2.html">
</frame>
</frameset>
</html>
============================================
2.html
--------------------------------------------
<html>
<body>
<form name="form1" action="about:blank">
<input name="i1" type="text">
</form>
</body>
</html>
============================================
IEManip.cpp
--------------------------------------------
#import <shdocvw.dll>
#import <mshtml.tlb>

int main(int argc, char* argv[])
{
  CoInitialize(0);
  {
    SHDocVw::IWebBrowser2Ptr pIE(__uuidof(SHDocVw::InternetExplorer));
    MSHTML::IHTMLDocument2Ptr pHTMLDoc;
   
    pIE->Visible = true;
    pIE->Navigate("file://c:/1.html");
    while(pIE->GetBusy())
      Sleep(100);
    pIE->GetDocument()->QueryInterface(&pHTMLDoc);

    MSHTML::IHTMLWindow2Ptr pHTMLWnd = (IUnknown*)pHTMLDoc->frames->item(&_variant_t("frame1"));
    MSHTML::IHTMLFormElementPtr pForm = (IUnknown*)pHTMLWnd->document->forms->item(&_variant_t("form1"));
    MSHTML::IHTMLInputElementPtr pInput = (IUnknown*)pForm->item(&_variant_t("i1"));
    pInput->value = "Test string";
  }
  CoUninitialize();
  return 0;
}
============================================

You should put 1.html and 2.html to the root of disk C.

Let me know if you have more questions about this.
0
 

Author Comment

by:PastorDwayne
ID: 6960954
This code looks great... just a few questions about it.  

1.  I would like to incorporate the ActiveX WebBrowser control inside a dialog box rather than IE.

2. I searched the HTML source code for this web page that I am trying to access, and there doesn't seem to be a <frame name> tag anywhere. Is there a default value I can use for this?

Thanks.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Accepted Solution

by:
Pavlik earned 250 total points
ID: 6961137
If you don't have frame names, then just select frame by its index.

Remove attribute name from frame tag from 1.html:
<frame src="file://c:/2.html">
And change:
MSHTML::IHTMLWindow2Ptr pHTMLWnd = (IUnknown*)pHTMLDoc->frames->item(&_variant_t("frame1"));
to:
MSHTML::IHTMLWindow2Ptr pHTMLWnd = (IUnknown*)pHTMLDoc->frames->item(&_variant_t(0L));


That's all you need.

> 1.  I would like to incorporate the ActiveX WebBrowser control inside a dialog box rather than IE.
Let's connect it to your code:
=====================================
MSHTML::IHTMLDocument2Ptr pDoc= m_web.GetDocument(); // This is what you have in the first line

//And this is the remaining
MSHTML::IHTMLWindow2Ptr pHTMLWnd = (IUnknown*)pDoc->frames->item(&_variant_t(0L));
MSHTML::IHTMLFormElementPtr pForm = (IUnknown*)pHTMLWnd->document->forms->item(&_variant_t("form1"));
MSHTML::IHTMLInputElementPtr pInput = (IUnknown*)pForm->item(&_variant_t("i1"));
pInput->value = "Test string";
======================================

Just you can get IWebBrowser2 interface from IE and from WebBrowser control. And it will always have document inside.
0
 

Author Comment

by:PastorDwayne
ID: 6962543
I can't seem to get this code to work.  For example, I load the www.yahoo.com page in my WebBrowser and have a button that is attatched to this code:

     
MSHTML::IHTMLDocument2Ptr pDoc= m_web.GetDocument();

MSHTML::IHTMLWindow2Ptr pHTMLWnd = (IUnknown*)pDoc->frames->item(&_variant_t(0L));
MSHTML::IHTMLFormElementPtr pForm = (IUnknown*)pHTMLWnd->document->forms->item(&_variant_t("f"));
MSHTML::IHTMLInputElementPtr pInput = (IUnknown*)pForm->item(&_variant_t("p"));
pInput->value = "Test string";

The yahoo page form name seems to be "f" and the input edit box is "p". But when I try to run this, the program crashes.

Any ideas?

thanks.
0
 
LVL 1

Expert Comment

by:Pavlik
ID: 6963317
The problem here is that Yahoo page doesn't have frames. So you should simply get the form from the root document.

MSHTML::IHTMLFormElementPtr pForm = (IUnknown*)pDoc->forms->item(&_variant_t("f"));
MSHTML::IHTMLInputElementPtr pInput = (IUnknown*)pForm->item(&_variant_t("p"));
pInput->value = "Test string";
Sleep(5000);
pForm->submit();


0
 

Author Comment

by:PastorDwayne
ID: 6973182
Pavlik,

This worked great! Thanks very much for your help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ORA-01403: no data found 43 83
substring method in java 1 79
sum28 challenge 31 96
Fibonacci challenge 11 84
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now