PDA

View Full Version : Direct formatting lost when updating paragraph



Nand S.
06-25-2017, 05:33 AM
Hi, I'm new to VBA in Word (2016), and a little bit lost.

I'm trying to scan through a document and updating specific paragraphs by replacing a prefix when found. The code works as expected but has a nasty side effect.
When using the code below (which was reduced to a strict minimum to increase readability) I'm loosing all direct formatting that was present in the paragraph. Hence, bold, italic etc. are being removed. I presume this is because I'm processing the paragraph as .Text and using string variables.

Any idea how I can update a paragraph prefix (it's just a string value at the start of the paragraph) without loosing the direct formatting?



For iDx = 1 To iParCount

' [code to select the appropriate paragraph]

xParText = ActiveDocument.Paragraphs(iDx).Range.Text

' [code that will convert xParText (string) into xNewText (string) by only changing the prefix]

ActiveDocument.Paragraphs(iDx).Range.Text = xNewText

Next iDx

In other words, how can I preserve the part after the prefix and append it to the new one?

gmaxey
06-25-2017, 05:57 AM
Basically your problem is that string variables don't have formatting. Can you give and example of what you are actually trying to do?

As you are new to VBA now is a good time to think about your style and develop good habits. Declare all variables and give your variables meaningful names. You might not adapt my style but I use lng for longs e.g., lngIndex and o for objects e.g., oRng.

Maybe something like this:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim lngIndex As Long
Dim oRng As Range
For lngIndex = 1 To ActiveDocument.Paragraphs.Count
Set oRng = ActiveDocument.Paragraphs(lngIndex).Range
Set oRng = fcnChangePrefix(oRng, "Old Prefix", "New Prefix")
Next lngIndex
lbl_Exit:
Exit Sub
End Sub

Function fcnChangePrefix(oRng As Range, _
strOldPrefix As String, _
strNewPrefix As String) As Range
With oRng.Find
.Text = strOld
.Replacement.Text = strNew
.Execute Replace:=wdReplaceOne
End With
End Function

Nand S.
06-25-2017, 06:29 AM
Each Word document to be processed contains the transcription of the 1702 census of an area in Brussels (Belgium).
The document contains a number of paragraphs each describing either a house or a family.
The basic idea is that each house (not family) is to receive an individual number.
After being drafted, each document is reviewed twice. During this review a paragraph can be marked as being a "house paragraph" or not. Such a mark is simply a "§." string at the start of the paragraph. A paragraph can also be unmarked by removing the "§." or a previously generated "<number>." sequence.
After each review the relevant paragraphs have to be renumbered with an ascending sequence number.

An example (in 18th century Flemish):



115. Item Den Gulden Arent. Louijs Immens, cnopmaker, sijn huijsvr(ouw)e, drij sonen, een dochter onder, een winkeldochter, een mijsen.

Het huijs genoempt De Diluvie. Ledigh.

§. Inden Coninck van Spanien. Alexander Delvart, perucquier, sijn huijsvr(ouw)e, een dochter onder 14, eenen soon onder 14, een mijsen.


So, only the paragraphs starting with either "§." or "<number>." are being processed and need to receive a new sequence number.
Texts in italic or bold must remain that way.

[I'm doing this for free for a non-profit genealogical association]

Nand S.
06-25-2017, 06:36 AM
The QUOTE option has put all the text in italic. That was not the intention.

gmaxey
06-25-2017, 06:59 AM
If as in your example, both the numbers and the section symbol are followed with a period then:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim lngIndex As Long
Dim oRng As Range
For lngIndex = 1 To ActiveDocument.Paragraphs.Count
Set oRng = ActiveDocument.Paragraphs(lngIndex).Range
Select Case True
Case oRng.Characters(1) = "§" Or IsNumeric(oRng.Characters(1))
oRng.Collapse wdCollapseStart
oRng.MoveEndUntil Chr(46)
oRng.Fields.Add oRng, wdFieldSequence, "Index"
End Select
Next lngIndex
lbl_Exit:
Exit Sub
End Sub

Nand S.
06-25-2017, 07:08 AM
Thank you.
Your examples put me on the right track.