I will test if this will do the trick today - If this will release my xlsx source files for update I will call this answer as solved.
Answer found here
Dim conn As Variant
For Each conn In ActiveWorkbook.Connections
conn.OLEDBConnection.MaintainConnection = False
Next conn
More info can be found here:
Microsoft learn
Whola - This worked ! I will here include how it is used in my code:
Sub TestEditWorkCenterQueries()
' Coded by Svein Arne Hylland 25.11.2022.
' This sub is to show how to change filters in all query
' In this case there is two queries one for 'Order' and one for 'Order Operations' - Bouth can filter on 'Main WorkCtr'.
' The WorkCenterArrey string will then be parsed from other sub in a form to change the filters on ThisWorkbook.Queries.
' Then each user can select what 'Main WorkCtr' they vant to include in their workbook.
Dim WorkCenterArrey As String
WorkCenterArrey = "'1338-XT','1338TOOL'"
EditAllWorkbookFormuals (WorkCenterArrey)
End Sub
Sub EditAllWorkbookFormuals(SelectedWorkCenters As String)
For Each q In ThisWorkbook.Queries
If q.Name <> "ZIQ09" Then
q.Formula = NewQuery(q.Formula, SelectedWorkCenters)
With ThisWorkbook.Connections("Query - " & q.Name)
.Refresh
.OLEDBConnection.MaintainConnection = False
End With
End If
Next
End Sub
Function NewQuery(MyQuery As String, WorkCenterArrey As String) As String
' Function recive the set Formula and returns the updated Formula with the new filter
WorkCenterArrey = Replace(WorkCenterArrey, "'", "")
' Convert the string to arrey for filter
Dim MyArr() As String
MyArr = Split(WorkCenterArrey, ",")
' Do you manipulation here of your own MyQuery string
'MsgBox MyQuery
' Use Select case, If, Inst, replace, and so on.
' .....
Pos1 = InStr(1, MyQuery, "each ([Main WorkCtr] = ") ' Find Start position to be replaced
If Pos1 > 0 Then Pos2 = InStr(Pos1, MyQuery, ")") ' Finds then End posistion to be replaced
If Pos1 > 0 Then
ToBeReplaced = Mid(MyQuery, Pos1, Pos2 - Pos1 + 1)
'MsgBox ToBeReplaced
' Build up the new filter from Arrey
NewFilter = "each ("
For e = LBound(MyArr) To UBound(MyArr)
NewFilter = NewFilter & "[Main WorkCtr] = """ & MyArr(e) & """ or "
Next
'Remove the last or statement - and add the parantese
NewFilter = Left(NewFilter, Len(NewFilter) - 4) & ")"
'MsgBox NewFilter
' Replace the filter
MyQuery = Replace(MyQuery, ToBeReplaced, NewFilter)
End If
' Parse the new string back to Sub as the NewQuery
NewQuery = MyQuery
End Function