none
Problem trying to wrap a Microsoft Word paragraph in XML tags?!? :/ RRS feed

  • Question

  • Hello everyone. I really hope this is not a big problem and that some of your could help. I wrote a Macro in VBA like a year ago that was intented to put a tag before every single paragraph in a document. The logic validates that the paragraph have no other tag before putting one. All of this is based in styles. Some font styles would end up with different tags. This is my code and it works:

    Sub edictos()
    '
    '
    ' Edictos de El Nuevo Día
    ' 06/20/2005 by Carlos Stella Sistemas de Información
    ' Actualización 08/08/2012
    '------------------------------------------------------
    
    'Ver 2.0 made by Samuel Otero -> 07/26/2013
    
    Dim oPara As Paragraph
    Dim oRng As Range
    
    'Borramos headers y footers
    Call ClearHeaderFooters
    
    'Borramos column breaks
    
     Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = "^n"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
    
    'Lógica para poner los tags de Tera donde van
        For Each oPara In ActiveDocument.Paragraphs
    
            If oPara.Range.Style = "C10" Then
                If InStr(1, oPara.Range.Text, "(intro)") = 0 And _
                   InStr(1, oPara.Range.Text, "(main)") = 0 And _
                   InStr(1, oPara.Range.Text, "(capara)") = 0 Then
                    oPara.Range.InsertBefore "(intro) "
                End If
            End If
            If oPara.Range.Style = "J10" Then
                If InStr(1, oPara.Range.Text, "(intro)") = 0 And _
                   InStr(1, oPara.Range.Text, "(main)") = 0 And _
                   InStr(1, oPara.Range.Text, "(capara)") = 0 Then
                    oPara.Range.InsertBefore "(intro) "
                End If
            End If
            If oPara.Range.Style = "J12" Then
                If InStr(1, oPara.Range.Text, "(intro)") = 0 And _
                   InStr(1, oPara.Range.Text, "(main)") = 0 And _
                   InStr(1, oPara.Range.Text, "(capara)") = 0 Then
                    oPara.Range.InsertBefore "(intro) "
                End If
            End If
            If oPara.Range.Style = "LE" Then
                If InStr(1, oPara.Range.Text, "(intro)") = 0 And _
                   InStr(1, oPara.Range.Text, "(main)") = 0 And _
                   InStr(1, oPara.Range.Text, "(capara)") = 0 Then
                    oPara.Range.InsertBefore "(main) "
                End If
            End If
            
            If oPara.Range.Style = "XL" Then
                If InStr(1, oPara.Range.Text, "(intro)") = 0 And _
                   InStr(1, oPara.Range.Text, "(main)") = 0 And _
                   InStr(1, oPara.Range.Text, "(capara)") = 0 Then
                    oPara.Range.InsertBefore "(main) "
                End If
            End If
            
             If oPara.Range.Style = "MF" Then
                If InStr(1, oPara.Range.Text, "(intro)") = 0 And _
                   InStr(1, oPara.Range.Text, "(main)") = 0 And _
                   InStr(1, oPara.Range.Text, "(capara)") = 0 Then
                    oPara.Range.InsertBefore "(main) "
                End If
            End If
            
        If oPara.Range.Style = "HG" Then
                If InStr(1, oPara.Range.Text, "(intro)") = 0 And _
                   InStr(1, oPara.Range.Text, "(main)") = 0 And _
                   InStr(1, oPara.Range.Text, "(capara)") = 0 Then
                    oPara.Range.InsertBefore "(main) "
                End If
            End If
            
            If oPara.Range.Style = "LW" Then
                If InStr(1, oPara.Range.Text, "(intro)") = 0 And _
                   InStr(1, oPara.Range.Text, "(main)") = 0 And _
                   InStr(1, oPara.Range.Text, "(capara)") = 0 Then
                    oPara.Range.InsertBefore "(main) "
                End If
            End If
            
            If oPara.Range.Style = "J8" Then
                If InStr(1, oPara.Range.Text, "(intro)") = 0 And _
                   InStr(1, oPara.Range.Text, "(main)") = 0 And _
                   InStr(1, oPara.Range.Text, "(capara)") = 0 Then
                    oPara.Range.InsertBefore "(main) "
                End If
            End If
            
    ' Agarrando texto sin estilo >_>
       
            If oPara.Range.Font.Size <= 6 Then
                If InStr(1, oPara.Range.Text, "(intro)") = 0 And _
                   InStr(1, oPara.Range.Text, "(main)") = 0 And _
                   InStr(1, oPara.Range.Text, "(capara)") = 0 Then
                    oPara.Range.InsertBefore "(main) "
                End If
            End If
    
    
            If oPara.Range.Font.Size = 8 Then
                If InStr(1, oPara.Range.Text, "(intro)") = 0 And _
                   InStr(1, oPara.Range.Text, "(main)") = 0 And _
                   InStr(1, oPara.Range.Text, "(capara)") = 0 Then
                    oPara.Range.InsertBefore "(capara) "
                    oPara.Range.InsertParagraphBefore
                    oPara.Range.InsertBefore "(start) "
                End If
            End If
            If oPara.Range.Font.Size = 10 Then
                If InStr(1, oPara.Range.Text, "(intro)") = 0 And _
                   InStr(1, oPara.Range.Text, "(main)") = 0 And _
                   InStr(1, oPara.Range.Text, "(capara)") = 0 Then
                    oPara.Range.InsertBefore "(intro) "
                End If
            End If
    
     
    
        Next oPara
     
            
    'Con esto borramos el primer espacio del documento (evitamos una línea demás en los edictos)
    Selection.HomeKey Unit:=wdStory
    Selection.MoveDown Unit:=wdLine, Count:=2
    Selection.MoveRight Unit:=wdCharacter, Count:=8
        Selection.TypeBackspace
        Selection.TypeBackspace
        Selection.TypeBackspace
        Selection.TypeBackspace
        Selection.TypeBackspace
        Selection.TypeBackspace
        Selection.TypeBackspace
        Selection.TypeBackspace
        Selection.TypeBackspace
        Selection.TypeBackspace
        Selection.TypeBackspace
        Selection.TypeBackspace
        Selection.TypeBackspace
        Selection.TypeBackspace
        Selection.TypeBackspace
        Selection.TypeBackspace
        Selection.TypeBackspace
        Selection.TypeBackspace
        Selection.TypeBackspace
        Selection.TypeBackspace
        Selection.TypeBackspace
        Selection.TypeBackspace
        Selection.TypeBackspace
        Selection.TypeBackspace
        Selection.TypeBackspace
        Selection.TypeBackspace
        Selection.TypeBackspace
        Selection.TypeBackspace
        Selection.TypeBackspace
        Selection.TypeBackspace
        Selection.TypeBackspace
        Selection.TypeBackspace
       
       
    
    'Crea el .txt para ser importado a Tera
        ChangeFileOpenDirectory "C:\edictos\"
        ActiveDocument.SaveAs FileName:="C:\edictos\Edictos.txt", FileFormat:= _
                              wdFormatText, AddToRecentFiles:=True, _
                              WritePassword:="", EmbedTrueTypeFonts:=False, _
                              SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
                              False, Encoding:=1252, InsertLineBreaks:=False, AllowSubstitutions:=False, _
                              LineEnding:=wdCRLF
                              
                                      
    
        
        MsgBox "Proceso completado", 0, "Yay!"
                             
           ActiveDocument.Close
    
    End Sub

    As you see, it ends up saving a .txt document with the tags.

    NOW... the issue is that the tags wont work anymore in the new system, and I need to wrap the paragraph in XML Tags. I tried to do something like for styles:

    If oPara.Range.Style = "LW" Then
                If InStr(1, oPara.Range.Text, "<intro>") = 0 And _
                   InStr(1, oPara.Range.Text, "<main>") = 0 And _
                   InStr(1, oPara.Range.Text, "<capara>") = 0 Then
                    oPara.Range.InsertAfter "</main> "
                    oPara.Range.InsertBefore "<main> "
                End If
            End If

    But it just would add the two tags before the paragraphs!!! Is there anyway to wrap the paragraph in XML tags, following my logic? Somebody help please!! Thank you!! :/

    Saturday, March 1, 2014 1:02 AM

Answers

  • I suggest you add a line at the top of your macro with

    Dim r As Word.Range

    then change your two oPara.Range.Insert... lines to something like the following

    Set r = oPara.Range
    r.MoveEnd wdCharacter, -1
    r.InsertBefore "<main> "
    r.InsertAfter "</main>"
    Set r = Nothing

     

    Peter Jamieson

    Sunday, March 2, 2014 12:33 PM