Clipboard paste in macro

Status
Not open for further replies.

Imbizile

Member
Outlook version
Outlook 2010 64 bit
Email Account
Exchange Server
Im struggling to make a macro that adds a specific text, lets sat "CRM ", plus then content of my clipboard, at the end of the subject of all messages I have selected. This following works great, but is missing the clipboard paste, as the "pastedtext" should be the actual clipboard data.

Sub Renamemails()
Dim ex As Explorer
Dim mail As MailItem
Dim strTemp As String
Set ex = Application.ActiveExplorer

For Each mail In ex.Selection
strTemp = mail.Subject & " CRM " & "pastedtext"
mail.Subject = strTemp
mail.Save
Next mail

End Sub

It seems there is no simple code just to paste in Outlook, at least i havent been able to find it, so any help get the the actual clipboard text into the subject would be highly appreciated :)

Thanks in advance :)

/Imbizile
 
Prior to running this macro i will have made a CTRL+C on a number eg. 1658794533, and i thought it would be an easy job to get that into a string.

So there is no way to get a simple CTRL+V into a String variable in outlook?
 
I messed a bit more with this and figured I could do this through an Inputbox to get my clipholder number into a variant, by doing a CTRL+V when it pops up. I also added Exit Sub, in case I press escape or dont enter anything, so my code now looks like this:

Sub Rename()
Dim ex As Explorer
Dim mail As MailItem
Dim strTemp As String
Set ex = Application.ActiveExplorer
Dim strNumber As Variant
strNumber = InputBox("Paste number")

If strNumber = False Then Exit Sub
If strNumber = "" Then Exit Sub

For Each mail In ex.Selection
strTemp = mail.Subject & " CRM " & strNumber
mail.Subject = strTemp
mail.Save
Next mail

End Sub

This works as intended but it is still an unnecessary step, as I would prefer to get it into the variant, without the "pop-up, CTRL+V, press return" act. Any ideas to do this or is this procedure as "shaved" as possible in Outlook?
 
Kewl. It was easier than I thought... and no Win API needed. UserForms support paste from clipboard, so you just need to call a userform object (no actual userform needed though).

As an FYI, using the win api *should* only be a few lines in a function but I couldn't find any samples online. It doesn't really matter because you can't beat 3 lines.

This is the macro I tested with -

Sub Rename()
Dim ex As Explorer
Dim mail As MailItem
Dim strTemp As String
Set ex = Application.ActiveExplorer
Dim strNumber As Variant

Dim DataObj As MSForms.DataObject

Set DataObj = New MSForms.DataObject

DataObj.GetFromClipboard

strNumber = DataObj.GetText(1)

MsgBox strNumber
'If strNumber = False Then Exit Sub

' If strNumber = "" Then Exit Sub
For Each mail In ex.Selection
strTemp = " CRM " & strNumber
mail.Subject = strTemp
mail.Save
Next mail

End Sub
 
Amazing, this was exactly what I was looking for. Edited the macro a bit and now it does what its supposed to, without pop-ups or anything :)

Oddly enough I got the "User-defined type not defined" error at first and read up on it and figured I had to make a reference to Microsoft Forms 2.0 Object Library, but it was not in my references, so had to add it manually from c:\Windows\SysWOW64\FM20.dll.

But as I said it works like a charm now and I really appreciate the input.

Thanks a bunch :)
 
Hi,

This thread was very helpful as I tried to find a solution to my problem - pasting from the clipboard into an Outlook message BUT taking the formatting of the text I'm pasting into. For example, I copied some heavily formatted text from a Word doc onto my clipboard. Now I want to paste that into the middle of a paragraphi I'd already written in an Outlook message. I want the pasted text to match the rest of the paragraph, not retain the formatting of the document I copied it from.

In Word I have this macro that does just what I want, and I assign it to Control + shift + V so I have a shortcut that does this.
Sub PasteUnformatted()

'

' PasteUnformatted Macro

'

'
Selection.PasteAndFormat (wdFormatPlainText)

