2011-03-15 46 views
3

我試圖創建一個小的VB應用程序,它將Word文檔中的內部鏈接刪除到他們的模板中。通過VBA刪除Word模板的內部鏈接

我發現本指南 http://word.tips.net/Pages/T001437_Batch_Template_Changes.html 我正在嘗試修改它,以便在Office中使用VBA代替宏編程。

但是,我遇到了如何讓Document.Open工作。任何幫助表示讚賞。

這應該是作爲一個獨立的應用程序運行,而不是從Word中肆意。 我正在尋找一種方法來執行宏所做的事情,但不是從Word中執行的。

+0

對不起,我對VBA /宏的引用以及'visual-studio-2010'和'vba'標籤有點困惑。 VBA是Office內部使用的宏語言。你是否試圖通過VS或VSTO中的Interop來做到這一點(這不會是一個VBA應用程序,這將是一個VS應用程序)? 「Document.Open」的哪一部分讓你起牀?你是否只是試圖將它放在'Document.Open'事件接收器中而引用了代碼? – 2011-03-30 17:26:46

+0

我已經更改了你的標籤以符合你的問題,但如果我弄錯了,請更改它們並澄清你的問題。請詳細說明Document.Open的情況。 – 2011-03-31 19:49:57

+0

Hej ...你是否在你提到的頁面上尋找第一或第二宏的幫助? – MikeD 2011-03-31 20:39:08

回答

5

這裏有兩件壞消息。

1)文件必須有一個模板。你不能刪除它,只能將它改成別的東西。

2)改變一個模板不會做任何事情。見this page

我在想,如果Open方法的問題是您試圖打開「.doc」擴展名文件,而不是現代的「.docx」擴展名文件。你鏈接的VBA子程序只做「.doc」文件。此VBA代碼確實有兩個:

Function StringEndsWith(_ 
    ByVal strValue As String, _ 
    CheckFor As String) As Boolean 

    Dim sCompare As String 
    Dim lLen As Long 

    lLen = Len(CheckFor) 
    If lLen > Len(strValue) Then Exit Function 
    sCompare = Right(strValue, lLen) 
    StringEndsWith = StrComp(sCompare, CheckFor, vbTextCompare) = 0 
End Function 


Sub ChangeTemplates() 
    Dim strDocPath As String 
    Dim strTemplateB As String 
    Dim strCurDoc As String 
    Dim docCurDoc As Document 

    ' set document folder path and template strings 
    strDocPath = "C:\tmp\" 

    ' get first doc - only time need to provide file spec 
    strCurDoc = Dir(strDocPath & "*.doc*") 

    ' ready to loop (for as long as file found) 
    Do While strCurDoc <> "" 
     If (StringEndsWith(strCurDoc, ".doc") Or StringEndsWith(strCurDoc, ".docx")) Then 
      ' open file 
      Set docCurDoc = Documents.Open(FileName:=strDocPath & strCurDoc) 
      ' change the template back to Normal 
      docCurDoc.AttachedTemplate = "" 
      ' save and close 
      docCurDoc.Close wdSaveChanges 
     End If 
     ' get next file name 
     strCurDoc = Dir 
    Loop 
    MsgBox "Finished" 
End Sub 
+4

我們最近將我們的模板遷移到了不同​​名稱的服務器,並且一些文檔變得非常緩慢,因爲Word試圖訪問那些不在那裏的模板。此過程確定了這些文檔的啓動時間較慢。所以改變一個模板確實能做些事情...... – Bobort 2011-06-24 14:55:20

1

答案之間的時間很長,但可能對其他人有用。如果您可以訪問Word文檔的VBE [Alt F11],並且想要刪除該引用,請轉到「工具/參考」[頂層菜單],並從參考文件列表中取消選擇它。我有一個類似的問題,模板不再存在,但它仍然在項目窗口中被「引用」,所以我做了上述操作。

相關問題