PDA

View Full Version : Quick Tagging in Word



entwined
02-10-2012, 11:53 PM
Hi VB'ers,

I need a macro that will quickly apply tags (kind of html tags but its a custom tag) in a word file. So it's like when I execute the macro assigned in a hotkey, a certain tag will be automatically typed. Example scenario is:

1. When I press Alt+U right at the beginning of any data (e.g. 004.P1.001), the macro will type a tag (e.g. <UniID>). The output of that will be -- <UniID>004.P1.001

2. That's just the start tag right, so there should be an end tag </UniID> and that will be applied at the end of the data by pressing ENTER key only. So I will press ENTER at the end of it and the output will be now -- <UniID>004.P1.001</UniID>

Would that be possible? I pray so. Please help :( . Any input will be very much appreciated. Thanks...

fumei
02-11-2012, 01:17 AM
As described, no this would be very difficult. Why? Because how is the macro to know where to put the closing tag? You are not identifing the piece of text.

004.P1.001

004.P1.001 yadda blah blah

"When I press Alt+U right at the beginning of any data (e.g. 004.P1.001"

Except, unless that data is in some field (or other container)...there is no DATA. It is just text.

That being said, there is a simple solution. Select the text (004.P1.001) and execute:
Sub AddTags()
Selection.Text = "<UnitID>" & Selection.Text & "<UnitID>"
End Sub
This adds <UnitID> before the selected text, and adds </UnitD> after the selected text.

Paul_Hossler
02-11-2012, 05:35 AM
Small typo -- dropped the / in the closing tag



Sub AddTags()
Selection.Text = "<UnitID>" & Selection.Text & "</UnitID>"
End Sub



To assign Alt-U to the macro, something like this, unless you want to set the AltU macro manually


Sub AssignKeyAltU()
CustomizationContext = NormalTemplate
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyU, wdKeyAlt), KeyCategory:=wdKeyCategoryMacro, Command:="AddTags"
End Sub



Paul

fumei
02-11-2012, 01:11 PM
Odd. I twice edited my post to fix the missing slash. It SEEMED to be fixed.

The coded customization procedure may work for you...or not. I mean it will work of course, it just may not be the correct/best route. You may not want it in your normal template for example.

deedii
02-14-2012, 08:56 AM
Ok thanks for that advice. Maybe i will just use the documents rather than integrating it in the form. It would be easy I guess.