5 Replies Latest reply on Jan 10, 2018 8:46 AM by Matt Bieringer

    Check to see what monitor Solidworks is open on.

    Matt Bieringer

      Hello everyone.

       

      I currently have a macro that will create a drawing from a part that will add a new sheet and various notes based on each configuration. At one point in the macro I open up excel so the user can reference certain data in the file. My issue is I need to find out what monitor Solidworks is open on so I can make sure that excel opens on the opposite monitor.

       

      Matt

        • Re: Check to see what monitor Solidworks is open on.
          Josh Brady

          I'm about 95% sure you're going to have to use windows API functions for this.

           

          You can get the HWnd for the SolidWorks application with Application.SldWorks.Frame.GetHWnd.

          You can get the HWnd for the desktop window (which is the entire desktop area upon which other windows are drawn) from the Windows API GetDesktopWindow.

           

          Using the HWnds, you can get dimensional and location information for the two using Windows API GetWindowInfo.

            • Re: Check to see what monitor Solidworks is open on.
              Matt Bieringer

              Thanks Josh,

               

              I was able to make a function that outputs a string as to which monitor the frame is on.

               

              Option Explicit
              Private Type RECT
                  Left            As Long
                  Top             As Long
                  Right           As Long
                  Bottom          As Long
              End Type
              
              Private Declare PtrSafe Function GetWindowRect Lib "User32" (ByVal HWnd As Long, lpRect As RECT) As Long
              Private Declare PtrSafe Function GetSystemMetrics32 Lib "User32" Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long
              
              Sub Main()
              Dim swApp As SldWorks.SldWorks
              Dim swFrame As Frame
              Dim HWnd As Long
              Dim strMonitor As String
              
              Set swApp = Application.SldWorks
              Set swFrame = swApp.Frame
              HWnd = swFrame.GetHWnd
              strMonitor = SWLoc(HWnd)
              Debug.Print strMonitor
              End Sub
              
              Function SWLoc(HWnd As Long) As String
              Dim typeRC  As RECT
              If GetWindowRect(HWnd, typeRC) <> 0 Then
                  If typeRC.Right <= (GetSystemMetrics32(78) / 2) Then SWLoc = "Left"
                  If typeRC.Right > (GetSystemMetrics32(78) / 2) Then SWLoc = "Right"
              End If
              End Function
              

              It works fine for my application, but now I need to figure out how to open Excel on the opposite monitor.