Word 2013 VBA - opening document aborts macro

2019-09-10 07:34发布

问题:

I'm running pretty simple VBA in Word:

Sub Something

On Error GoTo prob

Dim d As Document

Set d = Documents.Open(FileName:="c:\ajb files\test.docx")

'this should run if macro succeeds
MsgBox "ok"
Exit Sub

'this should run if macro fails
prob:
MsgBox "prob"

End Sub

This macro opens the Word document then stops (without triggering an error message).

I tried running the same macro in Word 2010 and it works fine. I haven't got an Acer laptop, or any weird anti-virus or other software running. I removed all Word add-ins, and the problem still happens.

If I go straight into Word and run this macro, it works fine. The problem comes when I first run a macro in Access which tries to run this code through Word VBA, and seizes up on the line above. If I then try to run the macro without first closing Word, it hangs after opening the document.

回答1:

OK, I've solved the problem. The Access VBA macro was opening a Word template which wasn't in a trusted location. Somewhere along the line this was causing it to hang. I've added the Word template folder to my list of trusted locations and it seems to have solved the problem.



回答2:

Check out this thread: VBA code stops after Documents.Open

I have the exact same problem and I thought I found a fix today when I set the windows security for the folder holding the file allow anything and everything to edit the folder. (The location was already trusted in Word). It solved the problem in ONE location, but the exact same fix, plus making sure the location was trusted, did NOT work in two other places.

I am going to try rolling back my Word version per the instructions in the thread I linked.