2011-06-29 63 views
0

我正在使用Access將數據發送到我在Word中創建的模板。在它成功發送我需要使打開的Word文檔不可編輯的數據之後。Word自動化:不可編輯

另外,我注意到在完成文檔後,它會提示保存。是否可以刪除此提示,但允許保存功能。

這是我用來做字自動化代碼:

' Create a Word document from template. 
Dim WordApp As Word.Application 
Dim strTemplateLocation As String 
Dim myVariable As String 
myVariable = 「TEST!!」 

' Specify location of template 
strTemplateLocation = Left(CurrentDb.Name, InStrRev(CurrentDb.Name, "\")) & "test.dot" 

Set WordApp = CreateObject("Word.Application") 

WordApp.Visible = True 
WordApp.WindowState = wdWindowStateMaximize 
WordApp.Documents.Add Template:=strTemplateLocation, NewTemplate:=False 


' Replace each bookmark with field contents. 
WordApp.Selection.GoTo what:=wdGoToBookmark, Name:="myBookmark" 
WordApp.Selection.TypeText myVariable 

DoEvents 
WordApp.Activate 
Set WordApp = Nothing 
+0

如果您顯示實際的代碼,提出建議會容易得多。否則,我們都必須猜測你目前是如何做到的。 –

+0

這是否甚至編譯?看着Dim myVariable =「TEST !!」... –

+0

這是一個例子。固定。 – Rick

回答

0

這應該做你所需要的。從Excel,不Access測試,所以你需要修復的模板路徑

Sub Tester() 
    ' Create a Word document from template. 
    Dim WordApp As Word.Application 
    Dim strTemplateLocation As String 
    Dim myVariable As String 

    myVariable = "TEST!!" 
    ' Specify location of template 
    strTemplateLocation = ThisWorkbook.Path & "\test.dotx" 
    Set WordApp = CreateObject("Word.Application") 
    With WordApp 
     .Visible = True 
     .WindowState = wdWindowStateMaximize 
     .Documents.Add Template:=strTemplateLocation, NewTemplate:=False 

     ' Replace each bookmark with field contents. 
     .Selection.GoTo what:=wdGoToBookmark, Name:="myBookmark" 
     .Selection.TypeText myVariable 

     DoEvents 

     With .ActiveDocument 
      .Protect Type:=wdAllowOnlyReading, Password:="blah" 
      .Saved = True 
     End With 

     .Activate 
    End With 

    Set WordApp = Nothing 

End Sub 
+0

嘗試了WordApp.DisplayAlerts = wdAlertsNone,但它似乎沒有工作。真的不想通過自動化來保存它。我需要它打開,不允許編輯。保存消息出現在關閉Word文檔時。無論如何找不到ReadOnlyRecommended參數。 – Rick

+0

在Access中嘗試了WordApp.ActiveWindow.Document.ReadOnly,並獲取錯誤消息:「屬性的無效使用」。 – Rick

+0

@Rick:這是因爲'ReadOnly'屬性(具有諷刺意味)是隻讀的。你不能設置它,你只能檢查它的狀態。你知道VBA有一個幫助文件嗎?我剛剛通過按下F1瞭解了這一切。 –

1

Document對象有一個Saved財產,如果作出任何改變通常變爲False。如果將此屬性設置爲True,則在關閉文檔時不會提示您保存文檔,但如果您願意,仍可以手動將其保存(通過SaveSave As...)。

您可以使用Document對象的Protect方法來限制用戶可以進行的更改。例如,您可以使用參數wdAllowOnlyReading來調用它,這意味着不能進行任何類型的更改。您可能還需要查看保護文檔的密碼,以防止用戶再次簡單取消保護

+0

這是在訪問端還是Word?你能提供完整的陳述嗎?謝謝。 – Rick

+0

嘗試訪問「WordApp.ActiveWindow.Document.Protect」中的語句,但獲取參數不是可選的錯誤消息 – Rick

+1

@Rick:嗯...這是因爲'保護'有一個參數是不可選的,你沒有給出論證任何價值。如果您在'.Protect'之後點擊空格鍵,則智能感知應顯示此內容。或者只是閱讀Word的VBA幫助'保護'。這裏都有解釋。 –