我需要使用模板和一些来自数据库或像数据库一样使用的 excel 文件的输入数据来动态修改 word 文档。 没用过macro和visual basic,所以要修改实际的scrip需要先了解一下。t
我已经分析了文件,这是用来自动生成文件的宏
Sub CreaDocumento()
'
' CreaDocumento Macro
'
'
Dim recordStart As Long
Dim recordEnd As Long
Dim percorsoDB As String
Dim NomeFileDB As String
Dim NomeFile As String
Dim percorsoFile As String
Dim NumRecord As Long
Dim KK As Long
Dim NomeForn As String
Dim CodPdv As Variant
Dim fDialog As Office.FileDialog
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
.AllowMultiSelect = False
.Title = "Seleziona il file da aprire"
.InitialFileName = ThisDocument.Path 'DA MODIFICARE
On Error Resume Next
.Filters.Add "Origini Dati", "*.xlsx", 2
On Error Resume Next
.FilterIndex = 2
If .Show = -1 Then
For Each selezione In .SelectedItems
percorsoDB = selezione
Next
Else
MsgBox "Operazione annullata!", vbInformation
Exit Sub
End If
If Right(percorsoDB, 5) <> ".xlsx" Then
MsgBox "Formato file non corretto! Operazione annullata!", vbInformation
Exit Sub
End If
End With
a = 0
ActiveDocument.MailMerge.OpenDataSource Name:= _
percorsoDB, ConfirmConversions:= _
False, ReadOnly:=False, LinkToSource:=True, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", WritePasswordDocument:="", _
WritePasswordTemplate:="", Revert:=False, Format:=wdOpenFormatAuto, _
Connection:= _
"Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=" & percorsoDB & ";Mode=Read;Extended Properties=""HDR=YES;IMEX=1;"";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Engine Type=37;Jet OLEDB:Databa" _
, SQLStatement:="SELECT * FROM `DATI$`", SQLStatement1:="", SubType:= _
wdMergeSubTypeAccess
c = 0
Dim objexcel As Object
Dim objworkbook As Object
Dim objworksheet As Object
Set objexcel = CreateObject("Excel.Application")
objexcel.Visible = True
Set objworkbook = objexcel.Workbooks.Open(percorsoDB)
Set objworksheet = objworkbook.Worksheets("DATI")
NomeFileDB = objworkbook.Name
NumRecord = objworkbook.Application.WorksheetFunction.CountA(objworksheet.Range("B:B"))
If NumRecord > 1 Then
For KK = 2 To NumRecord
NomeForn = objworksheet.Cells(KK, 2)
CodPdv = objworksheet.Cells(KK, 8)
percorsoFile = ThisDocument.Path
NomeFile = "AUTOR.RIFATT.DIRETTO." & NomeForn & "." & CodPdv & ".docx"
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = KK - 1
.LastRecord = KK - 1
End With
.Execute Pause:=False
End With
ChangeFileOpenDirectory percorsoFile & "\"
ActiveDocument.SaveAs2 FileName:=NomeFile, FileFormat:= _
wdFormatXMLDocument, LockComments:=False, Password:="", AddToRecentFiles _
:=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts _
:=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False, CompatibilityMode:=14
ActiveWindow.Close
Next KK
End If
objexcel.Application.Workbooks("DB.xlsx").Close SaveChanges:=False
Set objworksheet = Nothing
Set objworkbook = Nothing
Set objexcel = Nothing
End Sub
我注意到如果文档被修改,宏将停止工作,我正在考虑用专用的 javascript 工具替换宏。我想使用 nodejs,我发现微软已经发布了一个 javascript api,但我不确定我是否可以实现同样的事情。有没有比宏更好的方法来替换word文档里面的字段,自动生成需要的文档?