WDAPP.Selection.MoveDown(Unit:=WDAPP.wdParagraph, Count:=1) 拋出異常。wdParagraph 是 WdUnits 枚舉常量(值 4),但代碼中 `WDAPP` 定義為 `Object`(晚綁定),無法直接訪問 `WDAPP.wdParagraph`。應使用數值或引用 `Microsoft.Office.Interop.Word` 獲取枚舉。
代碼中兩次 `Documents.Add` 創建了兩個文檔實例,但**未正確關閉/釋放 COM 對象**,導致 Word 進程殘留,文件句柄未釋放。
Imports Microsoft.Office.Interop.Word
Imports System.Runtime.InteropServices
Public Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode <> Keys.Enter Then Return
Dim findText = InputBox("請輸入查找內容")
If String.IsNullOrEmpty(findText) Then Return
Dim wordApp As New Application() With {.Visible = False}
Dim srcDoc As Document = Nothing
Dim dstDoc As Document = Nothing
Try
' 1. 打開源文檔(而非 Add 新建)
srcDoc = wordApp.Documents.Open(TextBox1.Text, ReadOnly:=True)
' 2. 創建目標文檔
dstDoc = wordApp.Documents.Add()
' 3. 查找並複製內容
With wordApp.Selection
.Find.ClearFormatting()
.Find.Text = findText
.Find.Execute()
If .Find.Found Then
' 獲取頁碼/位置信息
Dim info = .Information(WdInformation.wdActiveEndPageNumber)
' 輸入找到的文本
.TypeText(findText)
' 正確移動到下一段
.MoveDown(WdUnits.wdParagraph, 1)
' 設置製表位
.ParagraphFormat.TabStops.Add(
Position:=wordApp.CentimetersToPoints(15.24),
Alignment:=WdTabAlignment.wdAlignTabRight,
Leader:=WdTabLeader.wdTabLeaderDashes)
.TypeText(vbTab)
.TypeText(info.ToString())
.TypeParagraph()
End If
End With
' 4. 保存並關閉
dstDoc.SaveAs2(TextBox2.Text)
Catch ex As Exception
MessageBox.Show($"錯誤: {ex.Message}")
Finally
' 5. 嚴格釋放 COM 對象(逆序)
If dstDoc IsNot Nothing Then
dstDoc.Close(SaveChanges:=False)
Marshal.ReleaseComObject(dstDoc)
End If
If srcDoc IsNot Nothing Then
srcDoc.Close(SaveChanges:=False)
Marshal.ReleaseComObject(srcDoc)
End If
wordApp.Quit()
Marshal.ReleaseComObject(wordApp)
' 強制 GC 回收
GC.Collect()
GC.WaitForPendingFinalizers()
End Try
End Sub
| 原問題 | 修正方案 |
|---|---|
| `WDAPP.wdParagraph` 晚綁定失敗 | 引用 Interop 組件,使用強類型 `WdUnits.wdParagraph` |
| `Documents.Add(TextBox1.Text)` 嘗試用文本打開文件 | 改用 `Documents.Open(path, ReadOnly:=True)` |
| 未關閉文檔/退出 Word 進程 | `Finally` 塊中 `Close` → `ReleaseComObject` → `Quit` → `GC.Collect` |
| 兩次 `Add` 創建多餘文檔 | 源文檔 `Open`,目標文檔 `Add`,邏輯清晰 |
| 選區操作依賴活動窗口 | 顯式指定 `srcDoc.Content` / `dstDoc.Content` 範圍,避免 `Selection` 競爭 |
VB.NET 操作 Word 核心在於:**早綁定、範圍操作、嚴格釋放 COM**。修正 `MoveDown` 參數、改用 `Open` 替代 `Add`、添加完整的 `Finally` 清理邏循,即可同時解決運行時異常與文件鎖定問題。