简体   繁体   English

如何在 VBA 中将 Word 模板文件另存为 a.docx? 或者从一张 excel 表创建多个特定文档?

[英]How to save a Word template file as a .docx in VBA? Or create multiple specific documents from one excel sheet?

I am trying to copy a bunch of text data from an excel spreadsheet into multiple separate word documents (one excel row = one document, but each column's heading has to be included before the text from the respective field of the row).我正在尝试将 excel 电子表格中的一堆文本数据复制到多个单独的 word 文档中(一个 excel 行 = 一个文档,但每列的标题必须包含在该行相应字段的文本之前)。 I also want these documents' names to be the text from specific fields in the spreadsheet (row headers).我还希望这些文档的名称是电子表格中特定字段(行标题)中的文本。

Because I want fancy formatting, and a specific order of copying/pasting things (not all fields are included), I am using a word template with bookmarks that I can feed into VBA.因为我想要花哨的格式,以及复制/粘贴的特定顺序(并非所有字段都包括在内),所以我使用带有书签的 word 模板,我可以将其输入 VBA。 It fills the template well, but I cannot save it as a standard word document before repeating the loop.它很好地填充了模板,但在重复循环之前我无法将其保存为标准 Word 文档。 I get the error 'Object doesn't support this property or method'.我收到错误“对象不支持此属性或方法”。 Is there a way to overcome it, or a more elegant method I've missed?有没有办法克服它,或者我错过了更优雅的方法?

Here is the code:这是代码:

Sub Primitive()

Dim objWord As Object
Dim ws As Worksheet
Dim i As Integer


   
    Set ws = ThisWorkbook.Sheets("Sheet1")
    i = 2 ' First row to process
    'Start of loop
    Do Until ws.Range("B" & i) = ""
         Set objWord = CreateObject("Word.Application")
         objWord.Visible = True
     'Change to local path of template file
     

         objWord.Documents.Open "C:path of template file.dotx"
         With objWord.ActiveDocument


              .Bookmarks("FirstBookmark").Range.Text = ws.Range("B" & i).Value & " " & ws.Range("C" & i)
              .Bookmarks("headlineZ").Range.Text = ws.Range("Z1").Value

lots of this^ here to arrange the data right in the document很多这个^在这里安排文档中的数据

           
              NewFileName = "C:\path of where I want the new file" & ws.Range("C" & i).Value & ".docx"

This is the line that gives the error 483:这是给出错误 483 的行:

    objWord.SaveAs2 Filename:="NewFileName"
    End With
    

    objWord.Close

 Set objWord = Nothing
        

              
        i = i + 1
        Loop
   


End Sub

You are trying to save Word, rather than the document.您正在尝试保存 Word,而不是文档。 If you replace this如果你更换这个

objWord.SaveAs2 Filename:="NewFileName"

with this有了这个

objWord.ActiveDocument.SaveAs2 Filename:="NewFileName"

it should work better.它应该工作得更好。 That said, you should not use ActiveDocument in your macros.也就是说,您不应该在宏中使用 ActiveDocument。 Consider replacing考虑更换

objWord.Documents.Open "C:path of template file.dotx"

with something like有类似的东西

Set TargetDocument = objWord.Documents.Open("C:path of template file.dotx")

and replace all ActiveDocument with TargetDocument .并将所有ActiveDocument替换为TargetDocument

Thanks everyone for the corrections - I'm completely new to VB so some issues were bound to happen.感谢大家的更正 - 我对 VB 完全陌生,所以肯定会发生一些问题。 Eventually I managed to get this working via a mail merge, then splitting the merge into individual documents using an add-in ( http://www.gmayor.com/individual_merge_letters.htm ).最终,我设法通过邮件合并来完成这项工作,然后使用插件( http://www.gmayor.com/individual_merge_letters.htm )将合并拆分为单独的文档。

I will have a look at the code corrections when I have time and see if I can get this working via VB as well.当我有时间时,我会看看代码更正,看看我是否也可以通过 VB 让它工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 excel vba 从行创建多个 word 文档? - How to create multiple word documents from rows using excel vba? Excel VBA打开word模板,填充,然后另存为.docx文件 - Excel VBA to open word template, populate, then save as .docx file somewhere else Excel VBA-将特定的列从多个文件合并到一张表 - Excel VBA - Merge specific columns from multiple files to one sheet 如何从一张 excel 表创建多个 CSV 文件 - VBA - How can I create multiple CSV Files from one excel sheet - VBA 将多个excel表格中的表格粘贴到word文件中,并将word命名为表格名称VBA - Paste table from multiple excel sheets into word file and name word as sheet name VBA VBA Excel尝试创建一个从文件导入数据的宏,然后如果数据等于特定值,则将一个单元格放到新文件的工作表中 - VBA excel trying to create a macro that imports data from file then if the data is equal to a specific value put one cell into a sheet on the new file 如何使用特定模板创建Excel工作表? - how to create excel sheet with specific template? 从 excel 中的数据创建一个 word docx - 每行一个 doc - Create a word docx from data in excel - one doc per row 如何将一张Excel工作表和Word文档保存成单个pdf和VBA - How to save an Excel sheet and Word document into single pdf with VBA 如何使用VBA将多个Excel工作表合并为一个Excel工作表 - How to combine multiple Excel sheet into one Excel sheet using VBA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM