通过VBA删除Word模板的内部链接

Gnu*_*utt 3 vba ms-word

我正在尝试创建一个小型VB应用程序,将Word文档中的内部链接删除到其模板中.

我找到了这个指南 http://word.tips.net/Pages/T001437_Batch_Template_Changes.html ,我正在尝试修改它,以便在Office中使用VBA而不是宏编程.

但是,我不知道如何让Document.Open工作.任何帮助表示赞赏.

这应该作为一个独立的应用程序运行,而不是在Word中运行.我正在寻找一种方法来执行宏所做的事情,但不是从Word中执行.

Sim*_* G. 5

这里有两条坏消息.

1)文档必须有一个模板.您无法将其删除,只能将其更改为其他内容.

2)无论如何,更改模板都无效.看到这个页面.

我很想知道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
Run Code Online (Sandbox Code Playgroud)

  • 我们最近将模板迁移到了一个名称不同的服务器,当Word试图访问那些不存在的模板时,一些文档的打开速度非常慢.此过程修复了这些文档的慢启动时间.所以更改模板确实有所作为...... (5认同)