Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search on date field only 1

Status
Not open for further replies.

cramd

Programmer
Mar 28, 2001
214
US
I am trying to search and replace for a "date" field only in MSWORD. I can search for all fields with the ^d, but with this macro that I'm working on, I only need to work with the "date" field. I've tried searching for {date \@ "MM'/'dd'/'yy"} but that does not find the date field for me, if I search by ^d than I have it---of course, that also finds all fields, which I do not want.
cramd
 
Depending on what you are plannin on using this "search" for... this is what I've used to access only "ASK" fields. I'm pretty sure it can be modified to "date" fields....

why do you want to "search and replace"??? this code "updates" the field... you can do whatever you want to after the "then" statement...

Code:
  For Each aField In ActiveDocument.Fields
    If InStr(1, aField.Code, "date", 1) Then
        aField.Locked = False: aField.Update: aField.Locked = True
    End If
    Next aField
[\code]

The InStr seaches for a string called "date" in your field codes. 
I chose to lock my fields so the user can't accidentally edit them. You can take those containers away.
If you are lonly looking for one instance, you can easily modify the code. The code I gave you searches all of the fields in the active document. you can set your own range and apply it to whatever you want.

cheers,

swingkyd
 
swingkyd:
I need to delete all "date" fields. The documents I'm working with have multiple fields and I need to know that I have a "date" field when deleting. How do you define afield? As string? If I could get your code working, I think all I need to do is "afield.delete". I am having trouble defining the variable? If I define afield as string, how do I move each field (field1, field2, and field3, into afield??

dim afield as string
afield = activedocument.fields (??)
For Each afield In ActiveDocument.Fields

If InStr(1, afield.Code, "date", 1) Then
afield.Delete:
End If
Next afield

Thanks for your interest in this post!
cramd

 
Swingkyd:
I have it working. I needed to define afield as "field" not "string" and all is working. Thanks for your help!!
cramd
 
no problem...thanks for the star :)

I was wondering though... you don't really need to define the aField since it will look at all the fields in the current document anyway... at least it did for me when I was updating "ask" fields. I think all the fields are sorted in an array anyway since you are defining the range as:
ActiveDocument.Fields
My code will just search all the fields in the ActiveDocument.

Make any sense? it just might clean up your code some more.

As far as I can tell, you can just delete your Dim and aField assignment and it should work anyway.

I'm curious as to what you added to make it work. Please post it if you can :)

swingkyd
 
Swingkyd:
Without defining "afield" my code would not run. I would error at this statement "For Each aField In ActiveDocument.Fields". Once I defined "aField as Field" my program ran just as you described. It picked up on all fields, but only deleted the date field as I needed.
Thanks for the tip, I needed to get a project done this weekend and this allowed me to complete it!!

cramd
 
That's interesting because according to the MSDN library, you don't need to declare variables as the 'compiler' will actively declare variables. They recommend it though as "solid" coding practice.

That being said vba is not strongly typed anyway.

Are you using the "Option Explicit" statement? That would make sense because the VBA editor will force you to declare all variables if this is set.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top