Question

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!! :/

Was it helpful?

Solution

I'm not that familiar with Word VBA but this worked for me:

Sub TT()

Dim p As Paragraph, r As Range

    For Each p In ActiveDocument.Paragraphs
        p.Range.InsertBefore "<main>"
        Set r = p.Range
        r.Collapse wdCollapseEnd
        r.Move wdCharacter, -1
        r.InsertAfter "</main>"
    Next p

End Sub

If you just insert after the whole paragraph range, then you're adding the text after the "paragraph" mark, when you want to add it before.

Take a look at the Word VBA Help for the Range.Collapse method - it has a section explaining this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top