Adding equations vie Macro using the formula editor - function stops working at equation no. 99

I created a document template including a customized ribbon for adding predefined elements like e.g. equations in a predefined font and layout.

In addition to the equations, a consecutive, equation reference number is added on the right hand side - everything is packed into a macro.

The related code for adding the consecutive number is the following:

.Fields.Add Range:=Selection.Range, _
                    Type:=wdFieldEmpty, _
                    Text:="SEQ PDEq \* MERGEFORMAT", _
                    PreserveFormatting:=False

The problem that I currently have is, that for any reason this section of the code for adding the consecutive number)doesn't work for equation numbers higher than 99.

If I copy and paste the entire line, it works also for numbers higher that 99.

Unfortunately I don't have an idea on where the problem can come from.

The following code creates 200 SEQ fields

Dim i As Long
Dim rng As Range
With ActiveDocument
    For i = 1 To 200
        Set rng = .Range
        rng.Collapse wdCollapseEnd
        .Fields.Add rng, wdFieldEmpty, "SEQ PDEq"
        rng.InsertAfter vbCr
    Next i
    .Range.Fields.Update
End With

Can you show us all of the code that you are using?

Hope this helps,
Doug Robbins - MVP Office Apps & Services (Word)
dougrobbinsmvp@gmail.com
It's time to replace ‘Diversity, Equity & Inclusion’ with ‘Excellence, Opportunity & Civility’ - V Ramaswamy

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Thanks for the quick reply!

To state at this point, I'm not a very advanced user in writing Macros :-)

The code you are proposing is generating 200 SEQ fields, that's right, but the code I posted before basically works - for equation numbers 1-99, it will not work for 100 and higher. The idea of the macro is not to generate 200 equations in one go, one more equation should be added each time when the macro is executed.

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Can you show us the complete macro and can you also send me a document in which you have already inserted 99 equations so that I do not have to run the macro 100 times.
Hope this helps,
Doug Robbins - MVP Office Apps & Services (Word)
dougrobbinsmvp@gmail.com
It's time to replace ‘Diversity, Equity & Inclusion’ with ‘Excellence, Opportunity & Civility’ - V Ramaswamy

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

The complete code is as follows:

----------

Sub PDInsertEquation()
 
    With Selection
        
        .Style = ActiveDocument.Styles("Normal")
        
        .TypeText Text:=vbTab
        
        .InlineShapes.AddOLEObject ClassType:="Equation.3", _
                                   LinkToFile:=False, _
                                   DisplayAsIcon:=False
                                   
        .TypeText Text:=vbTab & "("
        
        .Fields.Add Range:=Selection.Range, _
                    Type:=wdFieldEmpty, _
                    Text:="SEQ PDEq \* MERGEFORMAT", _
                    PreserveFormatting:=False
                    
        .MoveLeft Unit:=wdCharacter, count:=1, Extend:=wdExtend

        .Fields.Add Range:=Selection.Range, _
                    Type:=wdFieldEmpty, _
                    PreserveFormatting:=False
                    
        .TypeText Text:="MACROBUTTON PDInsertEqRef "
        
         
         .EndKey Unit:=wdLine
         
        .MoveLeft Unit:=wdCharacter, count:=2
        
        .Delete Unit:=wdCharacter, count:=1
          
        .Fields.Update
    
        .MoveRight Unit:=wdCharacter, count:=1
        
        .TypeText Text:=")"
    
        .TypeParagraph
    End With
End Sub
----------

Thanks for your answer!

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

I just realized that the Macro works when the Style "Normal" is assigned in the source code

.Style = ActiveDocument.Styles("Normal")

If I use a customized style (e.g. "Equation") it does not work, I guess this is a bug?

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Your code does this:

As I did not want to do that 100 times, I ran a macro containing the following code after first creating an equation and copying it to the clipboard

Dim i As Long
Dim rng As Range
With ActiveDocument
     For i = 1 To 200
         Set rng = .Range
         rng.Collapse wdCollapseEnd
         rng.Paste
         Set rng = .Range
         rng.Collapse wdCollapseEnd
         rng.InsertAfter vbTab & "("
         Set rng = .Range
         rng.Collapse wdCollapseEnd
         .Fields.Add rng, wdFieldEmpty, "SEQ PDEq"
         Set rng = .Range
         rng.Collapse wdCollapseEnd
         rng.InsertAfter ")" & vbCr
     Next i
     .Range.Fields.Update
End With

This is the last page of the document after the macro has finished

Hope this helps,
Doug Robbins - MVP Office Apps & Services (Word)
dougrobbinsmvp@gmail.com
It's time to replace ‘Diversity, Equity & Inclusion’ with ‘Excellence, Opportunity & Civility’ - V Ramaswamy

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Yes, that's the somehow strange behaviour, because if the entire equation is duplicated (by using copy/paste) it works - even if it is done via your Macro. But if there are more that 99 equations available, and someone is running the macro once, the number is not added.

And as further mentioned, it works if the style "Normal" is used instead of customized one.

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Doug, do you have any further recommendations for this issue? Unfortunately I could not solve the issue so far. Thanks !

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

I have no VBA expertise, and this doesn't address the problem of failure to create three-digit SEQ fields, but it appears to me that the line PreserveFormatting:=False just undoes the work of the \* MERGEFORMAT switch. If you omit that switch, I would think you would not need that line of code.
Microsoft MVP (Word) since 1999
Fairhope, Alabama USA
http://ssbarnhill.com
http://wordfaqs.ssbarnhill.com
Screen shots captured with TechSmith's Snagit

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 200 Applies to: