|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
A macro that can insert FILENAME, sendkeys CTRL ALT T, paste clipboard, and nextline
I'm new to this macro programming and could use a few pointers.
The first part of this macro I would like it to insert the file name of the current document without the file type extension as well as the last three characters of the filename. For example: "myworddoc_XX.doc" = "myworddoc" ... deleting the last three characters and filetype is not a necessity, but it would be nice. The second part of the macro would simulate keystrokes using sendkeys I believe. I have another program that dumps text to the clip board when you press CTRL + ALT + T, so I just need it to simulate me pressing and holding CTRL + ALT, and then just pressing the letter T. Afterwards, I need to paste the clipboard directly behind the "myworddoc" and then press ALT ENTER to put the cursor on the line directly below. Reading around I can't figure out how to get sendkeys to work properly. I get the impression that it isn't a very reliable command to use as well. Also, Is it possible to bind hotkeys to anything other than the keyboard or MS buttons? I have a transcription foot pedal that would be awesome if I could bind the hotkey to it, but my google searches left me hopeless. |
#2
|
||||
|
||||
Hi kyjac85,
Try the following macro: Code:
Sub InsertData() Application.ScreenUpdating = False SendKeys "^%T" With Selection .Text = Split(Split(ActiveDocument.Name, ".")(0), "_")(0) .Collapse wdCollapseEnd .Paste End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Only the sendkeys part doesn't work for me. I've tried other keys and I've decreased macro security settings in case that was interfering, but nothing works. It will paste whatever is in the clipboard, but not press CTRL ALT T. I have to press it manually for it to work. Also, it doesn't seem to be moving to a new line. Thanks for writting it out for me
Last edited by kyjac85; 09-13-2012 at 11:37 AM. |
#4
|
|||
|
|||
Also...Incase anyone is interested, I found a program that allows you to remap foot pedals to keyboard shortcuts. http://www.nch.com.au/footpedal/
|
#5
|
||||
|
||||
Hi kyjac85,
Try the following revision to the macro. I've taken out the SendKeys reference (it may be that your keyboard intercept doesn't recognise it) and added code for the new line: Code:
Sub InsertData() Application.ScreenUpdating = False With Selection .Text = Split(Split(ActiveDocument.Name, ".")(0), "_")(0) & Chr(11) .Collapse wdCollapseEnd .Paste End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#6
|
|||
|
|||
Thanks. Do you think there is any way for me to get it to recognize sendkeys? I've read all over the place about the sendkeys not working, and it seems like many people have this problem, but there is no solution.
|
#7
|
||||
|
||||
Hi kyjac85,
The problems with using SendKeys usually fall into two categories: (a) the solution is often tied to a specific version of the underlying application (and may even require a specific configuration of that application); and (b) timing, where the keystroke sequence (which relies on working with the user interface) is transmitted too fast for the application to process. Neither of those is likely to apply here, as we're dealing with a single keystroke combination. In coding the SendKeys command, I assumed the Ctrl-Alt-T sequence could be executed from within Word. However, as it involves another (as yet unnamed) program, what might actually be required (if that program supports it) is automating that program from Word. That would require an entirely different approach.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#8
|
|||
|
|||
Do you know how I can clear my clipboard after the scripts runs? I tried selection.copy but it isn't working for me. I believe I need to copy a blank character into the clipboard...which would suffice.
|
#9
|
||||
|
||||
Hi kyjac85,
After the 'End With', you can clear the clipboard by adding: Code:
Dim MyData As DataObject Set MyData = New DataObject MyData.SetText "" MyData.PutInClipboard
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#10
|
|||
|
|||
I don't know why but this code is not working for me anymore. I haven't changed any of the code either. I'm trying to find an answer to this, but so far the adding reference libraries hasn't worked yet.
Code:
Sub InsertClipboard() ' ' InsertClipboard Macro ' ' Application.ScreenUpdating = False With Selection .Text = Split(Split(ActiveDocument.Name, ".")(0), "_")(0) & Chr(11) .Collapse wdCollapseEnd .Paste End With Dim MyData As DataObject Set MyData = New DataObject MyData.SetText "" MyData.PutInClipboard Application.ScreenUpdating = True End Sub |
#11
|
||||
|
||||
Hi kyjac85,
Sorry, I omitted to mention that you need to set a vba reference (Tools|References) to the Microsoft Forms 2.0 Object Library, if it's not already checked. You shouldn't arbitrarily add other references, though.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#12
|
|||
|
|||
Quote:
thanks |
#13
|
||||
|
||||
Have you confirmed whether the vba reference to the Microsoft Forms 2.0 Object Library is already checked?
If it's missing (highly unlikely), you'll need to repair your Office installation. You can do this via Programs & Features > Microsoft Office > Change in the Windows Control Panel.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#14
|
|||
|
|||
Quote:
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
paste and insert, not replace | userman | Excel | 4 | 06-06-2012 05:15 AM |
Problem with the sendkeys in Win7 | vidyapakki | Excel Programming | 1 | 05-07-2012 11:10 PM |
Insert/Paste Picture into Protected Worksheet | udea | Excel | 1 | 02-06-2012 07:43 PM |
How to insert current date into default filename ? | czomberzdaniela | Word | 1 | 12-27-2011 07:18 PM |
Macro for automatically adding filename without .doc | RPM7 | Word VBA | 0 | 04-29-2010 01:43 AM |