Solved

Macro to capture keystrokes to use in Select Case or similar --> to apply styles to paragraphs

Posted on 2008-10-22
39
405 Views
Last Modified: 2012-08-13
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
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

Open in new window

0
Comment
Question by:DrTribos
  • 19
  • 17
39 Comments
 
LVL 9

Expert Comment

by:lwebber
ID: 22783246
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.
0
 
LVL 14

Author Comment

by:DrTribos
ID: 22783303
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
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 22941225
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...
0
 
LVL 14

Author Comment

by:DrTribos
ID: 22945105
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
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 22949708
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("Task")
ElseIf user Presses R Then
         Selection.Style = ActiveDocument.Styles("Responsibility")
Elseif user Presses F Then
         Selection.Style = ActiveDocument.Styles("Function")
End if

Will that work for the one portion of your question?





0
 
LVL 14

Author Comment

by:DrTribos
ID: 22954017
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("Task")
ElseIf user Presses ...

Cheers

Steve
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 22961612
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.....



0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 22962370
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
0
 
LVL 14

Author Comment

by:DrTribos
ID: 22963338
I'll give it a crack -
open your doc
Alt F11
R-click on module ... import...
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 22963399
Close! LOL

Right click - export, save, then import into your template.....
0
 
LVL 14

Author Comment

by:DrTribos
ID: 22963473
Um - tail between legs... I have uploaded picture, I don't know where the user form is :-(
DrTribos-UserForm1.jpg
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 22963523
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
0
 
LVL 14

Author Comment

by:DrTribos
ID: 22963690
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.
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 22963718
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
0
 
LVL 14

Author Comment

by:DrTribos
ID: 22963755
LOL - Ok, think I'll find it this time :-)
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 22963778
;)

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....

0
 
LVL 14

Author Comment

by:DrTribos
ID: 22963887
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
0
 
LVL 14

Author Comment

by:DrTribos
ID: 22963927
BTW - to get the form to show I restarted my windoz session....
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 14

Author Comment

by:DrTribos
ID: 22965098
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
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 22978416
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?
0
 
LVL 14

Author Comment

by:DrTribos
ID: 22982707
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
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 22984611
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?

0
 
LVL 14

Author Comment

by:DrTribos
ID: 22989527
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
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 22994123
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.

:)
0
 
LVL 14

Author Comment

by:DrTribos
ID: 22999500
I have uploaded the template
MacroDevelopment2.doc
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 23005753
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
0
 
LVL 14

Author Comment

by:DrTribos
ID: 23009558
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
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 23012788
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 :)


0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 23013449
See if this works Steve... I really have no way to test it -- not having a real document of yours....

MacroDevelopment2.doc
0
 
LVL 14

Author Comment

by:DrTribos
ID: 23017592
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
0
 
LVL 14

Author Comment

by:DrTribos
ID: 23017902
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


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

Open in new window

0
 
LVL 14

Author Comment

by:DrTribos
ID: 23017913
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)

:-)
0
 
LVL 37

Accepted Solution

by:
Joanne M. Orzech earned 500 total points
ID: 23028275
Well, I'm not sure I can do that programatically.... but I'll try to research... in the meantime, there appears to be a few styles that do not exist... Task End for one.... could you take a look at the attached?  And you can remove the REM apostrophes from your code whereever you want.....

:)

MacroDevelopment2.doc
0
 
LVL 14

Author Comment

by:DrTribos
ID: 23031488
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
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 23034813
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....


0
 
LVL 14

Author Closing Comment

by:DrTribos
ID: 31509010
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.
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 23080416
Thanks Steve.  Glad to have been of at least a little help...

Joanne
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction Authors who set out to write any sort of lengthy piece for online submission—be it a long question or comment on a technical form, an article, or a substantial blog entry—often find it useful to work up a draft in an editor other t…
I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now