VBS/VBA脚本的奇怪行为(仅在某些机器上工作,在其他机器上Word正在崩溃)



我的一个脚本有问题。我将一个公共子程序注入用户Normal.dotm(注入"ThisDocument")。脚本运行良好,但只有一个客户端。(在不起作用的客户端上,Word Process正在崩溃,请参阅下文…)如果一个有经验的程序员能看看我的代码,或者告诉我如何调试出了问题,那我就太好了。所有变量都已正确填充,并且客户端都是具有相同策略的Win7 Enterprise。每台机器上的normal.dotm也在同一位置。•在所有客户端上都设置了"对VBA工程对象模型的信任访问"。

有什么想法吗?

以下是完整的编码:

' 22.04.2015 
' Imports the CRM WordMerge Function into the Normal.dot

' Homefolder als Variable bereitstellen
Set oShell = CreateObject("WScript.Shell")
strHomeFolder = oShell.ExpandEnvironmentStrings("%APPDATA%")
' wscript.echo strHomeFolder
' Auslesen des aktuellen Verzeichnisspfades
scriptdir = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
' WScript.Echo scriptdir
' Get current User
Set objNetwork = CreateObject("Wscript.Network")
' Backup der Originalen Normal.dotm
dim filesys
set filesys=CreateObject("Scripting.FileSystemObject")
' Existierendes Backup löschen
If filesys.FolderExists(scriptdir & "Backup" & objNetwork.UserName) Then
filesys.DeleteFolder scriptdir & "Backup" & objNetwork.UserName
end if
' File mit Pfad zur normal.dotm einlesen:
strdatei=scriptdir & "Pathpath.txt"
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTest = objFSO.GetFile(strdatei)
If objTest.Size > 0 Then
Set objFile = objFSO.OpenTextFile(strdatei, ForReading)
strText = objFile.ReadAll
strText = oShell.ExpandEnvironmentStrings(strText)
'wscript.echo strText
objFile.Close
end if 


If filesys.FileExists(strText) Then
' MsgBox(scriptdir & "Backup" & objNetwork.UserName)
filesys.CreateFolder scriptdir & "Backup" & objNetwork.UserName
filesys.CopyFile strText, scriptdir & "Backup" & objNetwork.UserName & ""
end if

const wdDoNotSaveChanges = 0
WScript.Echo "Installing FrNow Macro for Word..."
' Dim oApplication, doc
Dim oApplication, doc
Set oApplication = CreateObject("Word.Application")
' WScript.Echo "Opening Normal.dot Template..."
oApplication.Documents.Open strText
Set doc = oApplication.ActiveDocument
Dim comp, components
Set components = oApplication.ActiveDocument.VBProject.VBComponents

' Importiere CRM Makro
oApplication.ActiveDocument.VBProject.VBComponents("ThisDocument").CodeModule.AddFromFile(scriptdir & "ImportFrNow.cls")
WScript.Echo "Installation Finished..."
doc.close wdDoNotSaveChanges
oApplication.Quit wdDoNotSaveChanges

https://support.microsoft.com/en-us/kb/282830

任何Automation客户端都可以访问VBA对象模型以编程方式,运行代码的用户必须显式授予通道若要打开访问,用户必须执行以下步骤。办公室2003和Office XP

打开有问题的Office 2003或Office XP应用程序。关于"工具"菜单,单击"宏",然后单击"安全"打开"宏"安全性"对话框。

在"受信任的源"选项卡上,单击以选择对Visual选中"基本项目"复选框以打开访问。

单击"确定"应用设置。

您可能需要重新启动应用程序才能使代码正常运行如果从组件对象模型(COM)加载项或样板Office 2007

打开有问题的2007 Microsoft Office system应用程序

单击Microsoft Office按钮,然后单击"应用程序选项"。

单击信任中心选项卡,然后单击信任中心设置。

单击"宏设置"选项卡,单击以选择对"VBA工程对象模型"复选框,然后单击"确定"。单击"确定。">

最新更新