尝试在XML标记中包装Microsoft Word段落有什么问题?!? :/

时间:2014-03-01 00:58:15

标签: xml vba ms-word ms-office

我真的希望这不是一个大问题,你的一些人可以提供帮助。我在一年前在VBA写了一篇Macro,它意图在文档的每一段之前加上一个标记。逻辑验证该段落在放置之前没有其他标记。所有这些都基于风格。某些字体样式最终会有不同的标签。这是我的代码,它可以工作:

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

如您所见,最终会使用标记保存.txt文档。

现在......问题是标签在新系统中不再起作用了,我需要将该段包装在XML标签中。我尝试做类似风格的事情:

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

但它只会在段落之前添加两个标签!无论如何,按照我的逻辑将段落包装在XML标签中?请有人帮忙!!谢谢!! :/

1 个答案:

答案 0 :(得分:1)

我对Word VBA并不熟悉,但这对我有用:

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

如果您只是在整个段落范围之后插入,那么当您想要在之前添加文本时,您将在“段落”标记之后添加文本。

查看Range.Collapse方法的Word VBA帮助 - 它有一节解释这一点。