【问题标题】:AutoUpdate VBA startup macro?AutoUpdate VBA 启动宏?
【发布时间】:2011-10-07 09:38:42
【问题描述】:

我正在构建一些必须放在 %APPDATA%\Microsoft\Word\Startup 文件夹中的 Word 2003 宏。

我无法更改此文件夹的位置(更改为网络共享)。如何自动更新此宏?

我尝试创建一个引导程序宏,其中包含一个 AutoExec 子程序,可将较新版本从文件共享复制到此文件夹。但是当 Word 锁定文件时,我得到一个拒绝异常。

有什么想法吗?

仅供参考,我编写了这段代码。该代码适用于 templates 目录中的更新模板,但不适用于 startup 目录:

' Bootstrapper module
Option Explicit

Sub AutoExec()
    Update
End Sub

Sub Update()

    MirrorDirectory MyPath.MyAppTemplatesPath, MyPath.WordTemplatesPath
    MirrorDirectory MyPath.MyAppStartupTemplatesPath, MyPath.WordTemplatesStartupPath

End Sub

' IOUtilities Module
Option Explicit

Dim fso As New Scripting.FileSystemObject

Public Sub MirrorDirectory(sourceDir As String, targetDir As String)
    Dim result As FoundFiles
    Dim s As Variant

    sourceDir = RemoveTrailingBackslash(sourceDir)
    targetDir = RemoveTrailingBackslash(targetDir)



    With Application.FileSearch
        .NewSearch
        .FileType = MsoFileType.msoFileTypeAllFiles
        .LookIn = sourceDir
        .SearchSubFolders = True
        .Execute
        Set result = .FoundFiles
    End With

    For Each s In result

        Dim relativePath As String
        relativePath = Mid(s, Len(sourceDir) + 1)

        Dim targetPath As String
        targetPath = targetDir + relativePath

        CopyIfNewer CStr(s), targetPath

    Next s

End Sub

Public Function RemoveTrailingBackslash(s As String)
    If Right(s, 1) = "\" Then
        RemoveTrailingBackslash = Left(s, Len(s) - 1)
    Else
        RemoveTrailingBackslash = s
    End If
End Function

Public Sub CopyIfNewer(source As String, target As String)
    Dim shouldCopy As Boolean
    shouldCopy = False
    If Not fso.FileExists(target) Then
        shouldCopy = True
    ElseIf FileDateTime(source) > FileDateTime(target) Then
        shouldCopy = True
    End If

    If (shouldCopy) Then
        If Not fso.FolderExists(fso.GetParentFolderName(target)) Then fso.CreateFolder (fso.GetParentFolderName(target))
        fso.CopyFile source, target, True
        Debug.Print "File copied : " + source + " to " + target
    Else
        Debug.Print "File not copied : " + source + " to " + target
    End If
End Sub

' MyPath module

Property Get WordTemplatesStartupPath()
    WordTemplatesStartupPath = "Path To Application Data\Microsoft\Word\STARTUP"
End Property

Property Get WordTemplatesPath()
    WordTemplatesPath = "Path To Application Data\Microsoft\Templates\Myapp\"
End Property


Property Get MyAppTemplatesPath()
    MyAppTemplatesPath = "p:\MyShare\templates"
End Property

Property Get XRefStartupTemplatesPath()
    MyAppStartupTemplatesPath = "p:\MyShare\startup"
End Property

[编辑] 我探索了另一种方式

我正在考虑的另一种方式是引导组织者:

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 10/7/2011 by beauge
'
Application.OrganizerCopy source:="P:\MyShare\Startup\myapp_bootstrapper.dot", _
     Destination:= _
    "PathToApplication Data\Microsoft\Word\STARTUP\myapp_bootstrapper.dot" _
    , Name:="MyModule", Object:=wdOrganizerObjectProjectItems
End Sub

这是可行的,但有局限性:

  • 要么我必须对模块进行硬编码才能组织起来
  • 或者我必须更改“信任 VBA 项目”选项以自动发现这样的项目(这是不可接受的,因为它需要降低站点的安全性):

项目枚举的代码是这样的:

Public Sub EnumProjectItem()
    Dim sourceProject As Document
    Dim targetProject As Document
    Set sourceProject = Application.Documents.Open("P:\MyShare\Startup\myapp_bootstrapper.dot", , , , , , , , , wdOpenFormatTemplate)
    Set targetProject = Application.Documents.Open("PathToApplication Data\Microsoft\Word\STARTUP\myapp_bootstrapper.dot", , , , , , , , , wdOpenFormatTemplate)
    Dim vbc As VBcomponent
    For Each vbc In sourceProject.VBProject.VBComponents 'crash here
        Application.ActiveDocument.Range.InsertAfter (vbc.Name + " / " + vbc.Type)
        Application.ActiveDocument.Paragraphs.Add

    Next vbc

End Sub

[编辑 2] 又一次不成功的尝试:

我在我的网络共享中放置了一个包含所有逻辑的 .dot。

在我的STARTUP 文件夹中,我放置了一个简单的.Dot 文件,它引用了前一个文件,并带有一个“Call MyApp.MySub”。

这实际上是可行的,但是由于目标模板不在受信任的位置,因此每次启动 word 时都会弹出安全警告(即使与当前应用程序宏无关)

【问题讨论】:

    标签: vba auto-update


    【解决方案1】:

    至少,我使用这些步骤部分成功了:

    1. 创建安装包。我使用 NSIS 脚本
      • 包检测到 Winword.exe 的任何实例,并要求用户在 word 关闭时重试
      • 从注册表中提取单词的选项路径
      • 将文件部署到 word 的启动文件夹中
      • 在本地用户添加/删除程序中添加卸载程序
    2. 我将包放在远程共享中。我还添加了一个包含最新版本软件包的 .ini 文件(格式为“1.0”)
    3. 在宏本身中,我有一个版本号(例如“0.9”)。
    4. 在启动时(AutoExec 宏),我比较本地版本和远程版本
    5. 如果找到更新的版本,我会使用 shell exec 启动设置。
    6. 安装程序将等待 Word 关闭

    有点棘手,但它适用于 Word 2K3 和 Word 2K10。

    【讨论】:

      猜你喜欢
      • 2016-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-14
      • 2014-12-21
      • 1970-01-01
      • 2015-07-26
      • 1970-01-01
      相关资源
      最近更新 更多