Partager via


Moving/Copying (ADO)

Topic Last Modified: 2006-06-12

Example

Visual Basic

Note

The following example uses a file URL with the Exchange OLE DB (ExOLEDB) provider. The ExOLEDB provider also supports The HTTP: URL Scheme. Using The HTTP: URL Scheme allows both client and server applications to use a single URL scheme.

' CDO: Moving/Copying Using CDOEX
' This sample shows how to move/copy objects.
' Add a reference to Microsoft ActiveX Data Objects 2.5 Library.
' Add a reference to Active DS Type Library.


Private Sub Command1_Click()
    Dim strPathOfSourceObject As String
    Dim strPathOfDestFolder As String
    Dim strDomainName As String
    Dim strUser As String

    ' Specify the domain and the user.
    strDomainName = GetDomainDNSName()
    strUser = "user1"


    ' Sample 1: Copy folders in MBX.
    strPathOfSourceObject = "MBX/" & strUser & "/Outbox/HelloFolder"
    strPathOfDestFolder = "MBX/" & strUser & "/Deleted Items"
    Call CopyMoveObjects(strDomainName, strPathOfSourceObject, strPathOfDestFolder, 0)


    ' Sample 2: Move folders in Public Folders.
    strPathOfSourceObject = "Public Folders/SourceFolder"
    strPathOfDestFolder = "Public Folders/DestFolder"
    'Call CopyMoveObjects(strDomainName, strPathOfSourceObject, strPathOfDestFolder, 1)


    ' Sample 3: Move a file in MBX.
    strPathOfSourceObject = "MBX/" & strUser & "/Outbox/TestFolder/Hello.txt"
    strPathOfDestFolder = "MBX/" & strUser & "/Deleted Items"
    'Call CopyMoveObjects(strDomainName, strPathOfSourceObject, strPathOfDestFolder, 0)

    Unload Me
End Sub



' bOption:
' False: copy
' True - Move
'
Private Sub CopyMoveObjects(strDomainName As String, _
                                strLocalPathOfSourceObject As String, _
                                strLocalPathOfDestFolder As String, _
                                bOption As Boolean)
    Dim Rec As New ADODB.Record
    Dim strSourceObjectUrl As String
    Dim strDestFolderURL As String
    Dim strNewFolderName As String

    ' Set the strURL to the location of the folders.
    strSourceObjectUrl = "file://./backofficestorage/" & _
                 strDomainName & "/" & strLocalPathOfSourceObject

    ' Open the record.
    ' The adModeReadWrite enum is required.
    Rec.Open strSourceObjectUrl, , adModeReadWrite
    strNewFolderName = Rec.Fields("DAV:displayname")

    strDestFolderURL = "file://./backofficestorage/" & _
                 strDomainName & "/" & strLocalPathOfDestFolder

    If bOption = False Then

       ' Copy the folder from the source location to the destination folder.
       ' Note that if an item already exists at the destination URI,
       ' it will be overwritten by the copy.
       Rec.CopyRecord strSourceObjectUrl, strDestFolderURL & "/" & _
          strNewFolderName, , , adCopyOverWrite

    ' The adModeReadWrite enum is required.
    Else
       ' Move the folder from the source location to the destination folder.
       ' Please note that if an item already exists at the destination URI,
       ' it will be overwritten by the copy.
       Rec.MoveRecord strSourceObjectUrl, strDestFolderURL & "/" & _
          strNewFolderName, , , adMoveOverWrite
    End If


    ' Close the record.
    Rec.Close

    ' Clean up.
    Set Rec = Nothing

    If Err.Number = 0 Then
      Debug.Print "Good Job!"
    End If

End Sub