PDA

View Full Version : Error when inserting a field



Roderick
12-03-2018, 01:28 PM
This is a macro developed earlier by Graham Mayor for which I'm very grateful and which works superbly well up to a point identified below - but only because I've "messed around" with it to try and achieve a solution.

Here is the macro:

Sub ReplaceFieldsV4()
'Graham Mayor - http://www.gmayor.com - Last updated - 24 Sep 2018
'===================================
'this procedure ensures that all the Standard Numbering SEQ fields have the \s option set
'irrespective of what had been put there earlier.
Dim oFld As Field, oNewFld As Field
Dim oRng As Range, oSeq As Range
Dim figRng As Range, figLvl As Long

ActiveWindow.View.ShowFieldCodes = True
For Each oFld In ActiveDocument.Fields
If oFld.Type = wdFieldSequence Then
If InStr(1, oFld.Code, "Figure") > 0 Then
oFld.Select
'this procedure inserts a Chapter Figure caption based on Word Headings levels
With Selection
Set figRng = .Range
Set figRng = figRng.GoTo(What:=wdGoToBookmark, Name:="\HeadingLevel")
figLvl = Right(figRng.Paragraphs.First.Style, 1)
End With
oFld.Code.Text = Replace(oFld.Code.Text, oFld.Code.Text, "SEQ Figure \* ARABIC \s \* MERGEFORMAT")
oFld.Update
Set oRng = oFld.Code
With Selection
.MoveLeft Unit:=wdCharacter, Count:=1
.TypeText Text:="-"
.Collapse
.MoveLeft Unit:=wdCharacter, Count:=1
End With
'now adds the StyleRef field
Set oNewFld = ActiveDocument.Fields.Add(Range:=.Range, _
Type:=wdFieldEmpty, _
Text:="StyleRef ""Heading " & figLvl & """ \s", _
PreserveFormatting:=False)
oNewFld.Update
Set oRng = oNewFld
End If
End If
Next oFld
ActiveWindow.View.ShowFieldCodes = False
lbl_Exit:
Set oFld = Nothing
Set oNewFld = Nothing
Set oRng = Nothing
Exit Sub
End Sub

What should happen is that a StyRef field should be inserted before the SEQ field, however, and error kicks in at this position here (in red):


Set oNewFld = ActiveDocument.Fields.Add(Range:=.Range, _
Type:=wdFieldEmpty, _
Text:="StyleRef ""Heading " & figLvl & """ \s", _
PreserveFormatting:=False)
oNewFld.Update
Set oRng = oNewFld
...and this is the error message which appears:

23324

I've tried "fiddling" with possible permutations to get the field added but don't seem to have the correct procedure to get it right.

Could the correct code be pointed out me, please, to achieve the process?

Thanks

Roderick

gmaxey
12-03-2018, 06:12 PM
Sub ReplaceFieldsV4()
Dim oFld As Field, oNewFld As Field
Dim oRng As Range, oSeq As Range
Dim figRng As Range, figLvl As Long
ActiveWindow.View.ShowFieldCodes = True
For Each oFld In ActiveDocument.Fields
If oFld.Type = wdFieldSequence Then
If InStr(1, oFld.Code, "Figure") > 0 Then
oFld.Select
With Selection
Set figRng = .Range
Set figRng = figRng.GoTo(What:=wdGoToBookmark, Name:="\HeadingLevel")
figLvl = Right(figRng.Paragraphs.First.Style, 1)
End With
oFld.Code.Text = Replace(oFld.Code.Text, oFld.Code.Text, "SEQ Figure \* ARABIC \s \* MERGEFORMAT")
oFld.Update
Set oRng = oFld.Code
With Selection
.MoveLeft Unit:=wdCharacter, Count:=1
.TypeText Text:="-"
.Collapse
.MoveLeft Unit:=wdCharacter, Count:=1
'End With 'Move ...
Set oNewFld = ActiveDocument.Fields.Add(Range:=.Range, _
Type:=wdFieldEmpty, _
Text:="StyleRef ""Heading " & figLvl & """ \s", _
PreserveFormatting:=False)
oNewFld.Update
Set oRng = oNewFld.Code 'Added .Code but not sure what you need it for.
End With '... to here
End If
End If
Next oFld
ActiveWindow.View.ShowFieldCodes = False
lbl_Exit:
Set oFld = Nothing
Set oNewFld = Nothing
Set oRng = Nothing
Exit Sub
End Sub

gmayor
12-03-2018, 11:58 PM
You raised this in the original thread http://www.vbaexpress.com/forum/showthread.php?63700-Find-and-replace-fields-in-Word-using-VBA&p=384250&highlight=#post384250 where I explained about moving the range to position the elements wherever you wish, and included a revised version of the code. The code you posted here bears little relationship to that code and as Greg has identified the .Range has become detached from what it is a range of - hence the error.

Roderick
12-04-2018, 11:38 AM
Thank you Graham for the reminder. Yes, you did post the code and it works perfectly as you showed. If you forgive me, I will post it again as it will avoid having to look back at it.

Here was your code:

Sub ReplaceFieldsV6()
'Graham Mayor - http://www.gmayor.com - Last updated - 25 Sep 2018
Dim oFld As Field, oNewFld As Field
Dim oRng As Range, oSeq As Range
For Each oFld In ActiveDocument.Fields
If oFld.Type = wdFieldSequence Then
If InStr(1, oFld.Code, "Figure") > 0 Then
oFld.Code.Text = Replace(oFld.Code.Text, oFld.Code.Text, "SEQ Figure \* ARABIC \s \* MERGEFORMAT")
oFld.Update
Set oRng = oFld.Code
oRng.MoveStart wdCharacter, -1
oRng.Collapse 1
oRng.Text = "-"
oRng.Collapse 1
Set oNewFld = ActiveDocument.Fields.Add(Range:=oRng, _
Type:=wdFieldStyleRef, _
Text:="""GA Numbered Heading 1""" & " \s", _
PreserveFormatting:=False)
oNewFld.Update
End If
End If
Next oFld
ActiveWindow.View.ShowFieldCodes = False
lbl_Exit:
Set oFld = Nothing
Set oNewFld = Nothing
Set oRng = Nothing
Exit Sub
End Sub

The problem arises that the following line of code now has the wrong style and needs to be changed to reflect the main heading style it is sitting under, e.g. Heading 1 or Heading 2 and so on.

Text:="""GA Numbered Heading 1""" & " \s",

This change is being forced upon me as I'm quite happy to keep the existing one.

This code identifies the necessary heading:


With Selection
Set figRng = .Range
Set figRng = figRng.GoTo(What:=wdGoToBookmark, Name:="\HeadingLevel")
figLvl = Right(figRng.Paragraphs.First.Style, 1)
End With

Now the new line of code has to look like this:

Text:="""Heading " & figLvl & """ \s"

THIS is where the problem arises and I'm trying to figure out a way to accomplish this change.

Again, I reiterate my "thank you" to you, Graham, for your help. I would not have changed the code if it had not been foisted on me to do so.

Roderick