I have a problem with one of my scripts. I Inject a Public-Sub Routine into the users Normal.dotm (into "ThisDocument"). The Script is working fine, but only one some clients. (On the clients where it doesn't work, the Word Process is crashing, see below...) I would be nice if an experienced programmer can have a look at my coding, or tell me how to debug what's going wrong. All variables are correctly filled and the clients are all Win7 Enterprise with same Policys. The normal.dotm is also in the same place on every machine. • „Trust access to the VBA project object model“ is set on all clients.
Any Ideas?
Here's the full coding:
' 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