Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 打开窗口左侧和右侧的文件夹_Vba - Fatal编程技术网

Vba 打开窗口左侧和右侧的文件夹

Vba 打开窗口左侧和右侧的文件夹,vba,Vba,我使用以下代码在min szie中打开一个文件夹 Call Shell("explorer.exe" & " " & "D:\Archive\", vbMinimizedFocus) Call Shell("explorer.exe" & " " & "D:\Shortcuts\", vbMinimizedFocus) 然而,我很想让它出现在彼此的旁边。一个在左边,一个在右边。像这样 有人知道打开屏幕后是否有移动屏幕的方法吗 经过尝试和测试[Win 7/Exce

我使用以下代码在min szie中打开一个文件夹

Call Shell("explorer.exe" & " " & "D:\Archive\", vbMinimizedFocus)
Call Shell("explorer.exe" & " " & "D:\Shortcuts\", vbMinimizedFocus)
然而,我很想让它出现在彼此的旁边。一个在左边,一个在右边。像这样

有人知道打开屏幕后是否有移动屏幕的方法吗

经过尝试和测试[Win 7/Excel 2010-VBA/1920 X 1080移动PC显示器]

下面是一个关于如何实现你想要的东西的非常基本的例子。我们将为此使用四个API

我不会单独介绍这些API。要了解他们做什么,只需单击相应的链接

逻辑:

较新的浏览器没有我在上面的评论中提到的标题。比如,你看这个

然而,在玩Spy++时,我可以看到它们有标题,但没有显示在文件夹的标题栏上。请参见下面的屏幕截图

使用FindwindowAPI使用窗口的标题定位窗口 使用SetParent,我们为指定的子窗口文件夹窗口分配父窗口,即桌面。 使用SetWindowPos API重新定位窗口 代码:

将此代码粘贴到模块中,并根据需要更改文件夹。这是一个非常基本的代码,我没有做任何错误处理。我相信你会处理好的

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

Private Declare Function SetParent Lib "user32.dll" _
(ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long

Private Declare Function SetWindowPos Lib "user32.dll" _
(ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
ByVal x As Long, ByVal y As Long, ByVal cx As Long, _
ByVal cy As Long, ByVal wFlags As Long) As Long

Private Declare Function GetDesktopWindow Lib "user32" () As Long

Private Const SWP_NOZORDER As Long = &H4
Private Const SWP_SHOWWINDOW As Long = &H40

Private Sub Sample()
    Dim lHwnd As Long
    Dim Fldr1Path As String, Fldr2Path As String
    Dim winName As String
    Dim Flder1X As Long, Flder1Y As Long
    Dim FlderWidth As Long, FlderHeight As Long

    '~~> Folder one X,Y screen position
    Flder1_X = 50: Flder1_Y = 50
    '~~> Folder Width and Height. Keepping the same for both
    FlderWidth = 200: FlderHeight = 200

    '~~> Two Folders you want to open
    Fldr1Path = "C:\Temp1"
    Fldr2Path = "C:\Temp2"

    '~~> The Top most folder name which is also the caption of the window
    winName = GetFolderName(Fldr1Path)

    '~~~> Launch the folder
    Shell "explorer.exe" & " " & Fldr1Path, vbMinimizedFocus

    '~~> wait for 2 seconds
    Wait 2

    '~~> Find the Window. 
    '~~> I am using `vbNullString` to make it compatible with XP
    lHwnd = FindWindow(vbNullString, winName)

    '~~> Set the parent as desktop
    SetParent lHwnd, GetDesktopWindow()

    '~~> Move the Window
    SetWindowPos lHwnd, 0, Flder1_X, Flder1_Y, FlderWidth, _
    FlderHeight, SWP_NOZORDER Or SWP_SHOWWINDOW

    '~~> Similary for Folder 2
    winName = GetFolderName(Fldr2Path)
    Shell "explorer.exe" & " " & Fldr2Path, vbMinimizedFocus
    Wait 2
    lHwnd = FindWindow(vbNullString, winName)
    SetParent lHwnd, 0
    SetWindowPos lHwnd, 0, Flder1_X + FlderWidth + 10, Flder1_Y, _
    FlderWidth, FlderHeight, SWP_NOZORDER Or SWP_SHOWWINDOW

    MsgBox "Done"
End Sub

Private Sub Wait(ByVal nSec As Long)
    nSec = nSec + Timer
    While nSec > Timer
        DoEvents
    Wend
End Sub

Function GetFolderName(sPath As String)
    Dim MyAr

    MyAr = Split(sPath, "\")

    GetFolderName = MyAr(UBound(MyAr))
End Function
截图:文件夹排列

编辑

经过尝试和测试[Win XP/Excel 2003-VBA/on VM]

特别感谢您为我测试这个

经过尝试和测试[Win 7/Excel 2010-VBA/1920 X 1080移动PC显示器]

下面是一个关于如何实现你想要的东西的非常基本的例子。我们将为此使用四个API

我不会单独介绍这些API。要了解他们做什么,只需单击相应的链接

逻辑:

较新的浏览器没有我在上面的评论中提到的标题。比如,你看这个

然而,在玩Spy++时,我可以看到它们有标题,但没有显示在文件夹的标题栏上。请参见下面的屏幕截图

使用FindwindowAPI使用窗口的标题定位窗口 使用SetParent,我们为指定的子窗口文件夹窗口分配父窗口,即桌面。 使用SetWindowPos API重新定位窗口 代码:

将此代码粘贴到模块中,并根据需要更改文件夹。这是一个非常基本的代码,我没有做任何错误处理。我相信你会处理好的

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

Private Declare Function SetParent Lib "user32.dll" _
(ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long

Private Declare Function SetWindowPos Lib "user32.dll" _
(ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
ByVal x As Long, ByVal y As Long, ByVal cx As Long, _
ByVal cy As Long, ByVal wFlags As Long) As Long

Private Declare Function GetDesktopWindow Lib "user32" () As Long

Private Const SWP_NOZORDER As Long = &H4
Private Const SWP_SHOWWINDOW As Long = &H40

Private Sub Sample()
    Dim lHwnd As Long
    Dim Fldr1Path As String, Fldr2Path As String
    Dim winName As String
    Dim Flder1X As Long, Flder1Y As Long
    Dim FlderWidth As Long, FlderHeight As Long

    '~~> Folder one X,Y screen position
    Flder1_X = 50: Flder1_Y = 50
    '~~> Folder Width and Height. Keepping the same for both
    FlderWidth = 200: FlderHeight = 200

    '~~> Two Folders you want to open
    Fldr1Path = "C:\Temp1"
    Fldr2Path = "C:\Temp2"

    '~~> The Top most folder name which is also the caption of the window
    winName = GetFolderName(Fldr1Path)

    '~~~> Launch the folder
    Shell "explorer.exe" & " " & Fldr1Path, vbMinimizedFocus

    '~~> wait for 2 seconds
    Wait 2

    '~~> Find the Window. 
    '~~> I am using `vbNullString` to make it compatible with XP
    lHwnd = FindWindow(vbNullString, winName)

    '~~> Set the parent as desktop
    SetParent lHwnd, GetDesktopWindow()

    '~~> Move the Window
    SetWindowPos lHwnd, 0, Flder1_X, Flder1_Y, FlderWidth, _
    FlderHeight, SWP_NOZORDER Or SWP_SHOWWINDOW

    '~~> Similary for Folder 2
    winName = GetFolderName(Fldr2Path)
    Shell "explorer.exe" & " " & Fldr2Path, vbMinimizedFocus
    Wait 2
    lHwnd = FindWindow(vbNullString, winName)
    SetParent lHwnd, 0
    SetWindowPos lHwnd, 0, Flder1_X + FlderWidth + 10, Flder1_Y, _
    FlderWidth, FlderHeight, SWP_NOZORDER Or SWP_SHOWWINDOW

    MsgBox "Done"
End Sub

Private Sub Wait(ByVal nSec As Long)
    nSec = nSec + Timer
    While nSec > Timer
        DoEvents
    Wend
End Sub

Function GetFolderName(sPath As String)
    Dim MyAr

    MyAr = Split(sPath, "\")

    GetFolderName = MyAr(UBound(MyAr))
End Function
截图:文件夹排列

编辑

经过尝试和测试[Win XP/Excel 2003-VBA/on VM]

特别感谢您为我测试这个


如果您使用的是相同的两个文件夹,则可以轻松地执行此操作

1-手动打开两个文件夹,然后设置所需的大小和位置。关闭文件夹

2-然后下次调用脚本时,执行以下操作

Set oShell = WScript.CreateObject("WScript.Shell")
oShell.Run "Explorer /n, D:\Archive\", 4, False 
oShell.Run "Explorer /n, D:\Shortcuts\", 4, False
这将打开具有上次保存位置和大小的文件夹


注:刚刚在我的Win7机器上测试了它,但它不工作。原来Win7不再记得文件夹的位置了,它只记得文件夹的大小。阅读更多关于它的信息

如果您使用的是相同的两个文件夹,则可以轻松地执行此操作

1-手动打开两个文件夹,然后设置所需的大小和位置。关闭文件夹

2-然后下次调用脚本时,执行以下操作

Set oShell = WScript.CreateObject("WScript.Shell")
oShell.Run "Explorer /n, D:\Archive\", 4, False 
oShell.Run "Explorer /n, D:\Shortcuts\", 4, False
这将打开具有上次保存位置和大小的文件夹


注:刚刚在我的Win7机器上测试了它,但它不工作。原来Win7不再记得文件夹的位置了,它只记得文件夹的大小。阅读更多关于它的信息

我可以用记事本的两个实例来实现,但从未用windows资源管理器尝试过。Windows资源管理器随Windows的每个版本而发展。较新的资源管理器没有标题。如果您有XP,使用Findwindow之类的API会更容易。还是让我玩一下,顺便问一下你用的是什么操作系统?从屏幕截图上看,您似乎正在使用XP?我可以用两个记事本实例来完成,但从未用windows资源管理器尝试过。Windows资源管理器随Windows的每个版本而发展。较新的资源管理器没有标题。如果您有XP,使用Findwindow之类的API会更容易。还是让我玩一下,顺便问一下你用的是什么操作系统?从屏幕截图上看,您似乎正在使用XP?Siddharth,非常感谢您的全面回复。它可以工作:。不过,我希望能更好地理解这一点。你有一些好的教程的来源吗。。。。?顺便问一下,这是什么语言?Visual Basic。要了解API是如何工作的,请查看上述答案中提到的链接。Siddharth,非常感谢您的全面回复。它可以工作:。不过,我希望能更好地理解这一点。你有一些好的教程的来源吗。。。。?顺便问一下,这是什么语言?Visual Basic。要了解API是如何工作的,请查看上述答案中提到的链接。