Well, in case anyone is interested in this feature, I managed to implement
something that does the job. The problem with pressing Shft+F9 or Alt+F9 to
edit the fields is that is screws-up the page layout. Anyhow, I solved my
problem by impementing the following in my "Normal.dot" template VBA code:
1) Insert > Class Module
2) Rename "Class1" to "ThisApplication"
3) Paste the following code:
'--- Start of ThisApplication Class Module ---
Option Explicit
Dim objEditBox As CommandBarControl
Public WithEvents objWordApp As Application
Private Sub objWordApp_WindowSelectionChange(ByVal Sel As Selection)
Set objEditBox = CommandBars("FieldEditBar").Controls(1)
If (Sel.Fields.Count > 0) Then
objEditBox.Text = Sel.Fields.Item(1).Code.Text
Else
objEditBox.Text = ""
End If
End Sub
'--- End of ThisApplication Class Module ---
4) Insert > Module
5) Rename "Module1" to "FieldEditBar"
6) Paste the following code in the module:
'--- Start of FieldEditBar module ---
Option Explicit
' Instatiate the Container class object
Dim objAppClass As New ThisApplication
Dim objEditBox As CommandBarControl
Public Sub AutoExec()
' By defining procedure AutoExec, the code will run
' automatically when the Addin loads
' (automatically load when Word starts)
Set objAppClass.objWordApp = Word.Application
Dim objCommandBar As CommandBar
Dim objCommandBarControl As CommandBarControl
Dim ToolbarExists As Integer
ToolbarExists = 0
For Each objCommandBar In Application.CommandBars
If objCommandBar.Name = "FieldEditBar" Then
ToolbarExists = 1
End If
Next objCommandBar
If ToolbarExists = 0 Then
Set objCommandBar = Application.CommandBars.Add("FieldEditBar")
objCommandBar.Name = "FieldEditBar"
objCommandBar.Visible = True
Set objCommandBarControl = Application.CommandBars _
("FieldEditBar").Controls.Add(Type:=msoControlEdit, id:=1)
Set objEditBox = CommandBars("FieldEditBar").Controls(1)
objEditBox.OnAction = "InsertField"
End If
End Sub
Function InsertField()
Set objEditBox = CommandBars("FieldEditBar").Controls(1)
If (Selection.Fields.Count > 0) Then
Selection.Fields.Item(1).Code.Text = objEditBox.Text
Selection.Fields.Item(1).Update
Else
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty,
Text:= _
objEditBox.Text, PreserveFormatting:=True
Selection.Collapse (wdCollapseEnd)
End If
End Function
'--- End of FieldEditBar module ---
This Addin creates an Edit box control on a toolbar that can be docked along
with the other toolbars. When a selection in the document is highlighted,
the field code is automatically displayed in the box, if the selection
contains a field. The field code can be modified immediately from the edit
box and is automatically updated upon pressing the Enter key in the edit box.
Typing anything in the edit box creates a new field from the code entered in
the edit box.
If anyone has a better idea to improve this fonctionality, comments are
welcome. But so far, this solves my problem.
alainr said:
My problem is that I find myself performing following (too) many times MS Word:
"Right-click on a field in a document" > Click [Edit Field...] > Click
[Field Code] > Set focus in Field Code edit textbox. ~ 4 mouse clicks, many
motions
I would like word to behave as follows:
When I set the cursor on a field in an MS Word document, I would like to
have a Toolbar component textbox that shows me the contents of the field so
that I can click immediately in the textbox and edit the field on the fly
(like when you click in a Cell in Excel and edit the contents using the
formula bar). Can anyone assess if this is possible and give me a few
pointers where to start to go about implementing this function in Either VBA,
VB or VC++?