Original Author: Jean-Francois Cayron
Author Notes:
Change Universe
Sub ChangeUniverse()
Dim UnvCount As Integer
Dim UnvNameList() As String
Dim ix As Integer
Dim DefaultPath As String
DefaultPath = "C:\infosol\"
Application.Interactive = False
Set CurDoc = Application.ActiveDocument
Set UnvList = Application.Universes
UnvCount = UnvList.Count
ReDim UnvNameList(UnvCount)
For ix = 1 To UnvCount
UnvNameList(ix) = UnvList.Item(ix).LongName
frmChoose.cboxFrom.AddItem (UnvNameList(ix))
frmChoose.cboxTo.AddItem (UnvNameList(ix))
Next ix
frmChoose.txtPath.Value = DefaultPath
Application.Interactive = True
frmChoose.cboxFrom.ListIndex = 0
frmChoose.cboxTo.ListIndex = 0
frmChoose.Show
End Sub
Private Sub btnCancel_Click()
Unload Me
End Sub
Private Sub btnGo_Click()
Dim ix, iy As Integer
Dim CountDP As Integer
Dim CountDocs As Integer
Dim ChangeFrom As String
Dim ChangeTo As Universe
Dim Docs As Documents
Dim CurDoc As Document
Dim DPList As DataProviders
Dim CurDP As DataProvider
Dim CurUnvName As String
Dim FolderName As String
Dim SearchPath As String
Dim DocName As String
Dim DocList() As String
Dim DocCount As Integer
Dim DirtyDoc As Boolean
Dim ErrorOn As Boolean
Dim ErrDocs() As String
Dim ErrCount As Integer
Dim ErrDisplay As String
Set Docs = Application.Documents
FolderName = Me.txtPath
On Error GoTo WrongPath
ChDir (FolderName)
If Mid(FolderName, 2, 1) = ":" Then ChDrive (FolderName)
On Error GoTo 0
FolderName = CurDir() & "\"
SearchPath = FolderName & "*.rep"
ChangeFrom = Me.cboxFrom.Value
Set ChangeTo = UnvList.Item(Me.cboxTo.ListIndex + 1)
DocName = Dir(SearchPath) 'Get first document and loop through the list
ix = 1
Do While DocName <> ""
ReDim Preserve DocList(ix)
DocList(ix) = DocName
DocName = Dir() ' Get next document
ix = ix + 1
Loop
DocCount = ix - 1
CountDP = 0
CountDocs = 0
ErrCount = 0
Application.Interactive = False
For iy = 1 To DocCount
DirtyDoc = False ' True means save document
DocName = FolderName & DocList(iy) ' Give full path, or it will try to open it in UserDocs
Set CurDoc = Docs.Open(DocName, True) ' Open with NoRefresh (true)
Set DPList = CurDoc.DataProviders
For ix = 1 To DPList.Count ' For each data provider
Set CurDP = DPList.Item(ix)
CurUnvName = CurDP.UniverseName
If CurUnvName = ChangeFrom Then ' If it was the universe we are looking for...
CountDP = CountDP + 1
DirtyDoc = True
On Error GoTo ChgUnvError
CurDP.ChangeUniverse ChangeTo ' Change it to the new one
On Error GoTo RefreshError
CurDP.Refresh ' .......... And Refresh the DP
On Error GoTo 0
End If
Next ix
If DirtyDoc Then
CountDocs = CountDocs + 1
CurDoc.Save
End If
CurDoc.Close
Next iy
Application.Interactive = True
MsgBox "Modified " & CountDP & " data providers in " & CountDocs & " documents"
If ErrCount > 0 Then
ErrDisplay = "Errors occured :" & Chr$(13)
For ix = 1 To ErrCount
ErrDisplay = ErrDisplay & ErrDocs(ix) & Chr$(13)
Next
MsgBox ErrDisplay
End If
Unload Me
Exit Sub
RefreshError:
ErrCount = ErrCount + 1
ReDim Preserve ErrDocs(ErrCount)
ErrDocs(ErrCount) = "Could not refresh " & CurDoc.Name
MsgBox "refresh error"
Resume Next
ChgUnvError:
Application.Interactive = True
MsgBox "Problem changing the universe on DP " & CurDP.Name & " of document " & CurDoc.Name
Unload Me
Exit Sub
WrongPath:
Application.Interactive = True
MsgBox "Check the path."
Exit Sub
End Sub
Note: This code is not functional by itself. You should download the add-in (rea file) in order to get the form that is called.
ChangeUniverse.zip (25.0 KB)
BOB Downloads (BOB member since 2003-05-05)