Sho*_*vik 11 vb.net excel vba excel-vba excel-automation
我试图在一些具有一些宏的Excel工作表上自动化数据填充.现在excel受到保护,我无法获得密钥.现在我能够运行宏,但是当我尝试传递参数时,我得到的参数不匹配.
如果我只是运行带有名称的宏,我得到inputbox一个额外的参数作为输入,并自动生成列的一些值.我必须手动输入此值到inputbox现在.有没有什么方法可以自动化该过程,即捕获vb.net脚本中的宏抛出的输入框并从那里输入值?即,我想运行宏,并在我得到弹出窗口要求我输入一些值后,使用vb.net代码输入该弹出窗口的值.
这就是我现在所拥有的
Public Class Form1
Dim excelApp As New Excel.Application
Dim excelWorkbook As Excel.Workbook
Dim excelWorkSheet As Excel.Worksheet
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
excelWorkbook = excelApp.Workbooks.Open("D:/excelSheets/plan_management_data_templates_network.xls")
excelApp.Visible = True
excelWorkSheet = excelWorkbook.Sheets("Networks")
With excelWorkSheet
.Range("B7").Value = "AR"
End With
excelApp.Run("createNetworks")
// now here I would like to enter the value into the createNetworks Popup box
excelApp.Quit()
releaseObject(excelApp)
releaseObject(excelWorkbook)
End Sub
Run Code Online (Sandbox Code Playgroud)
宏定义
createNetworks()
//does so basic comparisons on existing populated fields
//if true prompts an inputbox and waits for user input.
Run Code Online (Sandbox Code Playgroud)
这也阻止了我的vb.net脚本移动到下一行.
Sid*_*out 12
就像你和我,我们俩有名字,类似的窗口有handles(hWnd),Class等一旦你知道那是什么hWnd,它是更容易与窗口交互.
这是InputBox的屏幕截图

逻辑:
找到InputBox的句柄FindWindow和输入框的标题Create Network IDs
找到后,使用找到该窗口中编辑框的句柄 FindWindowEx
找到编辑框的句柄后,只需使用SendMessage它来写入.
在下面的例子中,我们将写入It is possible to Interact with InputBox from VB.NetExcel输入框.
代码:
创建一个表单并添加一个按钮.

粘贴此代码
Imports System.Runtime.InteropServices
Imports System.Text
Public Class Form1
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Integer
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
ByVal lParam As String) As Integer
Const WM_SETTEXT = &HC
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Ret As Integer, ChildRet As Integer
'~~> String we want to write to Input Box
Dim sMsg As String = "It is possible to Interact with InputBox from VB.Net"
'~~> Get the handle of the "Input Box" Window
Ret = FindWindow(vbNullString, "Create Network IDs")
If Ret <> 0 Then
'MessageBox.Show("Input Box Window Found")
'~~> Get the handle of the Text Area "Window"
ChildRet = FindWindowEx(Ret, 0, "EDTBX", vbNullString)
'~~> Check if we found it or not
If ChildRet <> 0 Then
'MessageBox.Show("Text Area Window Found")
SendMess(sMsg, ChildRet)
End If
End If
End Sub
Sub SendMess(ByVal Message As String, ByVal hwnd As Long)
Call SendMessage(hwnd, WM_SETTEXT, False, Message)
End Sub
End Class
Run Code Online (Sandbox Code Playgroud)
截图
当你运行代码时,这就是你得到的

编辑(基于进一步请求自动化聊天中的确定/取消)
自动化输入框的OK/CANCEL按钮
好的,这是一个有趣的事实.
您可以InputBox在Excel中以两种方式调用该函数
Sub Sample1()
Dim Ret
Ret = Application.InputBox("Called Via Application.InputBox", "Sample Title")
End Sub
Run Code Online (Sandbox Code Playgroud)
和
Sub Sample2()
Dim Ret
Ret = InputBox("Called Via InputBox", "Sample Title")
End Sub
Run Code Online (Sandbox Code Playgroud)

在你的情况下,使用第一种方式,不幸的是,OK和CANCEL按钮没有句柄,所以不幸的是,你将不得不使用SendKeys (Ouch!!!)它与它进行交互.如果您通过第二种方法生成了Inbutbox,那么我们可以轻松自动化OK和CANCEL按钮:)

附加信息:
在Visual Studio 2010 Ultimate(64位)/ Excel 2010(32位)上测试
受你的问题的启发,我实际上写了一篇关于如何与InputBox 上的按钮进行交互的博客文章OK.
| 归档时间: |
|
| 查看次数: |
5492 次 |
| 最近记录: |