简体   繁体   English

Excel VBA与Windows窗体应用程序流程通信

[英]Excel VBA to Windows Form Application Process Communication

I have been tasked to deal with an application where an excel spreadsheet needs to retrieve / access values directly from the windows form application running on a client side by side. 我的任务是处理一个需要Excel电子表格直接从客户端并行运行的Windows窗体应用程序检索/访问值的应用程序。 I have been reading on exposing .NET components to COM and I have been able to compile, register and consume them from excel. 我一直在阅读有关将.NET组件公开给COM的知识,并且能够从excel进行编译,注册和使用它们。 However, nothing that I have read so far has allowed me to tap directly into the windows process / form running and access some of the values on the form itself because I always have to "New" the .NET object in VBA and cannot access the current thread. 但是,到目前为止,我所读到的一切都没有让我直接进入Windows进程/窗体运行并访问窗体本身上的某些值的原因,因为我始终必须在VBA中“新建” .NET对象,并且无法访问当前线程。 My questions are: 我的问题是:

  • Is it possible to have direct process to process communication between an excel spreadsheet and a windows form application (no intermediary database or file)? 是否可以使用直接过程来处理excel电子表格和Windows窗体应用程序之间的通信(没有中间数据库或文件)?
  • Is it possible to compile the output of a .NET windows form application into a .dll? 是否可以将.NET Windows窗体应用程序的输出编译为.dll? So far, I can only get it to generate an .exe. 到目前为止,我只能生成一个.exe。

Please let me know if this is poorly worded and I can try to explain the problem better. 如果这句话措辞不佳,请告诉我,我可以尝试更好地解释这个问题。

Thank you for your help. 谢谢您的帮助。

I think the best way is use Messages between windows. 我认为最好的方法是在Windows之间使用消息。 Here is a sample to send from vb6 to vb.net, you can adapt it to vba: 这是从vb6发送到vb.net的示例,您可以使其适应vba:

vb6 code: vb6代码:

Private Sub cmdSendData_Click()
Dim sString As String
Dim lHwnd As Long
Dim cds As COPYDATASTRUCT
Dim buf(1 To 255) As Byte

sString = Trim$(txtString)
If sString = "" Then Exit Sub
'
' Get the handle of the target application's visible window.
'
lHwnd = FindWindow(vbNullString, cWINDOW_TITLE)
'
' Copy the string into a byte array,
' converting it to ASCII. Assign lpData
' the address of the byte array.
'
Call CopyMemory(buf(1), ByVal sString, Len(sString))
With cds
    .dwData = 3
    .cbData = Len(sString) + 1
    .lpData = VarPtr(buf(1))
End With
'
' Send the string.
'
Call SendMessage(lHwnd, WM_COPYDATA, Me.hwnd, cds)
End Sub

vb.net code: vb.net代码:

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    If m.Msg = MainForm.WM_COPYDATA Then
      Dim data As CopyData
      Dim message As String

      ' get the data...
      data = CType(m.GetLParam(GetType(CopyData)), CopyData)
      Dim B(255) As Byte
Marshal.Copy(data.lpData, B, 0, 255)
message = System.Text.Encoding.Default.GetString(B
      ' add the message
      Messages.Items.Add(String.Format("{0}: {1}", DateTime.Now.ToShortTimeString(), message))

      ' let them know we processed the message...
      m.Result = New IntPtr(1)
    Else
      MyBase.WndProc(m)
    End If
  End Sub

  Private Const WM_COPYDATA As Integer = &H4A

  <StructLayout(LayoutKind.Sequential)> _
  Private Structure CopyData
    Public dwData As IntPtr
    Public cbData As Integer
    Public lpData As IntPtr
  End Structure

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM