ExcelTip.Net留存知识帖 ---【注:附件之前被网盘供应商清空后,现已修复-现已修复-现已修复为本地下载!】
现在位置:首页 > 我的酷贴 > Excel VBA > 【VBA範例】:如何在Excel中操控MS Word、如何在MS Word中控制Excel

【VBA範例】:如何在Excel中操控MS Word、如何在MS Word中控制Excel

作者:绿色风 分类: 时间:2022-08-18 浏览:116
楼主
allall
如何在Excel中操控MS Word

若運用VBA得當,不但Word可以操控Excel,Excel也可以操控Word.
因為Excel無法計算字數,欲在Excel中計算字數,必須借用Word的算字功能.
下面這個程式將Excel中所選之區域貼到Word中,算出字數之後,將結果存回Excel.
====================================================

  1. Option Explicit[code]Sub count_in_Word()
  2. '在Excel中使用MS Word來計算稿名字數
  3. '
  4. '使用方法
  5. '假設稿名旁邊那一行空白
  6. '一、選擇要計算的稿名
  7. '二、執行本程式
  8. '
  9. 'In order to use this code you must set a reference to the
  10. 'Word object library by doing this. In the VB Editor click
  11. 'Tools, References. Then search for Microsoft Word n.n Object Library
  12. 'where n.n will depend on your version of Word.
  13. '
  14. '本程式最後修改日期:2008-8-21
  15. '
  16.     Dim wdApp As Word.Application, wdDoc As Word.Document
  17.     Dim oWdRange As Word.Range
  18.     Dim iLoop As Integer
  19.     Dim iRow As Integer
  20.     Dim iColumn As Integer
  21.     Dim l As Integer
  22.     Dim n As Integer
  23.     Dim m As Integer
  24.     Dim iBlock As Integer
  25.     Dim iRemainder As Integer
  26.     Dim iTotalWords1 As Integer
  27.     Dim iTotalWords2 As Integer
  28.     Application.DisplayAlerts = False
  29.     Application.ScreenUpdating = False
  30.     Application.Calculation = xlCalculationManual
  31.     On Error Resume Next
  32.     Set wdApp = GetObject(, "Word.Application")
  33.     If Err.Number <> 0 Then    'Word isn't already running
  34.         Set wdApp = CreateObject("Word.Application")
  35.     End If
  36.     On Error GoTo 0
  37.     Set wdDoc = wdApp.Documents.Add("Normal", False, 0)
  38.     '儲存時間,以計算本程式之執行時間
  39.     Cells(1, 6) = Now()
  40.     ThisWorkbook.Sheets("sheet1").Select
  41.     '找出選區之第一格及總格數
  42.     With Selection
  43.         iRow = Selection.Row()
  44.         iColumn = Selection.Column()
  45.         iLoop = Selection.Rows.Count
  46.     End With
  47.     '每次處理六格
  48.     iBlock = 6
  49.     '算餘數
  50.     iRemainder = iLoop Mod iBlock
  51.     '一定得用\,不能用/.後者會自動四捨五入
  52.     m = iLoop \ iBlock
  53.     For l = 1 To m
  54.         '每六格一選
  55.         Range(Cells(iRow + (l - 1) * iBlock, iColumn), Cells(iRow + l * iBlock - 1, iColumn)).Select
  56.         With Selection
  57.             Selection.Copy
  58.         End With
  59.         '貼到MS Word
  60.         wdApp.Selection.PasteSpecial DataType:=wdPasteText
  61.         For n = 1 To iBlock
  62.             '先算總字數
  63.             iTotalWords1 = wdApp.ActiveDocument.ComputeStatistics(wdStatisticWords)
  64.             '選取第一段,刪除之後,再算總字數.二者差額即第一段之字數
  65.             Set oWdRange = wdApp.ActiveDocument.Paragraphs(1).Range
  66.             oWdRange.Delete
  67.             iTotalWords2 = wdApp.ActiveDocument.ComputeStatistics(wdStatisticWords)
  68.             '將算出之字數存回Excel
  69.             Cells(iRow + (l - 1) * iBlock + n - 1, iColumn + 1).Value = iTotalWords1 - iTotalWords2
  70.         Next n
  71.     Next l
  72.     '處理最後一段
  73.     If iRemainder Then
  74.         Range(Cells(iRow + (l - 1) * iBlock, iColumn), Cells(iRow + (l - 1) * iBlock + iRemainder - 1, iColumn)).Select
  75.         With Selection
  76.             Selection.Copy
  77.         End With
  78.         wdApp.Selection.PasteSpecial DataType:=wdPasteText
  79.         For n = 1 To iRemainder
  80.             '先算總字數
  81.             iTotalWords1 = wdApp.ActiveDocument.ComputeStatistics(wdStatisticWords)
  82.             '選取第一段,刪除之後,再算總字數.二者差額即第一段之字數
  83.             Set oWdRange = wdApp.ActiveDocument.Paragraphs(1).Range
  84.             oWdRange.Delete
  85.             iTotalWords2 = wdApp.ActiveDocument.ComputeStatistics(wdStatisticWords)
  86.             Cells(iRow + (l - 1) * iBlock + n - 1, iColumn + 1).Value = iTotalWords1 - iTotalWords2
  87.         Next n
  88.     End If
  89.     Cells(2, 6) = Now()
  90.     Application.ScreenUpdating = True
  91.     Application.DisplayAlerts = True
  92.     Application.Calculation = xlCalculationAutomatic
  93.     wdDoc.Close False
  94.     wdApp.Quit
  95. End Sub
如何在MS Word中控制Excel

VBA的功能相當完備,可以由一個微軟程式來控制另一個微軟程式,例如它可以讓Excel控制Word,也可以讓Word控制Excel.今天先介紹如何由Word來控制Excel.
因為Excel並無計算字數的功能,所以想要計算字數時,只好借重Word.下面這個例子是先將要計算字數的那些格子貼到Word,Word算了字數後,會將結果直接輸到Excel.
====================


  1. Option Explicit
  2. '本程式計算每一段之字數,
  3. '然後將結果存於Excel檔案中
  4. '
  5. '使用方法
  6. '一、將要計算之格子由Excel(file1.xlsm)貼到Word(test.docx)裡
  7. '二、執行本程式
  8. '假設:file1.xlsm之第四行用以儲存字數,第六行之第一、二格儲存時間,
  9. '以計算程式執行時間.
  10. '
  11. '
  12. '使用前要先選用Excel Object Library.方法如下:
  13. ' 在Word裡,先進入VBA(Word 2007:developer/VB)然後選 Tools/References……接著勾選
  14. 'Microsoft Excel x.x Object Library.我的 x.x 是 12.0)
  15. Sub count_words_table()
  16. Dim oWdRange As Word.Range
  17. Dim iTotalWords1 As Integer
  18. Dim iTotalWords2 As Integer
  19. Dim iLoop As Integer
  20. Dim n As Integer
  21. Dim oRow As Row
  22. Dim oXL As Excel.Application
  23. Dim oWB As Excel.Workbook
  24. Dim oRng As Excel.Range
  25. Dim ExcelWasNotRunning As Boolean
  26. Dim WorkbookToWorkOn As String

  27. Application.ScreenUpdating = False
  28. '指定所要使用之 Excel 檔案名稱
  29. WorkbookToWorkOn = "C:\Documents and Settings\me\My Documents\file1.xlsm"

  30. Windows("test.docx").Activate
  31. '隱藏視窗,執行速度較快
  32. Application.Visible = False
  33. 'If Excel is running, get a handle on it; otherwise start a new instance of Excel
  34. On Error Resume Next
  35. Set oXL = GetObject(, "Excel.Application")
  36. If Err Then
  37.    ExcelWasNotRunning = True
  38.    Set oXL = New Excel.Application
  39. End If
  40. On Error GoTo Err_Handler

  41. '打開 Excel 檔案
  42. Set oWB = oXL.Workbooks.Open(FileName:=WorkbookToWorkOn)
  43. oWB.Application.ScreenUpdating = False
  44. '儲存程式開始執行的時間,用以計算本程式執行時間
  45. oXL.ActiveWorkbook.Worksheets("綜合").Cells(1, 6).Value = Now()
  46. '計算總共執行次數,一段一次
  47. iLoop = ActiveDocument.ComputeStatistics(wdStatisticParagraphs)
  48. For n = 1 To iLoop
  49. '先算總共的字數
  50. iTotalWords1 = ActiveDocument.ComputeStatistics(wdStatisticWords)

  51. '選取第一段,刪去,再算字數.二者之差即第一段之字數
  52. Set oWdRange = ActiveDocument.Paragraphs(1).Range
  53. oWdRange.Delete
  54. iTotalWords2 = ActiveDocument.ComputeStatistics(wdStatisticWords)
  55. '直接將結果儲存到 Excel 檔中
  56. oXL.ActiveWorkbook.Worksheets("sheet1").Cells(n, 4).Value = iTotalWords1 - iTotalWords2
  57. Next n

  58. '儲存程式結束的時間
  59. oXL.ActiveWorkbook.Worksheets("綜合").Cells(2, 6).Value = Now()
  60. oWB.Save
  61. oWB.Close False

  62. 'quit
  63. If ExcelWasNotRunning Then
  64.   oXL.Quit
  65. End If
  66. 'Make sure you release object references.
  67. Set oWB = Nothing
  68. Set oXL = Nothing
  69. Application.ScreenUpdating = True
  70. Application.Visible = True
  71. Exit Sub
  72. Err_Handler:
  73.    MsgBox WorkbookToWorkOn & " caused a problem. " & Err.Description, vbCritical, _
  74.            "Error: " & Err.Number
  75.    If ExcelWasNotRunning Then
  76.        oXL.Quit
  77.    End If
  78. End Sub
2楼
omnw
为了方便阅读代码,请将你帖子中的代码按照我编辑后的样子.
帖子的内容较多,建议拆分成两个帖子.
3楼
hwh6963
能否给一附件说明,谢谢

免责声明

有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素, 经与ExcelTip.Net站长Apolloh商议并征得其同意, 现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示, 供有需要的人士查询使用,也慰缅曾经的论坛时代。 所示各个帖子的原作者如对版权有异议, 可与本人沟通提出,或于本站点留言,我们会尽快处理。 在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一

评论列表
sitemap