This answer should provide you with the general approach to achieve the synchronization between Excel-VBA and word-VBA using a mutex to protect some shared data. The details depend on your target application..
The idea goes like this, you can create this code in the module ThisWorkbook
of Excel and similarly in ThisDocument
of Word:
' Module ThisWorkbook or similarly ThisDocument
Private myMutex As Long
Private Const ERROR_ALREADY_EXISTS = 183&
Private Const MUTEX_ALL_ACCESS = &H1F0001
Private Declare PtrSafe Function CreateMutex Lib "kernel32" Alias "CreateMutexA" (ByVal lpMutexAttributes As Long, ByVal bInitialOwner As Long, ByVal lpName As String) As Long
Private Declare PtrSafe Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare PtrSafe Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare PtrSafe Function OpenMutex Lib "kernel32" Alias "OpenMutexA" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal lpName As String) As Long
Private Declare PtrSafe Function ReleaseMutex Lib "kernel32" (ByVal hMutex As Long) As Long
Private Sub Workbook_Open()
myMutex = CreateMutex(0, 1, "myMutex")
Dim er As Long: er = Err.LastDllError
If er = 0 Then
MsgBox "myMutex Created"
ElseIf er = ERROR_ALREADY_EXISTS Then
MsgBox "mutex previously created"
myMutex = OpenMutex(MUTEX_ALL_ACCESS, 0, "myMutex")
Else
MsgBox "mutex creation failed"
End If
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
CloseHandle myMutex
End Sub
Private Sub doSomeCriticalTask()
WaitForSingleObject myMutex, 20000 ' say we can wait for 20 seconds
''''''''''''''''''''''''''''''''''''''''''''''''''''''
' do critical section code, access shared data safely
''''''''''''''''''''''''''''''''''''''''''''''''''''''
ReleaseMutex myMutex
End Sub