Look for ASK field first before creating it.

P

PiaD

Hi -
I have a macro that inserts 2 ASK fields at the top of an active document as
follows:
I would like to first look for the ASK fields first, and only add them if
they do not exist yet. Can someone help with the IF statement needed?

Sub AddFieldCodes()
' Used to add ASK field codes to an existing document, not initially created
using this template.
' Add ASK Reference Field Codes at start of document.
Selection.GoTo What:=wdGoToPage, Which:=wdGoToFirst, Name:=""
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
" ASK Name ""Name"" \d ""<Name>"" ", _
PreserveFormatting:=False
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
" ASK Type ""Type"" \d ""<Type>"" ", _
PreserveFormatting:=False
End Sub
 
K

Klaus Linke

Hi Pia,

You likely need a bit more than something that fits in an "IF" :-/

Loop through all the fields in the first paragraph (or the whole doc...
shouldn't take that long and might be safer).
See if there's an ASK field with the code already:
(The sample code is just for one of the ASK fields...)

Dim rngSearch As Range
' If you just want to look in the first paragraph:
Set rngSearch = ActiveDocument.Paragraphs(1).Range
' or if you aren't sure and want to look in the whole content:
' Set rngSearch = ActiveDocument.Content

Dim myField As Field
Dim sCode As String
Dim boolAlreadyThere As Boolean

boolAlreadyThere = False
sCode = "ASK Type ""Type"" \d ""<Type>"""
For Each myField In rngSearch.Fields
If myField.Type = wdFieldAsk And Trim(myField.Code) = sCode Then
boolAlreadyThere = True
End If
Next myField

' If you haven't found it, insert the ASK field:
If Not boolAlreadyThere Then
Selection.Fields.Add _
Range:=Selection.Range, _
Type:=wdFieldEmpty, _
Text:=sCode, _
PreserveFormatting:=False
Debug.Print sCode
Else
' Just for debugging:
' Debug.Print sCode, "already there"
End If


Regards,
Klaus
 
K

Klaus Linke

Better replace
If myField.Type = wdFieldAsk And Trim(myField.Code) = sCode Then with
If myField.Type = wdFieldAsk And Trim(myField.Code) = Trim(sCode) Then

Leading and trailing spaces else might mess up the comparison.

If you aren't sure the switch and the placeholder text are always the same,
you'd need a bit more code.

Klaus
 
P

PiaD

Thank you very much. This worked like a charm. Now do you know how I can
control the formatting of the autotext? I can't figure out how or when to
use the switch */Mergeformat. Should it be in the ASK or the REF field?
Thanks!
 
K

Klaus Linke

PiaD said:
Thank you very much. This worked like a charm. Now do you know how I can
control the formatting of the autotext? I can't figure out how or when to
use the switch */Mergeformat. Should it be in the ASK or the REF field?
Thanks!

Guess you'd use \* MERGEFORMAT in the REF field.
I don't think it would have any effect in the ASK field.

I usually do the formatting with styles, so the formatting doesn't change
when fields are updated.

Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top