PDA

View Full Version : How Can I Press Key With VB Macro Programming in Word ?



Neon
04-13-2010, 12:02 PM
When I Change Font from "Times New Romans" to "SutonnyMJ" , then I need to Press Control+Alt+B from Keyboard to write with "SutonnyMJ" Fonts . Again when I change font from "SutonnyMJ" to "Times New Romans" , then I need to press again Control+Alt+B to write english .

Every time Pressing Control+Alt+B from keyboard is very troublesome . How can I press these three keys together by macro programming in MS-Word 2003 . Currently, I am using macro programming to change the fonts . I am giving that codes below :
Sub English()
'
' English Macro
' Macro recorded 4/13/2010 by xxxxxxxxxxx
'
Selection.Font.Name = "Times New Roman"
Selection.Font.Size = 16
End Sub
Sub Bangla()
'
' Bangla Macro
' Macro recorded 4/13/2010 by xxxxxxxxxxxxx
'
Selection.Font.Name = "SutonnyMJ"
Selection.Font.Size = 16
End Sub
I have assigned F8 for English and F9 for Bangla . Now how can I press above three Keys in this macro in both Sub ?

Please help me . I am waiting for yours reply .............

- Thanks .

joms
04-14-2010, 12:29 AM
try this:

(Not tested)



SendKeys "^%b", True

TonyJollans
04-14-2010, 02:37 AM
Does Ctrl+Alt+B switch the keyboard?

If so, I suspect you may have a problem because I don't know that SendKeys can send to the right place - you could try it, I suppose.

fumei
04-14-2010, 10:38 AM
And it would be far far better to use Syles, rather than manually changing format.

Neon
04-14-2010, 11:33 PM
Firstly a lot of thanks to you all for your replies . I just copied the sendkeys line from here and pasted in macro . But it is not working .

I need to press Control+Alt+B to change the font unicode of Bijoy . Bijoy is an application which icon runs in system tray . By default Bijoy's icon is like Diamond and in this state word is eligable for writting english . When I press control+Alt+B it's icon change into a Tri-angle and in this state word is eligable for writting Bengali . And again when I press control+Alt+B it's icon change from Tri-angle to Diamond again and in this state word is eligable for writting English again . It is one kind of toggle mode . you can know more about Bijoy by googling it .

Fumei is saying to use Syles . But I do not understand Syles and do not know how to use it . I am totally novice in vba macro programming . Can you give me a little describe how to use it ?

Sorry for my poor English .

-And thanks a lot again to you all .

TonyJollans
04-15-2010, 02:15 AM
Sorry, but I don't think you're going to be able to do this - certainly not easily.

I don't know how the Bijoy keyboard is integrated into the system to be able to say whether Windows keyboard switching APIs (ActivateKeyboardLayout, etc.) might help, but, unless Bijoy, itself, has some facilities in this area, I'm not sure you'll find another way.

Neon
04-17-2010, 10:08 AM
Humm.....mm, though job . But Fumei said to use Syles . Still I do not know , what is this ? Can I ask what is this ?

fumei
04-19-2010, 12:37 PM
If:

.Font.Name = "Times New Roman"
.Font.Size = 16

is DEFINED - as a Style - named "English"

AND

.Font.Name = "SutonnyMJ"
.Font.Size = 16

is DEFINED - as a Style - named "Bangla"

Then you can switch between the Styles. They can be assigned keyboard shortcuts.

For example, I just made a Style - named "English" with the following attributes:

Times Roman
Bold
Italics
22 pts
Left Indent 1.2 cm
Right Indent 2.0 cm
Space Before: 10 pts
Space After: 6 pts


I then made a style name "Bangla"

WingDings
13 pts
Left Indent 2.0 cm
Right Indent 0.5 cm
Space Before: 7 pts
Space After: 10 pts

OK? Now I assigned (through the customize menu) the shortcut Alt-E to "English", and Alt-B to "Bangla"

Voila. Alt-E makes anything selected the "English" style (with all the formats I gave it) OR...toggles the style to start using "English"

The applies to "Bangla". Alt-B changes the selected text to "Bangla", or toggles the style to start using "Bangla"

The point being is that the format of the styles can be anything.

fumei
04-19-2010, 12:44 PM
Ok, since you are in fact asking about programming this (ratherthan using akeyboard shortcut) then the basic application of Styles applies.
ActiveDocument.Paragraphs(4).Range.Style = "Bangla"
The fourth paragraph has the "Bangla" style applied to it.
ActiveDocument.Table(4).Cell(3,2).Range.Style = "Bangla"

The Cell at Row_2, Column_2 of the fourth table in the document has the Style "Bangla" applied to it. NO OTHER CELL IS AFFECTED.

ActiveDocument.Sections(3).Headers(wdHeaderFooterFirstPage).Range.Style = "Bangla"
The first page header of Section 3 has the Style "Bangla" applied to it. NO OTHER HEADER IS AFFECTED.
ActiveDocument.Sections(3).Headers(wdHeaderFooterFirstPage) _
.Range.Paragraphs(2).Range.Style = "Bangla"
The second paragraph of the first page header of Section 3 has the Style "Bangla" applied to. NO OTHER PARAGRAPH IN THE HEADER IS AFFECTED.

Neon
04-22-2010, 09:08 AM
My head is spinning :stars:

fumei
04-22-2010, 12:56 PM
It is quite simple actually.

A Style is a defined set of formatting.

That is it.

Font
Font Size
Space Before
Space After
Line Spacing
Left Indent
Right Indent
Keep With Next
Highlight
and on and on and.

These are ALL format attributes, and ALL of them can be defined within a single style.

Once you do that, you can apply the style to a paragraph and...voila!...all the defined formats are applied.

Well designed documents NEVER use manual formatting.

In my examples I was trying to show different examples of the same thing.

You want to make the current paragraph a specific format (font, font size, spacing...whatever), then you can apply a style. It makes all the changes you want. Generally speaking, it is NOT good practice to change things manually like:Selection.Font.whatever

ANY paragraph(s), be it in a header, somwehere in the document, or a table, or even one cell of a table - ANY paragraph - can have a style applied.

In fact...they all DO have styles attached. ALL text in a Word document has a style attached. You can not have text without a style...so why not make the style exactly what you want, rather than manually formatting?