在Visual Basic中将焦点设置为Internet Explorer对象

时间:2012-06-25 20:37:05

标签: internet-explorer vba setfocus

有人知道如何使用Visual Basic将焦点设置到IE对象上吗?我已经尝试了myieobject.SetFocus,但是这个语句的编译错误。

4 个答案:

答案 0 :(得分:4)

我需要一个我的电子表格,在执行一个功能后“设置焦点”到Internet Explorer,所以我不必费心点击它。这是我发现的工作:

      Const myPageTitle As String = "Title of my webpage"
      Const myPageURL As String = "http://www.mywebpage.com"
      Dim myIE As SHDocVw.InternetExplorer
      Dim myIE As InternetExplorer
      Set myIE = GetOpenIEByTitle(myPageTitle, False)


      myIE.visible = false
      DoEvents
      myIE.visible = true  
     'for some reason, making the page invisible then visible always ensures it pops up

    Function GetOpenIEByTitle(i_Title As String, _
                          Optional ByVal i_ExactMatch As Boolean = True) As SHDocVw.InternetExplorer
Dim objShellWindows As New SHDocVw.ShellWindows

  If i_ExactMatch = False Then i_Title = "*" & i_Title & "*"
  'ignore errors when accessing the document property
  On Error Resume Next
  'loop over all Shell-Windows
  For Each GetOpenIEByTitle In objShellWindows
    'if the document is of type HTMLDocument, it is an IE window
    If TypeName(GetOpenIEByTitle.document) = "HTMLDocument" Then
      'check the title
      If GetOpenIEByTitle.document.Title Like i_Title Then
        'leave, we found the right window
        Exit Function
      End If
    End If
  Next
End Function

答案 1 :(得分:1)

设置.Visible=True - 如果您在弹出窗口的某个地方丢失了屏幕,则必须循环浏览窗口标题以激活特定标题。

Dim objShell As Shell
Dim objIndex As InternetExplorer

Set objShell = New Shell

For Each objIndex In objShell.Windows
    If TypeName(objIndex.Document) = "HTMLDocument" Then
        If InStr(objIndex.Document.Title, "Stack Overflow") > 0 Then
            objIndex.Visible = True
            Exit For
        End If
    End If
Next objIndex

以下是您可以对IE对象执行的操作:MSDN

答案 2 :(得分:1)

尝试一下:

'First, hide the object even if it's visible
myieobject.Visible = False
' Second, show the object to focusing
myieobject.Visible = True

答案 3 :(得分:0)

试试这个。通过shell命令打开Internet Explorer,您可以在其中定义焦点(这是焦点和小窗口),然后捕获该shell /资源管理器窗口并将其定义为Internet Explorer对象。也许有比睡眠更好的方式等待。

library(caret)
library(dplyr)
#data
 str(df,10)
'data.frame':   1648 obs. of  1904 variables:
$ life_form: Factor w/ 3 levels "","herbaceous",..
$ leaf_size: num  120.4 73.6 85.4 60.1 108.7 ...
$ X601     : num  0.0847 0.1065 0.1286 0.084 0.0967 ...
$ X602     : num  0.0844 0.1062 0.1284 0.0837 0.0964 ...
$ X603     : num  0.0841 0.1058 0.1281 0.0833 0.0961 ...
$ X604     : num  0.0837 0.1053 0.1277 0.0829 0.0957 ...
$ X605     : num  0.0833 0.1049 0.1273 0.0824 0.0953 ...
$ X606     : num  0.0828 0.1044 0.1269 0.0819 0.0948 ...
$ X607     : num  0.0823 0.1039 0.1265 0.0814 0.0943 ...
$ X608     : num  0.0818 0.1033 0.1259 0.0809 0.0937 ...
 ....
$ X2500    : num  0.0853 0.1072 0.129 0.0847 0.0972 ...

  df %>% 
  group_by(life_form) %>% 
  summarise(n = n())

# A tibble: 
1 ""             4
2 grass        564
3 herbaceous   227
4 woody        853

#Filtering per class
woody = df %>% dplyr::filter(life_form =="woody")

# Data partition
index <- createDataPartition(woody$leaf_size, p = 0.7, list = FALSE)
train_data <- woody[index, ]
test_data  <- woody[-index, ]

control = trainControl(method ="cv",number= 10, repeat=5,   selectionFunction = "oneSE")
set.seed(1234)
mod1 <- train(tam_folha ~ ., data = train_data,
method = "pls",
metric = "RMSE",
tuneLength = 10,
trControl = control)

### Repeat to other life form classes