我的Word宏没有找到第二个单词

时间:2017-11-21 11:13:15

标签: vba ms-word

如果存在一些带有“:”字符的行,我想在Microsoft Word中创建一个VBA脚本来查找txt文件。如果这是真的,我想得到这一行,拆分它并将此信息插入主文件中的表中。对于这个目标,我想通过所有找到行来获取此信息。

为此,我有这段代码:

Dim arrNames
    Dim cont As Integer

    cont = 0

    strPath = ActiveDocument.name
    Documents.Open path & "Mails.txt"
    strPath2 = ActiveDocument.name

    With Selection.Find
        .Text = ":"
        Do While .Execute(Forward:=True, Format:=True) = True

            Selection.Find.Execute FindText:=(":")
            Selection.Expand wdLine

            arrNames = Split(Selection.Text, ":")

            Documents(strPath).Activate

            If cont = 0 Then

                Call gestOSINT("Pwd")

                Selection.Find.Execute FindText:=("[Pwd]")

                ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:= _
                    3, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
                    wdAutoFitFixed
                With Selection.Tables(1)
                    If .Style <> "Tabla con cuadrícula" Then
                        .Style = "Tabla con cuadrícula"
                    End If
                    .ApplyStyleHeadingRows = True
                    .ApplyStyleLastRow = False
                    .ApplyStyleFirstColumn = True
                    .ApplyStyleLastColumn = False
                    .ApplyStyleRowBands = True
                    .ApplyStyleColumnBands = False
                End With
                Set tblNew = Selection.Tables(1)

                tblNew.Style = "Tabla de lista 1 clara - Énfasis 1"
                Selection.TypeText Text:="Correo electrónico"
                Selection.MoveRight Unit:=wdCell
                Selection.TypeText Text:="Tipo de filtrado"
                Selection.MoveRight Unit:=wdCell
                Selection.TypeText Text:="Plataforma"
            End If



            Set rowNew = tblNew.Rows.Add

            rowNew.Cells(1).Range.Text = arrNames(0)
            rowNew.Cells(2).Range.Text = arrNames(1)
            rowNew.Cells(3).Range.Text = arrNames(2)

            cont = cont + 1
            Documents(strPath2).Activate
            Selection.Text = arrNames(0) & vbCrLf


            Selection.MoveDown Unit:=wdLine, Count:=1
            Selection.Collapse wdCollapseEnd


        Loop
    End With



    Documents(strPath2).Activate
    ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
    Documents(strPath).Activate

    If cont = 0 Then
        pwdMails = False
    Else
        pwdMails = True
    End If

并且Mails.txt文件包含以下内容:

mail@mail.com
mail2@mail.com
mail3@mail.com:word1:word2
mail4@mail.com
mail5@mail.com:word3:word4

在Mails.txt中找到包含“:”,第3行的第一行,但未找到第二行,即Mails.txt中的第5行。

为什么会这样?我该如何解决?

1 个答案:

答案 0 :(得分:0)

这是一个通过FileSystemObject读取文件并避免使用JSON的版本。请注意,我注释掉了对我不起作用的行(样式名称,自定义函数)。 另外:您正在将两种样式应用于表格,先是一种,然后是另一种。请选一个。 ; - )

Selection
相关问题