VBS/VBA 脚本的奇怪行为(仅在某些机器上工作,在其他机器上 Word 崩溃)
Strange behavior of VBS/VBA Script (working only on some machines, on others Word is crashing)
我的一个脚本有问题。
我将 Public-Sub 例程注入用户 Normal.dotm(进入 "ThisDocument")。
脚本工作正常,但只有一些客户。
(在它不起作用的客户端上,Word Process 正在崩溃,见下文......)
如果有经验的程序员可以看看我的代码,或者告诉我如何调试出现的问题,我会很高兴。
所有变量都已正确填写,客户端都是具有相同策略的 Win7 企业版。 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 & "\Path\path.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 & "\Import\FrNow.cls")
WScript.Echo "Installation Finished..."
doc.close wdDoNotSaveChanges
oApplication.Quit wdDoNotSaveChanges
https://support.microsoft.com/en-us/kb/282830
For any Automation client to be able to access the VBA object model
programmatically, the user running the code must explicitly grant
access. To turn on access, the user must follow these steps. Office
2003 and Office XP
Open the Office 2003 or Office XP application in question. On the
Tools menu, click Macro, and then click Security to open the Macro
Security dialog box.
On the Trusted Sources tab, click to select the Trust access to Visual
Basic Project check box to turn on access.
Click OK to apply the setting.
You may need to restart the application for the code to run properly
if you automate from a Component Object Model (COM) add-in or
template. Office 2007
Open the 2007 Microsoft Office system application in question.
Click the Microsoft Office button, and then click Application Options.
Click the Trust Center tab, and then click Trust Center Settings.
Click the Macro Settings tab, click to select the Trust access to the
VBA project object model check box, and then click OK. Click OK.
我的一个脚本有问题。 我将 Public-Sub 例程注入用户 Normal.dotm(进入 "ThisDocument")。 脚本工作正常,但只有一些客户。 (在它不起作用的客户端上,Word Process 正在崩溃,见下文......) 如果有经验的程序员可以看看我的代码,或者告诉我如何调试出现的问题,我会很高兴。 所有变量都已正确填写,客户端都是具有相同策略的 Win7 企业版。 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 & "\Path\path.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 & "\Import\FrNow.cls")
WScript.Echo "Installation Finished..."
doc.close wdDoNotSaveChanges
oApplication.Quit wdDoNotSaveChanges
https://support.microsoft.com/en-us/kb/282830
For any Automation client to be able to access the VBA object model programmatically, the user running the code must explicitly grant access. To turn on access, the user must follow these steps. Office 2003 and Office XP
Open the Office 2003 or Office XP application in question. On the Tools menu, click Macro, and then click Security to open the Macro Security dialog box.
On the Trusted Sources tab, click to select the Trust access to Visual Basic Project check box to turn on access.
Click OK to apply the setting.
You may need to restart the application for the code to run properly if you automate from a Component Object Model (COM) add-in or template. Office 2007
Open the 2007 Microsoft Office system application in question.
Click the Microsoft Office button, and then click Application Options.
Click the Trust Center tab, and then click Trust Center Settings.
Click the Macro Settings tab, click to select the Trust access to the VBA project object model check box, and then click OK. Click OK.