PDA

View Full Version : add controls to ActiveDocument.Bookmarks



Bogdan0x400
06-10-2011, 09:45 AM
I have a .doc MS Word document, and it has a "time" field. How can I add the time field to the ActiveDocument.Bookmarks collection?

gmaxey
06-10-2011, 10:18 PM
Sub ScratchMacro()
'A quick macro scratch pad created by Greg Maxey
Dim oFld As Word.Field
Dim oRng As Word.Range
Set oFld = Selection.Fields(1)
Set oRng = ActiveDocument.Range(Start:=oFld.Code.Start, End:=oFld.Result.End)
ActiveDocument.Bookmarks.Add "Whatever", oRng
End Sub

Bogdan0x400
06-11-2011, 03:02 AM
Thank you for your example, but the field already exists on the document, I shouldn't create a new instance of field programmatically. And I also assume that there is a possibility of configuring this field to be added to bookmarks automatically, because I have another document, it contains this field in the Bookmarks, but I don't see it being added to the Bookmarks collection in the code.

Frosty
06-11-2011, 08:17 AM
Some kinds of fields, when first inserted into the document, will also create a generic bookmark at the same time. Those bookmarks can easily be lost with copy/paste of the fields (since bookmarks have to have unique names in a document)

What do you actually want accomplish?

Dim oField as Field
For Each oField in ActiveDocument.Fields
MsgBox oField.Name
Next

Will iterate through the fields. From there you could create bookmarks... But why do you want to?

Bogdan0x400
06-14-2011, 06:44 AM
I figured out that I don't need bookmarks :) . I just need to call the InsertDateTime method.
But how do I find my Time field in the ActiveDocument.Fields collection if I can't set any sort of ID to my field? I don't see this possibility anywhere in the properties of the field.

Frosty
06-14-2011, 03:45 PM
You can use the .Code property in conjunction with the .Type property (as well as some occasionally string manipulation) to extract out what you need.

For example, a Date field will show .Type = wdFieldDate
And the .Code property will be something like " DATE \@ "dddd, MMMM dd, yyyy" \* MERGEFORMAT"

You can use InStr to test to see if "DATE" exists...

Without more info about what you really want to do (you keep asking questions on *how* to do something, but you haven't really said *what* you want you to do), it's tough to point you in the right direction.

Bogdan0x400
06-15-2011, 05:53 AM
Thank you everyone for your answers, you've really helped me to solve this issue, my problem is solved now!