Copy file to memory

focus10

Registered User.
Local time
Today, 13:18
Joined
Mar 8, 2009
Messages
38
is there is a code to copy a file into memory (for pasting it) by giving file name and path?

thanks ahead
 
Do you mean copy the whole file from one location to another (FileCopy function?) OR copy the contents on to the clipboard for pasting into another application?
 
i mean copy the file to the clipboard for pasting it into another application
equal to Ctrl+c when selecting a file in disc
 
So you want to pass the filepath of a file to a procedure, have it open the file and copy the contents onto the clipboard? What file type is the file you are copying and what is the application you want to paste into?

If this is excel to excel, then simply use the filepath to open the spreadsheet, select all and copy and close the spreadsheet. Then paste.
 
no need to open the file or read it's contents.
just copying any kind of file onto clipboard
by passing the filepath of a file to a procedure
 
Sorry i must be really dense today.

if you were doing this manually;
  1. Navigate to file in Explorer
  2. Right click copy
  3. Open another application
  4. Paste file
Is this right?


Do you want to paste the contents of the file to the other application or just the file path?
 
1. copying to clipboard by passing the filepath of a file to a procedure ("c:\a.exe")
2. manually Navigate in Explorer to d:\
3. Right click paste
 
Ah!

So that pretty much goes back to me first response, FileCopy function.

FileCopy(Source as String, Destination as String)

So you can either provide source and destination paths OR you could use the FileDialog method to obtain the destination path.

the below code will prompt the user to select the file and destination folder.
Code:
Sub CopytoNew()

Dim fdPath As Office.FileDialog
Dim varFile As Variant
Dim lngCnt As Long
Dim strFrom, strFile, strTo As String
   
' Obtain Source File
Set fdPath = Application.FileDialog(msoFileDialogOpen)
 
With fdPath
    ' Allow user to make 1 selection only
    .AllowMultiSelect = False
    
    .Title = "Select file to copy"
    ' Show the dialog box. If the .Show method returns True, the
    ' user picked at least one file. If the .Show method returns
    ' False, the user clicked Cancel.
    .Show
      
    For lngCnt = 1 To .SelectedItems.Count
        strFrom = .SelectedItems(lngCnt)
        'get file name from path
        strFile Right(strFrom, Len(strFrom) - InStrRev(strFrom, "\"))
    Next lngCnt
End With

Set fdPath = Nothing

'Obtain destination folder
Set fdPath = Application.FileDialog(msoFileDialogFolderPicker)

With fdPath
    .Title = "Select destination folder"
    .Show
      
    For lngCnt = 1 To .SelectedItems.Count
        strTo = .SelectedItems(lngCnt)
    Next lngCnt
End With

FileCopy strFrom, strTo & "\" & strFile
   
End Sub
 

Users who are viewing this thread

Back
Top Bottom