ziemlichdunkel
ziemlichdunkel

Reputation: 35

Strange behavior of VBS/VBA Script (working only on some machines, on others Word is crashing)

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

Upvotes: 1

Views: 1086

Answers (1)

R3uK
R3uK

Reputation: 14547

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.

Upvotes: 1

Related Questions