End Sub​

I saw that your two macros each managed to paste from the clipboard into an Outlook message. I tweaked and tweaked but couldn't get either of yours to do what I need.

I tried figuring out a variant of the PasteAndFormat command from Word but no luck.

Would you have any counsel to offer? I'd be grateful. This "copy from Word and paste into Outlook" is something I do a lot and am forever needing to then highlight the pasted text and match the formatting of the paragraph into which I pasted it. My Word macro fixed it but I cannot make a working one for Outlook

Thanks,

S
 
I should have mentioned that one of Diane's posts on SlipStick solved another long-standing frustration of mine - a macro to format a selected block of text and a Quick Access toolbar button to fire it off. Very nice, a big time saver for me.
 
Thank you for the very fast reply.

I had seen that one and tried modifying it. It does paste unformatted text, but it pastes it into a new Appointment object. I need to paste it into whatever email I am working on at the time, the one that has "focus" on my desktop.

I confess I'm over my head in modifying the code below, from the one you suggested, so it pastes into my email instead of creating a new Appointment object and pasting unformatted into it.

Sub PasteUnFormattedClipboard()

On Error Resume Next

Dim olCal: Set olCal = Application.CreateItem(1)

olCal.Subject = "Testing " & Now

olCal.Location = "Here"

olCal.Display

Dim objItem As Object

Dim objInsp As Outlook.Inspector

Dim objWord As Word.Application

Dim objDoc As Word.Document

Dim objSel As Word.Selection

Set objItem = olCal ' Application.ActiveInspector.currentItem

Set objInsp = objItem.GetInspector

Set objDoc = objInsp.WordEditor

Set objWord = objDoc.Application

Set objSel = objWord.Selection

objSel.PasteAndFormat (Word.WdRecoveryType.wdFormatPlainText)

Set objItem = Nothing

Set objInsp = Nothing

Set objDoc = Nothing

Set objWord = Nothing

Set objSel = Nothing

End Sub​

S
 
i'm on my tablet and can't put them together, but you need to use the lines that dim objitem and below, and set the word objects. you don't need the lines at the top that create the outlook but you will need to reference the item you want to paste in - that is the set objitem = ocal line. going from memory, it would be set objitem = application.currentitem something. (the macro i have that formats selected text should reference the current item. )
 
Ah the horrors of a small tablet screen - on the big screen i now see the code you would use for current item is right in the line:

Set objItem = olCal ' Application.ActiveInspector.currentItem

Just need to delete the olCal ' part. :)
 
That's the guidance I needed, thank you. My completed macro follows. The key was setting the PasteAndFormat command to Word.wdFormatPlainText.

Sub Paste_Un_FormattedClipboard()
On Error Resume Next


Dim objItem As Object

Dim objInsp As Outlook.Inspector

Dim objWord As Word.Application

Dim objDoc As Word.Document

Dim objSel As Word.Selection

Set objItem = Application.ActiveInspector.CurrentItem

Set objInsp = objItem.GetInspector

Set objDoc = objInsp.WordEditor

Set objWord = objDoc.Application

Set objSel = objWord.Selection

objSel.PasteAndFormat (Word.wdFormatPlainText)

Set objItem = Nothing

Set objInsp = Nothing

Set objDoc = Nothing

Set objWord = Nothing

Set objSel = Nothing

End Sub

Thank you for the assistance. I’ve been trying to create these macros in Outlook 2010’e editor for ages, though only very intermittently. They will save me a lot of time. Thank you again.

