1

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
  • I think that the parameter that let you access to `VBProject` is something that you need to manually check in Office parameters before you can use your code – R3uK Apr 24 '15 at 08:42
  • Can you please explain in detail, how can I check - I cant see anything in this line which is client specific? – ziemlichdunkel Apr 24 '15 at 09:30
  • The details are in my answer just below, and I added another clue that may help you in my comment in answer – R3uK Apr 24 '15 at 09:34

1 Answers1

1

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.

R3uK
  • 14,417
  • 7
  • 43
  • 77
  • As quoted in my original question, I already set „Trust access to the VBA project object model“ in Word before executing. Otherwise you will get an error anyway... The problem must be something else but I dont get any error and it's working on some clients and on some it dont.... – ziemlichdunkel Apr 24 '15 at 09:26
  • Have you check if the `Reference` to `Microsoft Scripting Runtime`? `ThisWorkbook.VBProject.References.AddFromFile xRef` in code or there for more details : http://stackoverflow.com/questions/3233203/how-do-i-use-filesystemobject-in-vba – R3uK Apr 24 '15 at 09:32
  • Thank you for the advise, didn't know that, but on my own computer where it works, I dont have the MS-Scripting Runtime activate. I only have active: Visual Basic for Applications Microsoft Word 14.0 Object Library OLE Automation Microsoft Office 14.0 library Microsoft Forms 2.0 object Library If some of these are needed, is it possible to load them via script? – ziemlichdunkel Apr 24 '15 at 11:02
  • Yes it is, this link should help you : http://stackoverflow.com/questions/9879825/how-to-add-a-reference-programmatically-vba-excel You just need to get GUID of the particulars references you want to load and add this in your `Workbook_Open` or `Document_Open` event! ;) – R3uK Apr 24 '15 at 11:29
  • Hm, I think it's not a problem of the loaded runtimes or references, have verified they are all the same on my test computers, but on some it works, others dont. But I found an new case in the Windows Application Logs. On Computers where it doesn't work, it seems the Word Application is crashing when trying to open the normal.dotm. – ziemlichdunkel Apr 27 '15 at 14:45
  • `Name der fehlerhaften Anwendung: WINWORD.EXE, Version: 14.0.7145.5001, Zeitstempel: 0x54e3da8e Name des fehlerhaften Moduls: ole32.dll, Version: 6.1.7601.17514, Zeitstempel: 0x4ce7b96f Ausnahmecode: 0xc0000005 Fehleroffset: 0x0003bc21 ID des fehlerhaften Prozesses: 0xd70 Startzeit der fehlerhaften Anwendung: 0x01d080f7b063a5d9 Pfad der fehlerhaften Anwendung: C:\Program Files (x86)\Microsoft Office\Office14\WINWORD.EXE Pfad des fehlerhaften Moduls: C:\Windows\syswow64\ole32.dll ` Is there something I can change to make the Word Opening more bulletproof, or any Idea why it is crashing? – ziemlichdunkel Apr 27 '15 at 14:49
  • You should edit your question to add that info that is pretty interesting for all. Not sure how you select the template with `strText`but you can convert your 2 lines statements in 1 : `Set doc = oApplication.Documents.Open strText` – R3uK Apr 27 '15 at 15:01
  • There is an `.OpenAsDocument` in `Template` class for Word, with a `.Save` that might be what you need. – R3uK Apr 27 '15 at 15:04
  • 1
    Great, it's working now on all clients, changed like you suggested: `Set oApplication = CreateObject("Word.Application") oApplication.NormalTemplate.OpenAsDocument` Many thanks for your great help!!! One question remains: Any Idea if it is possible to check before import if a Sub with same name already exists in Module? (If I execute it multiple times it always imports the sub, so I end up with a messed up normal.dotm) It's not a big deal, but the users should execute it themselfes and I want do make it Idiot proof ;-) – ziemlichdunkel Apr 29 '15 at 08:31
  • Already tried (but not working): `if oApplication.ActiveDocument.VBProject.VBComponents("ThisDocument").CodeModule.Name = "AutoOpen" then` – ziemlichdunkel Apr 29 '15 at 08:33
  • A bit strange... In Object Browser (F2), I don't find the property .Name for CodeModule, and yet for `Application.VBE.ActiveCodePane.CodeModule.Name` it does work, but it'll be for the active Module (you can check to be sure). You may try the `CodeModule.Find` which work like this : `object.Find(target, startline, startcol, endline, endcol [, wholeword] [, matchcase] [, patternsearch]) As Boolean` and look for the name of a Sub if I got what I read right. Can you upadte your code in the question? – R3uK Apr 29 '15 at 09:12
  • Couldn't get it to work with "Find" right now, time is limited so think I will export the existing module and parse the file, might be easier... – ziemlichdunkel Apr 29 '15 at 13:11
  • Full coding is to big to post it here because I added lots of checks and stuff, would require lots of single posts? – ziemlichdunkel Apr 29 '15 at 13:13
  • You can just add this brutaly in an answer too, so that it'll be a base for other looking at the same issue! ;) – R3uK Apr 30 '15 at 08:31