Checking for field code in Word 2000

J

jbc

Good evening,

How would I have a macro check for a SEQ field code at the beginning of the
current paragraph?

Thanks.

jbc
 
H

Helmut Weber

Hi,

like this, e.g.:

Sub Makro1()
With Selection.Paragraphs(1).Range
With .Characters(1)
If .Fields.Count Then
If Left(.Fields(1).Code, 4) = " SEQ" Then
MsgBox "yes"
End If
End If
End With
End With
End Sub

Note: "[space]SEQ"

Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
H

Helmut Weber

hmm...

I am not good at fields.
You' better use the field.type

If .Fields.Count Then
If .Fields(1).Type = wdFieldSequence Then
MsgBox "yes"
End If
End If

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

jbc

Hi,

I thought I could figure this out on my own with some assistance by the
experts, but.....

I actually want 2 fields separated by a tab that is followed by bibliography
text.

{ SEQ Paragraph \# "[0000]" \n \* MERGEFORMAT }TAB { SEQ Biblio \# "0." \*
MERGEFORMAT } Followed by text

I want the macro to check to see what fields are there and based on what it
finds, put the appropriate field codes into the paragraph.

Thanks.

jbc
 
H

Helmut Weber

Hi jbc,

how about this one?

Checks whether a paragraph starts with a field.
if so, checks whether that field is of type wdFieldSequence.
if so, gets the length of that field's result.
Checks whether the character after the first field,
is in a field also.
If so, checks whether that field is of type wdFieldSequence, too.
If so, inserts a tab after the first field in the paragraph.

Sub Makro1()
Dim rTmp As Range ' a temporary range
Dim lFld As Long ' length of a field result
Dim cChr(1 To 2) As Object

ActiveWindow.View.ShowFieldCodes = False
Set rTmp = Selection.Paragraphs(1).Range

Set cChr(1) = rTmp.Characters(1)
If cChr(1).Fields.Count = 1 Then
If cChr(1).Fields(1).Type = wdFieldSequence Then
lFld = Len(rTmp.Fields(1).Result)
End If
Set cChr(2) = rTmp.Characters(1 + lFld)
If cChr(2).Fields.Count = 1 Then
If cChr(2).Fields(1).Type = wdFieldSequence Then
rTmp.Characters(lFld).InsertAfter chr(9)
End If
End If
End If

End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
H

Helmut Weber

Hi jbc,

some things seem to be complicated,
but indeed are very simple.

Some things seem to be simple,
but indeed are very complicated.

There is a mistake in the code,
as it doesn't exit, if there is a field
other then a SEQ-field at the start of the paragraph.

Here comes an improved version:

Sub MakroX()
Dim rTmp As Range ' a temporary range
Dim lFld As Long ' length of a field result
Dim cChr(1 To 2) As Object

ActiveWindow.View.ShowFieldCodes = False
Set rTmp = Selection.Paragraphs(1).Range

Set cChr(1) = rTmp.Characters(1)
If cChr(1).Fields.Count = 1 Then
If cChr(1).Fields(1).Type = wdFieldSequence Then
lFld = Len(rTmp.Fields(1).Result)
Set cChr(2) = rTmp.Characters(1 + lFld)
If cChr(2).Fields.Count = 1 Then
If cChr(2).Fields(1).Type = wdFieldSequence Then
rTmp.Characters(lFld).InsertAfter chr(9)
End If
End If
End If
End If

End Sub


--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

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