[英]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.