noveske
Board Regular
- Joined
- Apr 15, 2022
- Messages
- 120
- Office Version
- 365
- Platform
- Windows
- Mobile
- Web
Currently Excel VBA creates and opens Word.
On Word open, Excel is sized to the left half of the screen and Word is sized to the right half of the screen.
It sizes the windows to the full resolution of the desktop. 1920x1080.
This causes the 2 windows to be cut off at the bottom as they are hidden under the task bar. Can't see the tabs.
I'd like to get it to where it will fit the windows like the split screen feature built into Windows.
Excel on the left plane.
Word on the right plane.
Then have both windows be active on top if there are other windows open.
Tried a bunch of different active commands and it hasn't worked.
I tried resizing the actual window size and failed.
My other attempt placed the windows on top and covered the task bar. Still couldn't see the taskbar.
I believe I did get it to work once, it was a longer code than just:
wordDoc.Activate
Was wordDoc.Activate then maybe WordApplication xl and maybe a 1 in there.
But I didn't save it and lost it. I changed it again and tinkered with it because I didn't like how it would place a thick white border around both windows.
Is there a way to do this and something more efficient? Been slowly throwing this one together and figuring things out.
On Word open, Excel is sized to the left half of the screen and Word is sized to the right half of the screen.
It sizes the windows to the full resolution of the desktop. 1920x1080.
This causes the 2 windows to be cut off at the bottom as they are hidden under the task bar. Can't see the tabs.
I'd like to get it to where it will fit the windows like the split screen feature built into Windows.
Excel on the left plane.
Word on the right plane.
Then have both windows be active on top if there are other windows open.
Tried a bunch of different active commands and it hasn't worked.
I tried resizing the actual window size and failed.
My other attempt placed the windows on top and covered the task bar. Still couldn't see the taskbar.
I believe I did get it to work once, it was a longer code than just:
wordDoc.Activate
Was wordDoc.Activate then maybe WordApplication xl and maybe a 1 in there.
But I didn't save it and lost it. I changed it again and tinkered with it because I didn't like how it would place a thick white border around both windows.
Is there a way to do this and something more efficient? Been slowly throwing this one together and figuring things out.
VBA Code:
Declare PtrSafe Function FindWindowA Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Declare PtrSafe Function SetWindowPos Lib "user32" (ByVal hWnd As LongPtr, ByVal hWndInsertAfter As LongPtr, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal uFlags As Long) As Long
Sub CreateAndOpenWordDocument()
On Error Resume Next
Dim wordApp As Object
Set wordApp = GetObject(, "Word.Application")
' If Word application is not open, create a new instance
If wordApp Is Nothing Then
Set wordApp = CreateObject("Word.Application")
End If
On Error GoTo 0
Dim wordDoc As Object
Dim filePath As String
filePath = ThisWorkbook.Path & "\103.docx"
' Check if the Word document file exists
If Dir(filePath) = "" Then
Set wordDoc = wordApp.Documents.Add
' Save the Word document at the specified location
wordDoc.SaveAs2 filePath
Else
' Open the existing Word document
Set wordDoc = wordApp.Documents.Open(filePath)
End If
' Make sure the Word document is activated
wordDoc.Activate
wordApp.Visible = True
' Initialize Word event handler
Dim WordEventObj As New WordEventHandler
Set WordEventObj.wordApp = wordApp
' Get Excel window handle
Dim excelHwnd As LongPtr
excelHwnd = FindWindowA("XLMAIN", vbNullString)
' Get Word window handle
Dim wordHwnd As LongPtr
wordHwnd = FindWindowA("OpusApp", vbNullString)
' Set window positions and sizes for split view
If excelHwnd <> 0 And wordHwnd <> 0 Then
Dim screenWidth As Long, screenHeight As Long
screenWidth = 1920 ' Width of the screen
screenHeight = 1080 ' Height of the screen
' Excel window on the left, taking up the specified width and full height
SetWindowPos excelHwnd, 0, 0, 0, 960, screenHeight, &H4
' Word window on the right, taking up the specified width and full height
SetWindowPos wordHwnd, 0, 960, 0, 960, screenHeight, &H4
End If
' Loop to keep the script running until Word is closed
Do
DoEvents
Loop While WordEventObj.wordApp.Documents.Count > 0
' Run CopyTextFile immediately after Word is closed
Application.Run "CopyTextFile"
' Clean up
Set WordEventObj.wordApp = Nothing ' Disconnect the event handler first
Set wordDoc = Nothing
Set wordApp = Nothing
End Sub