|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Sub 生成word()
Application.ScreenUpdating = False
Dim Str1, Str2, curpath, wordname, wordpathname, i, j, myPath As String '导出文件名wordname, 导出路径文件名wordpathname
Dim Nsht As FileDialog '打开保存对话框
Application.ScreenUpdating = False
Set wdapp = CreateObject("Word.Application")
i = ThisWorkbook.Path & "\设计费情况说明.docx"
wordname = "设计费情况说明"
wordpathname = "\" & wordname & "_" & Right(Sheet1.Range("B2"), Len(Sheet1.Range("B2")) - 9) & ".docx" '拼接word的名字
wdapp.Visible = True '可见
Set wdoc = wdapp.Documents.Open(i) '打开word
With wdoc.Tables(1)
.cell(3, 2).Range.Text = Sheet1.Cells(2, 1)
.cell(4, 2).Range.Text = Sheet1.Cells(2, 2)
.cell(10, 2).Range.Text = Sheet1.Cells(2, 3)
.cell(11, 2).Range.Text = Sheet1.Cells(2, 4)
End With
'导出文件
Set Nsht = Application.FileDialog(msoFileDialogFolderPicker)
With Nsht
If .Show = -1 Then
' 如果单击了确定按钮,则将选取的路径保存在变量中
myPath = .SelectedItems(1)
End If
End With
wdoc.SaveAs Filename:=myPath & wordpathname
wdapp.Quit
Application.ScreenUpdating = True
End Sub
|
|