简体   繁体   English

使用WinAPI将返回键发送到Excel公式栏

[英]Send Return Key To Excel Formula Bar Using WinAPI

What I'm trying to do: 我正在尝试做什么:

I'm trying to copy text to the clipboard then paste into excel while keeping all the formatting. 我正在尝试将文本复制到剪贴板,然后粘贴到Excel中,同时保留所有格式。

The only way I have found to stop excel from spreading the text across many cells and keep formatting like bullet points etc is after copying to the clipboard, to paste it directly into the formula bar. 我发现停止excel在多个单元格中传播文本并保持格式如子弹点等的唯一方法是复制到剪贴板后,将其直接粘贴到公式栏中。

How I'm attempting it: 我是怎么尝试的:

I'm using Win API to get the handle of the formula bar. 我正在使用Win API来获取公式栏的句柄。

Then sending a WM_PASTE message to the window to paste what's on the clipboard. 然后向窗口发送WM_PASTE消息以粘贴剪贴板上的内容。

Then sending a WM_SETFOCUS message to the window ready to receive the return key. 然后向窗口发送WM_SETFOCUS消息,准备接收返回密钥。

Then sending a WM_KEYDOWN message for the return key. 然后为返回键发送WM_KEYDOWN消息。

Private Declare Function FindWindow _
    Lib "user32" _
    Alias "FindWindowA" _
    (ByVal lpClassName As String, _
    ByVal lpWindowName As String) As Long

Private Declare Function FindWindowEx _
    Lib "user32" _
    Alias "FindWindowExA" _
    (ByVal hwndParent As Long, _
    ByVal hwndChildAfter As Long, _
    ByVal lpszClass As String, _
    ByVal lpszWindow As String) As Long

Private Declare Function SendMessage _
    Lib "user32.dll" _
    Alias "SendMessageA" _
    (ByVal hWnd As Long, _
    ByVal Msg As Long, _
    ByVal wParam As Long, _
    ByRef lParam As Any) _
    As Long

Declare Function PostMessage _
    Lib "user32" _
    Alias "PostMessageA" ( _
    ByVal hWnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    lParam As Any) As Long

Declare Function SetForegroundWindow _
    Lib "user32" _
    (ByVal hWnd As Long) As Long

Private Const WM_CUT As Long = &H300
Private Const WM_COPY As Long = &H301
Private Const WM_PASTE As Long = &H302
Private Const WM_CLEAR As Long = &H303
Private Const WM_UNDO As Long = &H304
Private Const WM_KEYDOWN  As Long = &H100
Private Const WM_KEYUP  As Long = &H101
Private Const VK_F5  As Long = &H74
Private Const VK_RETURN  As Long = &HD
Private Const WM_CHAR  As Long = &H102
Private Const WM_SETFOCUS  As Long = &H7
Private Const WM_KILLFOCUS  As Long = &H8
Private Const WM_IME_SETCONTEXT  As Long = &H281

Public Sub pasteClipboard()
    hwndMain = Application.hWnd: Debug.Print hwndMain
    hwndFormulaBar = FindWindowEx(Application.hWnd, ByVal 0&, "EXCEL<", vbNullString): Debug.Print hwndFormulaBar
    hwndDesk = FindWindowEx(Application.hWnd, ByVal 0&, "XLDESK", vbNullString): Debug.Print hwndDesk
    hwndSheet = FindWindowEx(hwndDesk, ByVal 0&, "EXCEL7", vbNullString): Debug.Print hwndSheet

    RetVal = SendMessage(hwndFormulaBar, WM_PASTE, 0, ByVal 0)
    Debug.Print SendMessage(hwndFormulaBar, WM_SETFOCUS, 0, 0)
    Debug.Print SendMessage(hwndFormulaBar, WM_IME_SETCONTEXT, &H0, &H0)
    Debug.Print SendMessage(hwndFormulaBar, WM_KEYDOWN, VK_RETURN, &H0)
End Sub

The Problem: 问题:

This all works up until when I send the return key to finish editing the cell which is what I'd like to happen. 这一切都有效,直到我发送返回键完成编辑单元格,这是我想要发生的。 Instead it puts a carriage return in the text box which makes a lot of sense but not the result I wanted. 相反,它在文本框中放置一个回车,这很有意义,但不是我想要的结果。

I've looked at the formula window with Spy++ and watched what happens when I type something in the window and hit return - when the return key is hit it finishes editing the cell. 我用Spy ++查看了公式窗口并观察了当我在窗口中键入内容并点击返回时会发生什么 - 当返回键被击中时,它完成了对单元格的编辑。

在此输入图像描述

The only two commands I'm not using that show up in Spy++ are WM_IME_SETCONTEXT and WM_IME_NOTIFY but in all honesty I'm not sure what these two commands do. 在Spy ++中出现的唯一两个我没有使用的命令是WM_IME_SETCONTEXT和WM_IME_NOTIFY但是老实说我不确定这两个命令是做什么的。

I tried using the WM_IME_SETCONTEXT above thinking I may have to change it before sending the return key but the results didn't change. 我尝试使用上面的WM_IME_SETCONTEXT认为我可能必须在发送返回键之前更改它,但结果没有改变。

Any solutions? 有解决方案吗

If anyone can point me in the right direction of how to send a message that will finish editing the cell (doesn't have to be the return key, that was just my first thought) that would be great. 如果有人能指出我正确的方向,如何发送一个消息,将完成编辑单元格(不一定是返回键,这只是我的第一个想法),这将是伟大的。

Many Thanks 非常感谢

Thank you for all the comments. 感谢您的所有评论。

@Rita Han - MSFT The application.SendKeys worked as long as the focus was not the VBE window which is great! @Rita Han - MSFT应用程序.SendKeys工作只要焦点不是VBE窗口,这是伟大的!

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

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