S
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
D Outlook macro with today's date in subject and paste clipboard in body Outlook VBA and Custom Forms 1
K Paste from Windows Clipboard then change font without losing formatting Outlook VBA and Custom Forms 1
Diane Poremsky Paste clipboard contents using VBA Using Outlook 0
K How to copy Contact Item to Clipboard and Paste as "VCF Link? Outlook VBA and Custom Forms 4
S Macro to extract email addresses of recipients in current drafted email and put into clipboard Outlook VBA and Custom Forms 2
A Clipboard Resize Issue Using Outlook 7
B Programmatically force html send and insert clipboard contents into body Outlook VBA and Custom Forms 0
M Retrieve data from GAL and put it in Clipboard Using Outlook 1
F How can i copy the mail subject and the link to the mail to th clipboard? Outlook VBA and Custom Forms 3
R Outlook 2013 VBA I want to put the entire message to clipboard Using Outlook 5
V Screenshots / Clipboard images disappear after sending Using Outlook 3
M Clipboard Manager Not showing in Outlook 2010 on Start Using Outlook 8
J PSA: How to create custom keyboard shortcut for "Paste Unformatted Text" in Outlook on Windows Outlook VBA and Custom Forms 1
P Outlook 2016 Change Paste Special Default Format Using Outlook 8
P Can't paste an image into a task Using Outlook 3
P Add Paste Unformatted to QAT Using Outlook 1
B Paste formatted text (bold, underlined and in quotes) Outlook VBA and Custom Forms 1
J VBA Outlook : Subject line : Cut and Paste name to heading , number to very end of the body of Email Outlook VBA and Custom Forms 1
O Macro - paste as plain text Outlook VBA and Custom Forms 2
D Paste Excel table into Outlook message Outlook VBA and Custom Forms 6
O How to paste website content using a specific font and removing URLs Using Outlook 2
N Paste content to Excel when .txt file (attachment) is opened Outlook VBA and Custom Forms 1
Stilgar Relsik Create a rule to copy text from an email and paste it in the subject line. Using Outlook 1
H Macro to Copy Specific content from Mail Body and Paste to Excel Outlook VBA and Custom Forms 4
A Outlook: copy & paste Outlook VBA and Custom Forms 9
V Copy and paste body and subject and send multiple emails Outlook VBA and Custom Forms 3
G outlook 13 copy & paste without losing formatting Using Outlook 1
C Copy Cell value from Excel and paste into current email Outlook VBA and Custom Forms 10
R Copy/paste mailing address Outlook 2013 Using Outlook 6
D Open attached CSV, copy newdata and paste it to database CSV Outlook VBA and Custom Forms 2
O Copy email content and paste into new Word Document using a different font Using Outlook 1
makinmyway Research Pane in Emails Turns on and Cut Paste Keys then Stop Working Using Outlook 5
L Outlook 2007 Delete and Paste Between Fields Using Outlook 25
L "sometimes" cant send mail, we have to copy and paste the message and resend? Using Outlook 2
F Cut and Paste Addresses from Excel Suddenly Limited Using Outlook 4
N Cut and Paste Taking 25 seconds Using Outlook 0
R how to copy a list of email contacts and paste them only as names (not names + email address) Using Outlook 12
G Outlook 2007 Macro: Paste - Paste Special - Unformatted Text Outlook VBA and Custom Forms 9
K Copy Entire Email Content - Paste into new Task Outlook VBA and Custom Forms 2
Z unable to paste screenshot into custom form Outlook VBA and Custom Forms 2
S Using copy paste to grab email addresses from the TO: address fiel Using Outlook 14
J Outlook macro to run before email is being send Outlook VBA and Custom Forms 0
H Macro to Delete Duplicate items in Outlook calendar where title is the same and date is the same Outlook VBA and Custom Forms 0
X Custom icon (not from Office 365) for a macro in Outlook Outlook VBA and Custom Forms 1
X Run macro automatically when a mail appears in the sent folder Using Outlook 5
mrrobski68 Issue with Find messages in a conversation macro Outlook VBA and Custom Forms 1
G Creating Macro to scrape emails from calendar invite body Outlook VBA and Custom Forms 6
M Use Macro to change account settings Outlook VBA and Custom Forms 0
J Macro to Reply to Emails w/ Template Outlook VBA and Custom Forms 3
C Outlook - Macro to block senders domain - Macro Fix Outlook VBA and Custom Forms 1

Similar threads

Back
Top