tagCANDY CGI VBレスキュー(花ちゃん)の Visual Basic 6.0用 掲示板
VBレスキュー(花ちゃん)の Visual Basic 6.0用 掲示板
[ツリー表示へ]  [ワード検索]  [Home]

タイトル Re^3: Webページの範囲選択につきまして
投稿日: 2017/07/05(Wed) 23:56
投稿者魔界の仮面弁士
> 選択状態を解除しても構わないのであれば、これで如何でしょう。
execCommand メソッドで、"ClearSelection" コマンドを発行した方が手っ取り早いかも。

https://msdn.microsoft.com/ja-jp/library/ms536419.aspx
https://msdn.microsoft.com/ja-jp/library/ms533049.aspx


> プログラムで、検索欄にカーソルがある場合でも範囲選択の方法はありますでしょうか。

選択前に、『どこを選択するのか』を定めておく必要があるでしょうね。

google サイトの例でいえば、ロード時に検索欄にフォーカスが移動していますので、
たとえばコピーを始める前に、body 要素をアクティブにするなどです。

 objInternetExplorer.Document.body.Focus
 objInternetExplorer.ExecWB OLECMDID_SELECTALL, OLECMDEXECOPT_DODEFAULT


また、フレームの問題もあります。たとえば、フレーム付きのサイトである
http://hanatyan.sakura.ne.jp/vbhlp/index.html
などは、どのフレームを選択状態にするのかを定めねばなりませんよね。


Option Explicit
Private WithEvents objInternetExplorer As InternetExplorer
Private Const targetUrl As String = "http://hanatyan.sakura.ne.jp/vbhlp/index.html"
Private Sub Command1_Click()
    Set objInternetExplorer = New InternetExplorer
    objInternetExplorer.Visible = True
    objInternetExplorer.Navigate2 targetUrl
End Sub
Private Sub objInternetExplorer_DocumentComplete(ByVal pDisp As Object, url As Variant)
    If url = targetUrl Then
        Dim frames As Object
        Set frames = pDisp.Document.frames
        Debug.Print frames.Length
        
        Dim frame As Object
        'Set frame = frames(0)    '左のフレーム(menu.htm)
        Set frame = frames(1)    '右のフレーム(top.htm)
        
        Dim doc As HTMLDocument
        Set doc = frame.Document
        doc.body.Focus
        
        pDisp.ExecWB OLECMDID_SELECTALL, OLECMDEXECOPT_DODEFAULT
        pDisp.ExecWB OLECMDID_COPY, OLECMDEXECOPT_DODEFAULT
        
        Dim selection As Object
        Set selection = doc.selection.createRange()
        selection.execCommand "ClearSelection"
    End If
End Sub

Private Sub objInternetExplorer_OnQuit()
    Set objInternetExplorer = Nothing
End Sub

Private Sub Form_Unload(ByRef Cancel As Integer)
    If Not objInternetExplorer Is Nothing Then
        objInternetExplorer.Quit
    End If
End Sub


> 質問2
> Internet Explorer 以外のブラウザでは範囲選択の解除はできないものと考えてよいでしょうか。
どのブラウザーをターゲットにするかで変わってくると思います。
(自作ブラウザーまで含めた、あらゆるブラウザーに対応するというのは流石に無理なので)

操作対象のアプリケーションによっては、UIAutomation から Invoke するとか、
IAccessible の accSelect メソッドなり accDoDefaultAction メソッドなりが
通じるケースもあるかもしれませんが、案件次第では、Selenium WebDriver を
利用できるかもしれません。いずれにせよ操作対象に依存するでしょう。

- 関連一覧ツリー をクリックするとツリー全体を一括表示します)

古いスレッドにレスはつけられません。