Excel 将MsgBox文本复制到剪贴板

Excel 将MsgBox文本复制到剪贴板,excel,vba,Excel,Vba,如何使用VBA将MsgBox文本复制到剪贴板 myValue = InputBox("Please enter user text") MsgBox("This is my text " & myValue & ", my second text & Now & ".") 我不想使用ctrl+c,我想在MsgBox出现后自动复制此消息。将消息复制到何处? 如果要将其粘贴到单元格中,请使用以下内容: myValue = InputBox("Please enter

如何使用VBA将MsgBox文本复制到剪贴板

myValue = InputBox("Please enter user text")
MsgBox("This is my text " & myValue & ", my second text & Now & ".")
我不想使用ctrl+c,我想在MsgBox出现后自动复制此消息。

将消息复制到何处? 如果要将其粘贴到单元格中,请使用以下内容:

myValue = InputBox("Please enter user text")

MsgBox("This is my text " & myValue & "," & my second text & Now & ".")

myMsgBox = "This is my text " & myValue & "," & my second text & Now & "."

ThisWorkbook.Sheets("Sheet_to_be_pasted_in").Range("A2").Value = myMsgBox
如果您想以任何其他方式使用它,该值将存储在变量myMsgBox中。 将其复制到剪贴板,以便在下一个粘贴命令中可用:

Dim objMsgBox As New DataObject
myValue = InputBox("Please enter user text")   
MsgBox("This is my text " & myValue & "," & my second text & Now & ".")
myMsgBox = "This is my text " & myValue & "," & my second text & Now & "."
objMsgBox.SetText myMsgBox
objMsgBox.PutInClipboard
把信息复制到哪里? 如果要将其粘贴到单元格中,请使用以下内容:

myValue = InputBox("Please enter user text")

MsgBox("This is my text " & myValue & "," & my second text & Now & ".")

myMsgBox = "This is my text " & myValue & "," & my second text & Now & "."

ThisWorkbook.Sheets("Sheet_to_be_pasted_in").Range("A2").Value = myMsgBox
如果您想以任何其他方式使用它,该值将存储在变量myMsgBox中。 将其复制到剪贴板,以便在下一个粘贴命令中可用:

Dim objMsgBox As New DataObject
myValue = InputBox("Please enter user text")   
MsgBox("This is my text " & myValue & "," & my second text & Now & ".")
myMsgBox = "This is my text " & myValue & "," & my second text & Now & "."
objMsgBox.SetText myMsgBox
objMsgBox.PutInClipboard

有一种简单的方法可以复制所有“MsgBox”文本! 最简单的方法(即使您个人不想这样做:-)肯定是使用每个MsgBox中内置的Ctrl+C“复制处理程序

在执行MsgBox(…)命令之前,只需将ctrl+c击键发送回VBA本身

不要被其他人误导,他们想让你相信使用Application.SendKeys()命令是不可靠的。那不是真的!唯一的事实是,Application.SendKeys()命令命令会将您的击键填充到应用程序的键盘缓冲区中,而不管其中已经有什么。这正是许多其他VBA人员认为应用程序.SendKeys()命令不可靠的原因

在发出Ctrl+C SendKeys命令之前,您必须采取的唯一步骤是,您必须清除主机的键盘缓冲区!这很简单:只需在调用“Application.SendKeys()”方法之前写一行“DoEvents”,就完成了

现在,要使整个方法真正防弹,您只需确保您的MsgBox确实是接收Windows消息的当前窗口。只有最顶端(活动)的窗口接收发送的消息,而没有特殊的收件人地址(包括您自己的“Ctrl+c”消息)。要实现这一点,只需标记您的MsgBox”vbSystemModal+vbMsgBoxSetForeground”,您就可以开始了

也就是说,这是在公园里散步来解决你的“问题”

Public Sub Test(Optional ByVal myValue As String = vbNullString, _
            Optional ByVal ShowMsgBox As Boolean = False, _
            Optional ByVal EchoOut As Boolean = True)
' Param: myValue    => Text that gets decorated
' Param: ShowMsgBox => If true then messageBox gets displayed
' Param: EchoOut    => If true echos Clipboard to immediate window

If Len(myValue) = 0 Then
    ' Get something from the user
    myValue = InputBox("Please enter something")
End If

' Clear keyboard buffer
DoEvents
' Send "Ctrl+c" "Copy2Clip" to MSO Host
Application.SendKeys "^c"

' If you like, let's also press the OK button of the message box
If Not ShowMsgBox Then
    Application.SendKeys "{ENTER}"
End If

' Display Msgbox
MsgBox "This is my text '" & myValue & _
       "' my second text [" & Now & "].", _
        vbSystemModal + vbMsgBoxSetForeground

If EchoOut Then
    ' This will clear VBA's Console.
    DoEvents
    Application.SendKeys "^g ^a {DEL}"

    ' Copy what's in the Clipboard into VBA's "Immediate" window
    DoEvents
    Application.SendKeys "^g ^v"
End If

' Do not call DoEvents here! This only works when debugging in
' single-step mode(!) At runtime, VBA's immediate window cannot
' get the focus (Ctrl+g) to receive the "Ctrl+v" windows message
' here(!) Thus, DoEvents would eat up your 
' Application.SendKeys "^g ^v"
'
' DoEvents ' no, No, NO!
'
End Sub
将测试例程添加到一个公共代码模块中。然后转到即时窗口,键入测试“这是我的文本”,然后按enter键。祝您玩得开心


PS:不仅有Application.SendKeys()方法(请参见此处:),还有VBA“SendKeys()”语句(请参见此处:)。后者甚至允许您将击键发送到其他应用程序(如某些外部文本编辑器)。从VBA代码内部唯一需要做的事情是,在向该窗口发送“Ctrl+v”之前,必须打开另一个应用程序并将其主窗口置于最前面。启动其他Windows可执行文件,等待它们准备就绪,然后发送击键(使用VBA的SendKeys语句)当然可能会有点棘手,而且容易出错。因此,这种方法应该是您最后的选择。您可以在StackOverflow上找到如何做到这一点的示例。如果您安装了Microsoft Word,我强烈建议您使用Word Automation,而不是使用NotePad.exe玩Tic Tac Toe:-)

有一种简单的方法如何复制所有“MsgBox”文本! 最简单的方法(即使您个人不想这样做:-)肯定是使用每个MsgBox中内置的Ctrl+C“复制处理程序

在执行MsgBox(…)命令之前,只需将ctrl+c击键发送回VBA本身

不要被其他人误导,他们想让你相信使用Application.SendKeys()命令是不可靠的。那不是真的!唯一的事实是,Application.SendKeys()命令将把您的击键填充到应用程序的键盘缓冲区中,而不管其中已经包含了什么。这正是许多其他VBA人员认为Application.SendKeys()命令不可靠的原因

在发出Ctrl+C SendKeys命令之前,您必须执行的唯一步骤是,必须清除主机的键盘缓冲区!这是小菜一碟:在调用“Application.SendKeys()”方法之前只需写一行“DoEvents”,就完成了

现在,要使整个方法真正防弹,您只需确保您的MsgBox确实是当前接收Windows消息的窗口。只有最上面的(活动)窗口接收不带特殊接收者地址的消息(包括您自己的“Ctrl+c”消息)。要实现这一点,只需将MsgBox标记为“vbSystemModal+vbMsgBoxSetForeground”,就可以了

也就是说,这是在公园里散步来解决你的“问题”。这里有一些代码可以使用

Public Sub Test(Optional ByVal myValue As String = vbNullString, _
            Optional ByVal ShowMsgBox As Boolean = False, _
            Optional ByVal EchoOut As Boolean = True)
' Param: myValue    => Text that gets decorated
' Param: ShowMsgBox => If true then messageBox gets displayed
' Param: EchoOut    => If true echos Clipboard to immediate window

If Len(myValue) = 0 Then
    ' Get something from the user
    myValue = InputBox("Please enter something")
End If

' Clear keyboard buffer
DoEvents
' Send "Ctrl+c" "Copy2Clip" to MSO Host
Application.SendKeys "^c"

' If you like, let's also press the OK button of the message box
If Not ShowMsgBox Then
    Application.SendKeys "{ENTER}"
End If

' Display Msgbox
MsgBox "This is my text '" & myValue & _
       "' my second text [" & Now & "].", _
        vbSystemModal + vbMsgBoxSetForeground

If EchoOut Then
    ' This will clear VBA's Console.
    DoEvents
    Application.SendKeys "^g ^a {DEL}"

    ' Copy what's in the Clipboard into VBA's "Immediate" window
    DoEvents
    Application.SendKeys "^g ^v"
End If

' Do not call DoEvents here! This only works when debugging in
' single-step mode(!) At runtime, VBA's immediate window cannot
' get the focus (Ctrl+g) to receive the "Ctrl+v" windows message
' here(!) Thus, DoEvents would eat up your 
' Application.SendKeys "^g ^v"
'
' DoEvents ' no, No, NO!
'
End Sub
将测试例程添加到一个公共代码模块中。然后转到即时窗口,键入测试“这是我的文本”,然后按enter键。玩得开心


PS:这里不仅有Application.SendKeys()方法(请参见此处:),还有VBA“SendKeys()”语句(请参见此处:)。后者甚至允许您将击键发送到不同的应用程序(如某些外部文本编辑器)。从VBA代码内部要做的唯一一件事是,在向该窗口发送“Ctrl+v”之前,必须打开另一个应用程序并将其主窗口置于最前面。嗯,启动其他Windows可执行文件,等待它们准备就绪,发送击键(使用VBA的SendKeys语句)肯定有点棘手,而且容易出错。因此,这种方法应该是你的最后手段。您可以在StackOverflow上找到如何执行此操作的示例。如果您安装了Microsoft Word,我强烈建议您使用Word Automation,而不是使用NotePad.exe玩Tic Tac Toe:-)

将msgbox的文本存储在另一个变量中。类似于
STR=“这是我的文本”&MyValue&“,我的第二个文本”
要复制此STR,为什么“STR.copy”不工作