2013-11-28 29 views
0

我得到消息如何通過VBA宏在讀取中寫入更多文字並將文本寫入文檔?

  1. 打開讀取可用 當我需要打開在讀寫模式下的文檔,和之前的宏的文檔關閉僅
  2. 本地副本
  3. 收到通知執行

而且我ahve在路上懷疑我寫的文字

Sub Read_Write_Document() 

    Dim p As Long, r As Long 
    Set wrdApp = CreateObject("Word.Application") 

    wrdApp.Visible = True 

    Set wrdDoc = wrdApp.Documents.Open("C:\Documents and Settings\Desktop\Word_File_read_write_1.docx") 

    Dim i As Integer 
    i = 1 
    With wrdDoc_Read 
     For p = 1 To .Paragraphs.Count 
      Set tRange = .Range(Start:=.Paragraphs(p).Range.Start, End:=.Paragraphs(p).Range.End) 
      tString = tRange.Text 
      tString = Left(tString, Len(tString) - 1) 

      If InStr(1, tString, "1") > 0 Then 
       If Mid(tString, 1, 4) = "date" Then 
        tRange.Text = "DATE" ' Write Text 
       End If 
      End If 
     Next p 
    End With 
    .SaveAs ("C:\Documents and Settings\Desktop\Word_File_read_write_2.docx") 
    wrdApp.Quit ' close the Word application 
    Set wrdDoc = Nothing 
    Set wrdApp = Nothing 

End Sub 

回答

0

幾件事情

一個

它總是建議使用Option Explicit

聲明該Word文檔作爲wrdDoc但使用wrdDoc_Read

更改線路

With wrdDoc_Read 

With wrdDoc 

下一頁您.SaveAs例程是With - End With之外,因此會給你錯誤

Ç

你直接退出Word應用程序,而不關閉word文檔。在.SaveAs之後發出wrdDoc.Close (False)總是很好,因爲有些情況下已安裝的加載項可以對文檔進行更改,退出word應用程序將再次提示.SaveAs

d

而不是通過細胞循環和更換的文字,你可以使用.FindReplace

相關問題