Consulting

Results 1 to 6 of 6

Thread: Direct formatting lost when updating paragraph

  1. #1
    VBAX Newbie
    Joined
    Jun 2017
    Posts
    4
    Location

    Direct formatting lost when updating paragraph

    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?

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,337
    Location
    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
    Last edited by gmaxey; 06-25-2017 at 06:13 AM.
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Newbie
    Joined
    Jun 2017
    Posts
    4
    Location
    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]

  4. #4
    VBAX Newbie
    Joined
    Jun 2017
    Posts
    4
    Location
    The QUOTE option has put all the text in italic. That was not the intention.

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,337
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  6. #6
    VBAX Newbie
    Joined
    Jun 2017
    Posts
    4
    Location
    Thank you.
    Your examples put me on the right track.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •