Removing an internal link to Word templates through VBA

I am trying to create a small VB application that removes an internal link in Word Documents into my templates.

I found this guide http://word.tips.net/Pages/T001437_Batch_Template_Changes.html and I'm trying to change it to use with VBA instead of programming macros inside Office.

However, I was fixated on how to make Document.Open work. Any help is appreciated.

It is assumed that it runs as a standalone application, and not from Word. I am looking for a way to accomplish what Macro does, but not from Word.

+3
source share
2 answers

There is two bad news here.

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
+5

, . VBE Word [Alt F11], , "/" [ ] . , , "" "", .

+1

All Articles