DrTribos
asked on
Macro to capture keystrokes to use in Select Case or similar --> to apply styles to paragraphs
Hi
I have some code that relies on a user form to apply styles to paragraphs in a document. Problem is that some of the styles are applied very frequently and I would like to use a keyboard short-cut to apply.
Not sure how to do this programatically.
Kind regards
Steve
I have some code that relies on a user form to apply styles to paragraphs in a document. Problem is that some of the styles are applied very frequently and I would like to use a keyboard short-cut to apply.
Not sure how to do this programatically.
Kind regards
Steve
Sub theStylist1()
Dim Para As Paragraph
Dim myForm As styleBox 'using a user form to select styles
Dim rng As Range
Dim rng2 As Range
Dim paraLen As Long
If MsgBox("Do you want to resume from the selection point", vbYesNo) = vbYes Then
Set rng = Selection.Range
rng.End = ActiveDocument.Range.End
Else
Set rng = ActiveDocument.Range
End If
Set myForm = New styleBox
For Each Para In rng.Paragraphs
'Don't want to waste time on empty lines --> they can all be set to normal
Set rng2 = Para.Range
If Len(rng2.Text) = 1 Then
Para.Style = "Normal"
Else
On Error GoTo errNoTable
If rng2.Cells.Count = 1 Then
If rng2.End = rng2.Cells(1).Range.End Then
'Avoid selecting the cell marker
rng2.MoveEnd wdCharacter, -1
End If
End If
errNoTable:
If Err = 5907 Then
MsgBox Para.Style & " No Table, Please Check Document"
Para.Style = "Normal"
Resume errNextTable
End If
rng2.Select 'select to show paragraph to user
paraLen = Selection.End - Selection.Start
If paraLen = 1 Then
Para.Style = "Normal"
Else
myForm.Caption = Para.Style
myForm.Show
Select Case myForm.Tag
Case 99
Unload myForm
Set myForm = Nothing
Exit Sub
Case 0
'do nothing
Case 1
Para.Style = "Normal"
Case 2
Para.Style = "TaskStep"
Case 3
Para.Style = "Function"
Case 4
Para.Style = "Sub-Step"
Case 5
Para.Style = "SubStepBullet"
Case 6
Para.Style = "Caution"
Case 7
Para.Style = "Note"
Case 8
Para.Style = "Task End"
Case 9
Para.Style = "Task Name"
Case 10
Para.Style = "Table Sub Heading"
Case 11
Para.Style = "Procedure Title"
Case 12
Para.Style = "TaskStep"
Case 13
Para.Style = "Heading 1"
Case 14
Para.Style = "Heading 2"
Case 15
Para.Style = "Heading 3"
End Select
End If
End If
errNextTable:
Next Para
End Sub
Why not just create keyboard shortcuts for those actual styles? Then the user won't need your macro at all. Just select the paras and hit the appropriate keyboard shortcut.
ASKER
Too many button presses/mouse movements. The code goes through the document and selects the paragraphs for me. I have about 2000 documents to convert :-( so I just want to be able to press a button to assign an appropriate style.
The other thing that I need, which I omitted from my original question, is for the selected text to be centred vertically on the screen.
Cheers
Steve
The other thing that I need, which I omitted from my original question, is for the selected text to be centred vertically on the screen.
Cheers
Steve
I would like to be able to help you but I don't understand:
1. Why you want something centered vertically ON SCREEN
and
2. Do you just want a button on a toolbar that will apply the style to the selected paragraphs? If so, that is fairly easy....you could also create a hotkey at the same time...
1. Why you want something centered vertically ON SCREEN
and
2. Do you just want a button on a toolbar that will apply the style to the selected paragraphs? If so, that is fairly easy....you could also create a hotkey at the same time...
ASKER
Hi JOrzech
Thank you for your question.
Basically I have several documents which I inherited (as did everyone else it seems). I need to apply styles and can create a macro which identifies styles that definitely need replacing... eg anything with "Normal" or "Body Text, 1 ,2, 3" etc. needs to have correct style applied.
So my macro runs and finds text - it selects the text but the user has to scan the page for highlighted text (i.e. selected paragraph). It would help if the selection was roughly in the same spot each time (centre of screen was an arbitrary choice). Currently selected text can be hidden by the user form or seems to be off the screen.
Retrospectively I could have the selection displayed into a message box - with a quick read of the text the user will generally know what style to apply.
I'm using toolbars, I had not thought of the possibility of using a toobar to apply style while a macro is running - hence I used a user form. However - the first time I ran my macro I cursed because I had to use the mouse so much.
So now I want this kind of functionality:
Macro searches --> finds --> selects text (and puts text in an easy to see position, eg centered on screen)
Macro spits out prompt: "style is wrong press 'F' for Function, 'R' for Responsibility, 'T' for Task etc..
(where Function, Responsibility & Task are styles that I have in my document)
Macro captures the keystroke and applies the appropriate Style
Macro repeats till all "junk" styles have been checked
Hope this helps
Kind regards
Steve
Thank you for your question.
Basically I have several documents which I inherited (as did everyone else it seems). I need to apply styles and can create a macro which identifies styles that definitely need replacing... eg anything with "Normal" or "Body Text, 1 ,2, 3" etc. needs to have correct style applied.
So my macro runs and finds text - it selects the text but the user has to scan the page for highlighted text (i.e. selected paragraph). It would help if the selection was roughly in the same spot each time (centre of screen was an arbitrary choice). Currently selected text can be hidden by the user form or seems to be off the screen.
Retrospectively I could have the selection displayed into a message box - with a quick read of the text the user will generally know what style to apply.
I'm using toolbars, I had not thought of the possibility of using a toobar to apply style while a macro is running - hence I used a user form. However - the first time I ran my macro I cursed because I had to use the mouse so much.
So now I want this kind of functionality:
Macro searches --> finds --> selects text (and puts text in an easy to see position, eg centered on screen)
Macro spits out prompt: "style is wrong press 'F' for Function, 'R' for Responsibility, 'T' for Task etc..
(where Function, Responsibility & Task are styles that I have in my document)
Macro captures the keystroke and applies the appropriate Style
Macro repeats till all "junk" styles have been checked
Hope this helps
Kind regards
Steve
Well, I guess you could put in your macro an IF STATEMENT... the logic of which would be
If USER Presses T Then
Selection.Style = ActiveDocument.Styles("Tas k")
ElseIf user Presses R Then
Selection.Style = ActiveDocument.Styles("Res ponsibilit y")
Elseif user Presses F Then
Selection.Style = ActiveDocument.Styles("Fun ction")
End if
Will that work for the one portion of your question?
If USER Presses T Then
Selection.Style = ActiveDocument.Styles("Tas
ElseIf user Presses R Then
Selection.Style = ActiveDocument.Styles("Res
Elseif user Presses F Then
Selection.Style = ActiveDocument.Styles("Fun
End if
Will that work for the one portion of your question?
ASKER
That would be the main part of the question answered... all I need is the syntax and to test it
Also I would still like to have the user form with letter underlined (or capitalised) eg. press T for Task or press X for teXt... with the option of also using mouse clicks (some ppl just can't be trained to use the keyboard!!!).
so I was originally thinking logic would be:
If USER Presses T OR Presses FormButton.Task Then
Selection.Style = ActiveDocument.Styles("Tas k")
ElseIf user Presses ...
Cheers
Steve
Also I would still like to have the user form with letter underlined (or capitalised) eg. press T for Task or press X for teXt... with the option of also using mouse clicks (some ppl just can't be trained to use the keyboard!!!).
so I was originally thinking logic would be:
If USER Presses T OR Presses FormButton.Task Then
Selection.Style = ActiveDocument.Styles("Tas
ElseIf user Presses ...
Cheers
Steve
You said:
Also I would still like to have the user form with letter underlined (or capitalised) eg. press T for Task or press X for teXt... with the option of also using mouse clicks (some ppl just can't be trained to use the keyboard!!!).
In order to do that, just put an AMPERSAND before the letter you want them to press -- as I said above -- for example --- you want them to press T for training .... then you need to type &Training and the T will be underlined.
I can't write any additional code for you unless you upload your template or your user form.....
Also I would still like to have the user form with letter underlined (or capitalised) eg. press T for Task or press X for teXt... with the option of also using mouse clicks (some ppl just can't be trained to use the keyboard!!!).
In order to do that, just put an AMPERSAND before the letter you want them to press -- as I said above -- for example --- you want them to press T for training .... then you need to type &Training and the T will be underlined.
I can't write any additional code for you unless you upload your template or your user form.....
I'm uploading a document which contains a sample user form that you need to import into your template for testing purposes. Do you know how to import a user form into a template?
DrTribos.doc
DrTribos.doc
ASKER
I'll give it a crack -
open your doc
Alt F11
R-click on module ... import...
open your doc
Alt F11
R-click on module ... import...
Close! LOL
Right click - export, save, then import into your template.....
Right click - export, save, then import into your template.....
ASKER
Um - tail between legs... I have uploaded picture, I don't know where the user form is :-(
DrTribos-UserForm1.jpg
DrTribos-UserForm1.jpg
Oh NO! Mr. Billl!!!!
My tail between legs.... I created the user form in the default location! Which is MY normal.... and I thought I had put it in your doc, Doc.
Red in face.... attaching again
DrTribos.doc
My tail between legs.... I created the user form in the default location! Which is MY normal.... and I thought I had put it in your doc, Doc.
Red in face.... attaching again
DrTribos.doc
ASKER
Still cant see it, should I be able to see it in the file menu view, like when I create my own yeah?
View is exactly same as before - however file size was slightly bigger.
View is exactly same as before - however file size was slightly bigger.
Ok - I KNOW it's in there this time! I just re-downloaded the one I uploaded and here's the screenshot - you need to click on forms....
untitled2.jpg
untitled2.jpg
ASKER
LOL - Ok, think I'll find it this time :-)
;)
Heading out home for the weekend.... hope yours is great. I don't usually get on the computer over the weekend as my two year old granddaughter comes to visit....
Heading out home for the weekend.... hope yours is great. I don't usually get on the computer over the weekend as my two year old granddaughter comes to visit....
ASKER
Got it - not sure what was going wrong - will have a play with the form and try figure out how it works.
Have a great weekend, thanks for your help.
Cheers
Steve
Have a great weekend, thanks for your help.
Cheers
Steve
ASKER
BTW - to get the form to show I restarted my windoz session....
ASKER
JOrzech Said:
I can't write any additional code for you unless you upload your template or your user form.....
^^^^^^ Sorry I missed this post ^^^^^^^^
Not sure if it helps - uploading a template for you to look at, cheers Steve
PS successfully imported your form
MacroDevelopment.doc
I can't write any additional code for you unless you upload your template or your user form.....
^^^^^^ Sorry I missed this post ^^^^^^^^
Not sure if it helps - uploading a template for you to look at, cheers Steve
PS successfully imported your form
MacroDevelopment.doc
Ok - just refresh my mind here..... you want the user to be able to press either a button on a user form or use a keystroke? It may be easier to just create a toolbar.... but let me look at what you've got Steve.
I guess I'm a little confused because your macro refers to styles which don't exist in the document you uploaded...
e.g. Hdg1 Text style....
Can you clarify for me please?
I guess I'm a little confused because your macro refers to styles which don't exist in the document you uploaded...
e.g. Hdg1 Text style....
Can you clarify for me please?
ASKER
Ahh, sorry the styles are missing from the uploaded document, an oversight - I was giving you the macro and user form.
You are right. I user to have option to be able to use mouse click a soft button (user form, toolbar or what ever might work) OR a use a *single* keystroke on the keyboard. And by *OR* I mean I want both options available to the user (not 1 or the other).
They style names could be changed to styles that already exist... heading 1, heading 2, heading 3... (my actual template has all the styles + a bunch of IP..., hence I uploaded a blank doc & forgot all about the styles)
Sorry for the confusion.
(aside - does the difficulty arise due to 2 input methods, i.e. would it be easier to have macro just wait for a single keystroke from the keyboard? If so, I guess I could live with 2 macros; 1 for keyboarders the other for mousers - although my preference is a single macro).
Kind regards
Steve
You are right. I user to have option to be able to use mouse click a soft button (user form, toolbar or what ever might work) OR a use a *single* keystroke on the keyboard. And by *OR* I mean I want both options available to the user (not 1 or the other).
They style names could be changed to styles that already exist... heading 1, heading 2, heading 3... (my actual template has all the styles + a bunch of IP..., hence I uploaded a blank doc & forgot all about the styles)
Sorry for the confusion.
(aside - does the difficulty arise due to 2 input methods, i.e. would it be easier to have macro just wait for a single keystroke from the keyboard? If so, I guess I could live with 2 macros; 1 for keyboarders the other for mousers - although my preference is a single macro).
Kind regards
Steve
Well, what I would do is modify each style to have it's own keyboard shortcut, then I would create a toolbar containing all styles in the document (and to the right of the style name, I would indicate the keyboard shortcut)....
Does that make sense to you?
Does that make sense to you?
ASKER
Makes sense - that's pretty much what lwebber said... (1st response).
If it is not possible to have macro act on single keystroke inputs then that is probably what I'll have to do. But I want the macro to run in the background because I can not rely on people to find all instances of (what I will now call) the banned styles.
I really want to apply the style with a single key stroke / button press, and so the dreary task of reapplying styles can be achieved with the computer prompting the document editor "what style should this be" and with coffee in 1 hand the document editor can push "r" on the keyboard and (for example) the style responsibility is applied.
I know I'm difficult ;-)
Would it be possible to remap the keyboard only while the macro is running, e.g. f remapped to Ctrl+f etc.?? Then revert to original map when macro ends?
Cheers
Steve
If it is not possible to have macro act on single keystroke inputs then that is probably what I'll have to do. But I want the macro to run in the background because I can not rely on people to find all instances of (what I will now call) the banned styles.
I really want to apply the style with a single key stroke / button press, and so the dreary task of reapplying styles can be achieved with the computer prompting the document editor "what style should this be" and with coffee in 1 hand the document editor can push "r" on the keyboard and (for example) the style responsibility is applied.
I know I'm difficult ;-)
Would it be possible to remap the keyboard only while the macro is running, e.g. f remapped to Ctrl+f etc.?? Then revert to original map when macro ends?
Cheers
Steve
Difficult? Hardly...but the macro can still respond with a single keystroke.... if I'm understanding you correctly. Just upload the template with the appropriate styles and I'll take a look.
:)
:)
ASKER
I have uploaded the template
MacroDevelopment2.doc
MacroDevelopment2.doc
Ok Steve:
Here's one thing I was thinking of ..... is this in line with your thoughts? They would just press the "N" for Normal.... "p" for Task Step, etc.....
Before I go any further, I want to be sure we're on the same page :)
UserForm.jpg
Here's one thing I was thinking of ..... is this in line with your thoughts? They would just press the "N" for Normal.... "p" for Task Step, etc.....
Before I go any further, I want to be sure we're on the same page :)
UserForm.jpg
ASKER
That looks exactly in line with my thoughts...
A lazy person like myself could press "N" on the keyboard to apply the Normal style, or a person who likes playing with their mouse could press the "Normal" button on the user form ;-)
Hope the annotated code in the latest uploaded template was of more use!
Cheers
Steve
A lazy person like myself could press "N" on the keyboard to apply the Normal style, or a person who likes playing with their mouse could press the "Normal" button on the user form ;-)
Hope the annotated code in the latest uploaded template was of more use!
Cheers
Steve
Ok - I'll get to work on it - love the annotated code :) Although I'm really not working the code, just the user form itself :)
See if this works Steve... I really have no way to test it -- not having a real document of yours....
MacroDevelopment2.doc
MacroDevelopment2.doc
ASKER
Thanks I'll give it a go... can't wait to see how you've worked the user form (this is something that I totally overlooked).
Cheers
Steve
Cheers
Steve
ASKER
OK! Finally I see the main(?) change that you've made, in command button properties there is an "Accelerator" - I'm unlikely to forget that now ;-)
The code works so very nearly as I want it to, and here is the but... on my machine I have to hold down the ALT key while pressing the underlined letter on the user form.
If we can programmaticaly hold down the ALT key while the macro runs then all will be well, no perfect.
I note that the extra code that you put in the user form is commented out, I'm not sure how to interpret the code/comments - I've not been able to get the message box to appear.
Many thanks
Steve
The code works so very nearly as I want it to, and here is the but... on my machine I have to hold down the ALT key while pressing the underlined letter on the user form.
If we can programmaticaly hold down the ALT key while the macro runs then all will be well, no perfect.
I note that the extra code that you put in the user form is commented out, I'm not sure how to interpret the code/comments - I've not been able to get the message box to appear.
Many thanks
Steve
Private Sub Text1_KeyDown(keycode As Integer, Shift As Integer)
MsgBox keycode
' - Page Up 33
' - Page Down 34
' - Home 36
' - End 35
' - Spacebar 32
' - Left Arrow 37
' - Right Arrow 39
' - Down Arrow 40
' - Up Arrow 38
' - 0 48 or 96
' - 1 49 or 97
' - 2 50 or 98
' - 3 51 or 99
' - 4 52 or 100
' - n || N
' - Delete 46
' - Print Screen 45
' - Pause 19
End Sub
ASKER
Points of clarification:
By *finally* I meant - after looking at your submission for ages I finally saw what you did
By *well, no perfect* I meant - if that happens the macro will be perfect (well in my eyes anyway)
:-)
By *finally* I meant - after looking at your submission for ages I finally saw what you did
By *well, no perfect* I meant - if that happens the macro will be perfect (well in my eyes anyway)
:-)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Oh my! I must have bumped my head. Pls ignore my 2nd last (now 3rd last) ramble - I gave you that remmed code... must have been a leftover while searching for answers.
Regarding the programmatic holding down of the Alt key - could it be done with some sort of a loop? Perhaps a repetitious sending of the wdKey=Alt (or so), probably fraught with danger.
BTW - Thanks. Had a look at the Tool Bar you created, I did not know I could do that - its a great step forward for the toolbar.
Cheers
Steve
PS x-ray = 4 ribs
Regarding the programmatic holding down of the Alt key - could it be done with some sort of a loop? Perhaps a repetitious sending of the wdKey=Alt (or so), probably fraught with danger.
BTW - Thanks. Had a look at the Tool Bar you created, I did not know I could do that - its a great step forward for the toolbar.
Cheers
Steve
PS x-ray = 4 ribs
Ouch!!! Hope you are feeling better really soon!
To be perfectly honest, I just don't know how to get around the Alt key thing.... most Word users understand and instinctively press the Alt key when they see an accelerator.... it's the way Word is built and always has been so I don't think it's a stretch to have them press the Alt key in conjunction with the accelerator key. I mean - Word won't know whether you want to TYPE the "n" or if you're using it as a shortcut.... so I think you're stuck with the Alt key....
To be perfectly honest, I just don't know how to get around the Alt key thing.... most Word users understand and instinctively press the Alt key when they see an accelerator.... it's the way Word is built and always has been so I don't think it's a stretch to have them press the Alt key in conjunction with the accelerator key. I mean - Word won't know whether you want to TYPE the "n" or if you're using it as a shortcut.... so I think you're stuck with the Alt key....
ASKER
Thank you so much for your help, sorry I took a while to close (just waiting to see if you found a workaround for Alt). Really appreciate your patience with this 1. Have a great weekend... I'm back to wrk next week 6/8 hrs and 70% duties so might not be on EE as much. Take care.
Thanks Steve. Glad to have been of at least a little help...
Joanne
Joanne