当前位置: 首页 > 知识库问答 >
问题:

将文本复制到带有表的文档中

江飞白
2023-03-14

我有一个从Excel生成文档的代码。一方面,我有一个需要填充标记的文档,以生成与Excel中的行一样多的文档。另一方面,我有一个Excel,它在一行中有这个标记,并在Excel的每一行中填写Word文档中要填写的信息。

代码在只有文本和标签的普通文档中工作正常,但是当文档包含填充有文本的表时,它就不工作了...

这些是Excel和文档的一些图像...

带标签的Excel

普通文件

带表格的文件

这是代码:

Sub generate_documents()

    intAnswer = MsgBox("Se dispone a generar los escritos. Antes de continuar confirme que los datos incluidos en la pestaña DATOS son correctos." & Chr(10) & Chr(10) & "¿Está seguro de continuar?", vbYesNo, "ATENCIÓN")

    If (intAnswer <> 6) Then Exit Sub

    Application.Cursor = xlWait
    Application.ScreenUpdating = False

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set objFolder = fso.GetFolder(ActiveWorkbook.Path)

    strWrittensPath = ActiveWorkbook.Path & "\ESCRITOS (" & Format(Now, "dd-mm-yyyy hhnnss") & ")"
    fso.CreateFolder (strWrittensPath)

    Dim wdApp As Object
    Set wdApp = Nothing
    Set wdApp = CreateObject("Word.Application")
    wdApp.Visible = True

    intLastRow = Worksheets("DATOS").Range("A" & Rows.Count).End(xlUp).Row

    For Each objFile In objFolder.Files

        If objFile.Type = "Microsoft Word Document" And Left(objFile.Name, 1) <> "~" Then

            For i = 2 To intLastRow

                    strData = " "
                    intColumn = 1

                    wdApp.Documents.Open (objFile.Path)
                    wdApp.ActiveDocument.SaveAs (strWrittensPath & "\" & Worksheets("DATOS").Cells(i, intColumn).Value)

                    Do While strData <> ""

                        intColumn = intColumn + 1

                        strData = Worksheets("DATOS").Cells(1, intColumn).Value
                        strReplace = Worksheets("DATOS").Cells(i, intColumn).Value
                        strReplace = Replace(strReplace, Chr(10), vbCr)

                        If strData <> "" And strReplace <> "" Then

                             If InStr(wdApp.ActiveDocument.Content, strData) = 0 Then

                                intAnswer = MsgBox("No se ha encontrado la etiqueta " & strData & " en el archivo WORD." & Chr(10) & Chr(10) & "¿Desea continuar igualmente?", vbYesNo, "ATENCIÓN")
                                If (intAnswer = 7) Then

                                    wdApp.ActiveDocument.Save
                                    wdApp.ActiveDocument.Close
                                    wdApp.Quit
                                    Set wdApp = Nothing
                                    fso.DeleteFolder (strWrittensPath)
                                    Application.Cursor = xlDefault
                                    Exit Sub

                                End If

                            Else

                                    wdApp.ActiveDocument.Content.Find.Execute _
                                    FindText:=strData, ReplaceWith:=strReplace, Replace:=2, Forward:=True, MatchWholeWord:=True

                                End If

                            End If

                    Loop

                    wdApp.ActiveDocument.Save
                    wdApp.ActiveDocument.Close

            Next i

        End If

    Next objFile

    wdApp.Quit
    Set wdApp = Nothing
    Application.CutCopyMode = False
    Application.Cursor = xlDefault
    Application.ScreenUpdating = True

    intAnswer = MsgBox("Los documentos se han generado con exito." & Chr(10) & Chr(10) & "¿Desea abrir la carpeta que contiene los documentos?", vbYesNo, "Información")
    If (intAnswer = 6) Then Shell "explorer.exe" & " " & strWrittensPath, vbNormalFocus

End Sub

共有1个答案

宦书
2023-03-14

它工作得很好,问题是我试图写一个扩展名为. doc的文档,而不是. docx,现在有了. docx,就没有问题了。

 类似资料:
  • 我已经很多年没有使用VB了,所以如果这是显而易见的,请原谅我。我正在尝试编写一个word vba宏,以便在模板中使用,该模板将显示一个userform,然后导入fileA的内容。docx,fileB。docx或fileC。docx取决于用户表单。(之后我将使用书签填写一些表单数据,我不知道这是否相关)。文件A、B和C将包含一些基本格式(如列表)的文本,但没有什么特别之处。 我在网上看到的解决方案可

  • 如何将一个文本文件复制到另一个文本文件中?我试过这个: 这只是在中留下以下值:。 我做错了什么?

  • 我的s3存储桶中有很多文件,所以是否有任何aws cli命令可用于在s3中查找带有前缀名的最新文件?如何将该文件从s3复制到本地文件夹?我可以使用Boto3或python库来实现这一点吗?

  • 本文向大家介绍在MongoDB中找到带有ObjectID的文档?,包括了在MongoDB中找到带有ObjectID的文档?的使用技巧和注意事项,需要的朋友参考一下 要在MongoDB中查找带有Objectid的文档,请使用以下语法- 为了理解上述语法,让我们用文档创建一个集合。使用文档创建集合的查询如下- 在method的帮助下显示集合中的所有文档。查询如下- 以下是输出- 情况1-这是在Mong

  • 问题内容: 我在将包含该文件夹中文件的文件夹复制到另一个文件夹时遇到一些问题。Command 不会复制文件夹中的文件。 问题答案: 您正在寻找的选项是。 如果不存在,将创建它。 意味着。您也可以使用,因为它不区分大小写。 请注意根据@ muni764的注释添加尾随的细微差别。