Sub export_ti_an_report()
Dim tmpFilePath As String
tmpFilePath = ThisWorkbook.Path & "\公司提案承辦表模板.wpt"
If Not FileExists(tmpFilePath) Then
MsgBox ("導出失敗:" & vbNewLine & "當前目錄下沒有找到導出模板文件“公司提案承辦表模板.wpt”!" & vbNewLine & "請將導出模板文件“公司提案承辦表模板.wpt”放到當前文件目錄下。")
Exit Sub
End If
Dim activeRow As Integer
'Dim activeColumn As Integer
' 獲取活動單元格的行號和列號
activeRow = ActiveCell.Row
'activeColumn = ActiveCell.Column
If activeRow < 3 Then
Exit Sub
End If
If Sheets("提案數據庫").Range("A3").Value & "CS" = "CS" Then
MsgBox ("當前行沒有需要保存的公司提案承辦表!請檢查。")
Exit Sub
End If
result = MsgBox("你確定要導出公司提案承辦表到WPS文件嗎?", vbYesNo, "確認對話框")
' 檢查用戶的選擇
If result = vbNo Then
Exit Sub
End If
Dim wordApp As Object
Dim wordDoc As Object
Dim findText As String
Dim replaceText As String
' 創建Word應用程序對象
Set wordApp = CreateObject("Word.Application")
wordApp.Visible = True
Set wordDoc = wordApp.Documents.Open(tmpFilePath)
Set oTable = wordApp.ActiveDocument.Tables(1)
For i = 3 To 1000
If Sheets("提案數據庫").Range("A" & i).Value & "CS" <> "CS" Then
oTable.Rows.Add '然后按照統計的行數進行對第5個表格當中進行行的插入
oTable.Cell(i + 1, 1).Range.Text = Sheets("提案數據庫").Range("C" & i).Value
oTable.Cell(i + 1, 2).Range.Text = Sheets("提案數據庫").Range("D" & i).Value
oTable.Cell(i + 1, 3).Range.Text = Sheets("提案數據庫").Range("I" & i).Value
oTable.Cell(i + 1, 4).Range.Text = Sheets("提案數據庫").Range("J" & i).Value
Else
Exit For
End If
Next
'刪除首行,首行用于儲存數據行格式信息
Set oRow = oTable.Rows(2) ' Word中的行索引從1開始
oRow.Delete
' 關閉Word文檔和應用程序
Dim fileName As String
fileName = "D:\公司提案承辦表(" & Year(Date) & "年" & Month(Date) & "月).wps" ' 修改為輸出文件的實際路徑
wordDoc.SaveAs fileName
wordDoc.Close
wordApp.Quit
' 釋放對象
Set wordDoc = Nothing
Set wordApp = Nothing
MsgBox "已將文件保存到:“" & fileName & "”"
End Sub