word vba表格汇总(根据模板生成word文档并填充数据)

Option Compare DatabaseOption Explicit,今天小编就来聊一聊关于word vba表格汇总?接下来我们就一起去研究一下吧!

word vba表格汇总(根据模板生成word文档并填充数据)

word vba表格汇总

Option Compare Database

Option Explicit

Private Sub Command生成_Click()

On Error GoTo outputerror

'输入文件名

Dim outputname As String

outputname = InputBox("请输入导出的文件名", "生成文件", "准考证:" & Me.准考证号) '输入要生成的表名-

If outputname = "" Or IsNull(outputname) Then '为空则不执行程序

Exit Sub

End If

'选择导出的位置(文件夹)

Dim exportpath As String

Dim dlgOpen As FileDialog

Set dlgOpen = application.FileDialog(msoFileDialogFolderPicker)

With dlgOpen

If .Show = -1 Then

exportpath = .SelectedItems(1)

Else

Exit Sub

End If

End With

Dim appword As New Word.application

Dim worddoc As Object

appword.Visible = True

Dim modelpathname As String

modelpathname = CurrentProject.Path & "\" & "准考证模板.dotx" '模板文件

Dim newwordpathname As String

newwordpathname = exportpath & "\" & outputname & ".docx"

If IsFileExists(newwordpathname) = True Then

MsgBox "该文档已存在"

GoTo 存在则跳过

End If

Set worddoc = appword.documents.Add(template:=modelpathname, Visible:=True)

appword.Selection.Goto what:=wdGoToBookmark, Name:="学号"

appword.Selection.typetext Text:=Me.学号

appword.Selection.Goto what:=wdGoToBookmark, Name:="姓名"

appword.Selection.typetext Text:=Me.姓名

appword.Selection.Goto what:=wdGoToBookmark, Name:="性别"

appword.Selection.typetext Text:=Me.性别

appword.Selection.Goto what:=wdGoToBookmark, Name:="班级"

appword.Selection.typetext Text:=Me.班级

appword.Selection.Goto what:=wdGoToBookmark, Name:="考场"

appword.Selection.typetext Text:=Me.考场

appword.Selection.Goto what:=wdGoToBookmark, Name:="准考证号"

appword.Selection.typetext Text:=Me.准考证号

worddoc.SaveAs newwordpathname, wdformatdocumentdefault ' & ".docx"

worddoc.Close

Set worddoc = Nothing

存在则跳过:

appword.Quit

Set appword = Nothing

MsgBox "生成完成"

'------------------------------------------

Exit Sub

outputerror:

MsgBox Err.Description

End Sub

Function IsFileExists(ByVal strFileName As String) As Boolean '判断文件是否存在

If Len(Dir(strFileName)) <> 0 Then

IsFileExists = True

Else

IsFileExists = False

End If

End Function

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页