excel copy and paste in Windows Search

2019-08-07 02:42发布

I am using Excel 2010 and Windows 7 Pro. I want to copy one cell content into Windows Search box. How do I write a VBA for that?

Sub CopytoSearchWindow() 
'CopytoSearchWindow Macro ' ' 
sCell = Range(Application.InputBox(Prompt:="Pick the Cell", Type:=8)).Value 

1条回答
神经病院院长
2楼-- · 2019-08-07 03:08

You can use the Shell.Application object to do this in conjunction with sending some key strokes.

Example: (replace my searchString bit with whatever you like)

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub searchExample()

    Dim searchCell As Range
    Dim shellApp As Object
    Dim wshShell As Object

    On Error Resume Next 'to catch if user presses cancel
    Set searchCell = Application.InputBox(Prompt:="Pick the Cell", Type:=8)
    On Error Goto 0

    If searchCell Is Nothing Then Exit Sub

    Set shellApp = CreateObject("Shell.Application")
    Set wshShell = CreateObject("WScript.Shell")

    shellApp.FindFiles
    Sleep 500
    wshShell.SendKeys searchCell.Value & "{enter}"

End Sub

Shell.Application reference: http://msdn.microsoft.com/en-us/library/windows/desktop/bb773938%28v=vs.85%29.aspx

Windows scripting host Shell reference: http://msdn.microsoft.com/en-us/library/aew9yb99%28v=vs.84%29.aspx

查看更多
登录 后发表回答