Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 09-12-2012, 11:30 PM
kyjac85 kyjac85 is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: Sep 2012
Posts: 11
kyjac85 is on a distinguished road
Post 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.
Reply With Quote
  #2  
Old 09-13-2012, 03:09 AM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,989
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

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
I don't know anything about transcription foot pedals, so I can't help you on that front.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #3  
Old 09-13-2012, 09:41 AM
kyjac85 kyjac85 is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: Sep 2012
Posts: 11
kyjac85 is on a distinguished road
Default

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.
Reply With Quote
  #4  
Old 09-13-2012, 11:27 AM
kyjac85 kyjac85 is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: Sep 2012
Posts: 11
kyjac85 is on a distinguished road
Default

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/
Reply With Quote
  #5  
Old 09-13-2012, 04:39 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,989
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

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
[MS MVP - Word]
Reply With Quote
  #6  
Old 09-13-2012, 07:02 PM
kyjac85 kyjac85 is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: Sep 2012
Posts: 11
kyjac85 is on a distinguished road
Default

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.
Reply With Quote
  #7  
Old 09-13-2012, 07:16 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,989
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

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
[MS MVP - Word]
Reply With Quote
  #8  
Old 09-14-2012, 01:03 AM
kyjac85 kyjac85 is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: Sep 2012
Posts: 11
kyjac85 is on a distinguished road
Default

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.
Reply With Quote
  #9  
Old 09-14-2012, 02:12 AM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,989
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

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
[MS MVP - Word]
Reply With Quote
  #10  
Old 09-20-2012, 03:11 PM
kyjac85 kyjac85 is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: Sep 2012
Posts: 11
kyjac85 is on a distinguished road
Default

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
It keeps telling me Compile error: User-defined type not defined.
Reply With Quote
  #11  
Old 09-20-2012, 03:58 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,989
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

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
[MS MVP - Word]
Reply With Quote
  #12  
Old 09-20-2012, 04:02 PM
kyjac85 kyjac85 is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: Sep 2012
Posts: 11
kyjac85 is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
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.
I don't see a Forms reference, only "Feed". I added that one and restarted but no luck. I still get the compile error. It highlights the "Sub insertclipboard()" line in yellow if that makes a difference.

thanks
Reply With Quote
  #13  
Old 09-20-2012, 04:52 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,989
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

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
[MS MVP - Word]
Reply With Quote
  #14  
Old 09-20-2012, 05:00 PM
kyjac85 kyjac85 is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: Sep 2012
Posts: 11
kyjac85 is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
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.
Ok I figured it out. I repaired the installation but that didn't work, so I just went to Tools > References > Browse and opened FM20.dll. Now everything is back in working order.
Reply With Quote
  #15  
Old 09-20-2012, 05:03 PM
kyjac85 kyjac85 is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: Sep 2012
Posts: 11
kyjac85 is on a distinguished road
Default

EDIT: so now it appears I have another problem. Runtime error 4198. I think it has to do with the clipboard being cleared because it works as long as I have something in my clipboard, but the second time I run it it gives me this error. Also, debug stops at ".Paste". So I'm guessing that since there is nothing for it to paste on the second run it gives an error? It's weird though...this worked perfectly before.
Reply With Quote
Reply

Thread Tools
Display Modes


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 0 04-29-2010 01:43 AM


All times are GMT -7. The time now is 06:24 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft