[英]VBA Autofill for every row
First of all, I dont have any experience with VBA. 首先,我对VBA没有任何经验。 What i have here is based on what i've read from various tutorial.
我在这里的内容是基于我从各种教程中读到的内容。 I have data in Excel sheet that i wanted to insert into the bookmarks in MS Word as auto-fill.
我在Excel工作表中有数据,我想在MS Word中插入书签作为自动填充。 Each row of data will be saved as one MS Word doc file.
每行数据将保存为一个MS Word doc文件。 The problem i'm having is i dont know how to continuously auto-fill on the next row until the row in column A is blank
我遇到的问题是我不知道如何连续自动填充下一行,直到A列中的行为空
Dim wdApp As Word.Application
Dim myDoc As Word.Document
Dim mywdRange As Word.Range
Dim Tagno As Range
Dim Csheetno As Range
Set wdApp = New Word.Application
With wdApp
.Visible = True
.WindowState = wdWindowStateMaximize
End With
'Autofill start
'-----Set Range-----
Set myDoc = wdApp.Documents.Add(Template:="C:\Template\" & Range("B2") & ".docx")
Set Tagno = Sheets("Autofill").Range("A2")
Set Csheetno = Sheets("Autofill").Range("B2")
'-----Paste to msWord bookmarks-----
With myDoc.Bookmarks
.Item("tagno").Range.InsertAfter Tagno
.Item("csheetno").Range.InsertAfter Csheetno
End With
myDoc.SaveAs "C:" & "\" & Range("A2") & "_" & Range("B2") & ".docx"
myDoc.Close False
wdApp.Quit
Set doc = Nothing
Set wd = Nothing
Try something based on: 尝试一下基于:
Sub Demo()
Dim wdApp As New Word.Application, wdDoc As Word.Document, r As Long
'Hide Word
wdApp.Visible = False
'Create, populate & save Word documents, 1 per row.
With Sheets("Autofill")
For r = 2 To .UsedRange.Cells.SpecialCells(xlCellTypeLastCell).Row
If .Range("A" & r).Text = "" Then GoTo ErrExit
'Create document
Set wdDoc = wdApp.Documents.Add(Template:="C:\Template\" & .Range("B" & r).Text & ".docx")
'-----Update Word bookmarks-----
wdDoc.Bookmarks("tagno").Range.InsertAfter .Range("A" & r).Text
wdDoc.Bookmarks("csheetno").Range.InsertAfter .Range("B" & r).Text
'Save & close document
wdDoc.SaveAs "C:" & "\" & .Range("A" & r).Text & "_" & .Range("B" & r).Text & ".docx", _
FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
wdDoc.Close False
Next
End With
ErrExit:
wdApp.Quit
Set wdDoc = Nothing: Set wdApp = Nothing
End Sub
Note: You should be using Word templates as templates, not Word documents. 注意:您应该使用Word模板作为模板,而不是Word文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.