Jump to content
Macro Express Forums

How to copy a file to the clipboard


Recommended Posts

Does anyone know a way to programmatically copy a file, via Macro Express, without navigating through the Windows user interface?

 

For example, let's say there is a file on my hard drive that I frequently need to copy to the clipboard:

 

c:\users\docs\tips.pdf

 

I don't want to do this, which works, but is occasionally unreliable:

Open Folder to "c:\users\docs" // Open the folder that contains the file
Delay: 500 milliseconds
Text Type (Simulate Keystrokes): tips // Navigate to a file that starts with "tips" via the incremental search built into a folder
Delay: 200 milliseconds
Clipboard Copy
Delay: 400 milliseconds
Window Close: c:\users\docs // Close the folder that contains the file

In other words, is there a programmatically way to copy a file to the clipboard with MEP? I think there is a way to do it with files that contain graphics, but not for doc files, pdfs, etc.

Link to comment
Share on other sites

Hi Alan,

 

Not 'programmatical', but it seems to work OK for me:

 

// This copies 'the file'. NOT the file's contents and NOT the file's path/filename. Presumably the only practical next step would be to copy it into another folder. So...
// It is asumed that an Explorer folder is open and active when this macro is run. This will be the destination for the copied file.

Text Type (Simulate Keystrokes): <ALT>d
Clipboard Copy
Variable Set String %tFolderFullTitle% from the clipboard contents
Copy File/Files: "C:\TestC\Alan.txt" to "%tFolderFullTitle%"

<COMMENT Value="This copies 'the file'. NOT the file's contents and NOT the file's path/filename. Presumably the only practical next step would be to copy it into another folder. So..."/>
<COMMENT Value="It is asumed that an Explorer folder is open and active when this macro is run. This will be the destination for the copied file."/>
<COMMENT/>
<TEXT TYPE Action="0" Text="<ALT>d"/>
<CLIPBOARD COPY/>
<VARIABLE SET STRING Option="\x02" Destination="%tFolderFullTitle%" NoEmbeddedVars="FALSE"/>
<COPY FILE/FILES Source="C:\\TestC\\Alan.txt" Dest="%tFolderFullTitle%" Progress="FALSE" Recurse="FALSE"/>

CopyFile-Alan.mex

Link to comment
Share on other sites

I don't think there is. I do it very easily in VB but I checked online and the first post I found claims that VBScript does not support clipboard. VBScript is not as capable as VB. Perhaps one of the other scripting languages do.

Link to comment
Share on other sites

Thank you Terry and Cory for mulling this with me. My solution of drilling through the UI is not particularly elegant, but it does get the job done.

 

My macro is designed to automate the process of preparing an email message in Outlook that includes a certain attachment.

 

My first attempt was to use Macro Express to manipulate the UI of Outlook to select the attachment. The macro worked, but sometimes misfired.

 

My second attempt is the subject of this thread: Copy the file in Windows Explorer, and paste it directly into an Outlook message. (Not many people realize this, but a file copied to the clipboard and pasted directly into an Outlook message becomes an attachment.) Attempt 2 works faster and is more reliable than Attempt 1, but it's not pretty.

 

Cory's message has given me an idea that I will eventually try: use Outlook VBA.

Link to comment
Share on other sites

If you know the location of the file you can do this:

Variable Set From Misc:  "Path to My Documents" into %MyDocuments% // Get the path to the Documents folder
Variable Set String set %FileContents% to the contents of %MyDocuments%\MyFile.txt // Load the file into a variable
Variable Modify String: Save %FileContents% to the clipboard // Copy the variable to the clipboard

Link to comment
Share on other sites

Excellent ideas everybody!

 

I found another solution that works reliably, is fast, does not involve the clipboard, and as a bonus, includes a mechanism for inserting the subject line and the body text. This solution does involve sending keystrokes to the user interface, but only minimally.

 

It's a hybrid MEP + VBA macro:

 

1. A Macro Express script triggers the VBA script via the Outlook UI.

2. The VBA does its magic entirely programatically,

3. Macro Express sends a few keystrokes to finish the job.

 

In Outlook, open the VBA Editor, add a new module, and insert this code for a script called AddAttachment. Note that the script also defines the subject line and the body text. (Both are optional.)

Sub AddAttachment()
     Dim myItem As Outlook.MailItem
     Dim myAttachments As Outlook.Attachments

     Dim AttachmentName, BodyText, SubjectText As String

     Let AttachmentName = "c:\test.pdf"
     Let BodyText = "Thank you for writing. The attachment contains a full explanation."
     Let SubjectText = "Reply to your Question."

     Set myItem = Application.CreateItem(olMailItem)
     Set myAttachments = myItem.Attachments
     myAttachments.Add AttachmentName
     myItem.Subject = SubjectText
     myItem.Body = BodyText
     myItem.Display
End Sub

In MEP, create a macro with window specific scope of:

 

- Message (

 

This forces the script to be recognized in any Outlook email message.

 

MEP opens the Outlook "Macro" window (Alt + F8), types in the name of the VBA script, and runs it (Alt + R). The VBA code inserts the attachment, subject and body text. Then Macro Express moves keyboard focus into the body field and moves the cursor to the end:

Text Type (Simulate Keystrokes): <ALT><F8>
Text Type (Simulate Keystrokes): AddAttachment
Text Type (Simulate Keystrokes): <ALT>r
Text Type (Simulate Keystrokes): <SHIFTD><TAB><TAB><TAB><TAB><TAB><SHIFTU>
Text Type (Simulate Keystrokes): <CONTROL><END>
Link to comment
Share on other sites

Funny, I was going to suggest that the other day but got busy. If you're interested I've been writing simple apps using the MS Office interop to automate Outlook. VBA sucks compared to VB.NET and it's nice not to have to manage VB code in OL. EG I could write an app for you that simply launch and does it all using Outlook but invisibly and instantly without bothering the user. I writing one now that prompts the user using the OL FolderPicker and goes into each mail message and tries to locate a client ID number in the subject and message body and automatically files it as an MSG file on the server. Also of course if you're sending email and you don't need to use Outlook one can easily build an application with .NET to send automated messages. Don't even need OL installed. and it's SUPER simple. I have several I write to send batches with variable attachments and contents or notices on demand. Of course it's not worth it unless you are doing a large number or are having problems getting MEP to d a specific thing.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...