none
Run-time error 4248 when opening a Word document with a double click in Access

    Question

  • Hi,

    I'm working with Office 2007 and 2010.

    I have a Word template which performs some checking on the document (based upon it) when it is opened. In different points it uses the ActiveDcument propiety.

    When I open the document normally, everything OK

    When I open the document from Access using the following code, it is also OK:

       Dim objWordApp As Word.Application
       Dim objWordDoc As Word.Document
       
       Set objWordApp = CreateObject("Word.Application")
       With objWordApp
          .Visible = false ' or True
          .Documents.Open("MYDoc.docx")
       End With

    But when I double click the bound object frame (OLE) in an Access frame, Word will generate an error when it encounters the ActiveDocument reference.

    Run-time error '4248':
    This command is not available because no document is open.

    Could you explian me why? And suggest some work around?

    Lauro

    P.S.:

    I noticed that Application.UserControl is TRUE (in the two first cases; and False in the last) [strange!  I would have tought that in the second one would it be false...]


    • Edited by Lauro2 Thursday, August 16, 2012 10:08 PM
    • Moved by Leo_GaoModerator Wednesday, August 22, 2012 2:42 AM (From:Word for Developers)
    Thursday, August 16, 2012 10:06 PM

All replies

  • hi,

    I have an Access (2007 or 2010) database; in a form I have a bound object frame wich contains a Word (2007 or 2010) file.

    The Word documents are based on a MyTemplate, which uses ActiveDocuments on Create and Open events.

    When I open the word doc from a VBA function like the following:

       Set objWordApp = CreateObject("Word.Application")
       With objWordApp
          .Visible = False ' or False
          .Documents.Open strNameFile
       End With
    

    I have no problem.

    But when I open the word doc from my form (double click or left mouse,OLE,open) I get

    Run-time error '4248':
    This command is not available because no document is open.

    when the word code arrive at the ActiveDocument instruction.

    Why?

    Is it different the way I can open an OLE object by code and the way Access does it?

    Why when I open the doc by code the word Application.UserControl is TRUE and when I double click in the form is FALSE?

    Any tip?

    Lauro

    Saturday, August 18, 2012 11:25 AM
  • Hi Lauro

    It's been a long time since I've tried storing any Office documents in an Access database and opening them - mostly because trying to handle them has been a mess... Have you tried asking this in the Access forum? If not, it might be best to try there, first, as the Access specialists probably have more experience with the problem and may already know a solution.

    If you have, and they've told you to ask here, can you tell me how a normal document (one not attached to a template with code) opens when you double-click it? Does it open in Word inside of Access (as an embedded object)? Or does it start the Word application and open in that (in a Word window)?

    One thing you can try is to build a loop at the beginning of the code to check the Documents.Count property. As long as it's < 1 no document is open, so ActiveDocument won't be available...


    Cindy Meister, VSTO/Word MVP

    Sunday, August 19, 2012 10:17 AM
  • Hi Lauro,

    Welcome to the MSDN forum!

    Please check this kb to see if it helps:

    WD2000: Error Message: "Run-time error '4248'; 'This command is not available because no document is open.'"
    http://support.microsoft.com/kb/209160 

    Have a nice day.


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us


    Monday, August 20, 2012 3:14 AM
    Moderator
  • Hi,

    thanks for the link you suggested; but it really doesn't help me.

    Because when I open the Word file, either directly either by code from Access, every thing is OK; the problem arise only when ACCESS is opening it (and there is nothing I can do, then).

    I just wanted to understand the different way Access use to open a word file.

    Probably I have to modify my code at the opening event; or put that code at a different event (maybe activate); or I have to disable the standard double clicking way Access uses to open an OLE object and put a command buttan with my code to open the file.

    But why, when I open a file by code (the file is not yet opened ... it is opening...) the ActiveDocument istruction no problem arise, and when Access does the same thing I get an error?

    Thanks again, Lauro

    Monday, August 20, 2012 6:18 PM
  • Hi Cindy,

    I'm waiting for a satisfactoring answer from the Access forum; in the meanwhile I tried to reproduce in a simpler manner my problem.

    I charged the 4 simple files on skydrive.

    1. access database "database1", with a table and a form (3 fields: ID, path, word Doc OLE)
    2. a simple file "normal.docx"
    3. a file "with code.docx" based on the following template
    4. a template with this only code on ThisDocument
    Private Sub Document_Open() 
    Debug.Print ActiveDocument.Name 
    End Sub

    I the database I inserted the 2 files. (Right click on the doc OLE, Insert Object, create from file, browse, linked).

    When I double click on the file, it opens in a new word application.

    BUT. The file based on the template arise the infamous error

    Run-time error '4248':
    This command is not available because no document is open.

    Actually I also had problem to insert it the first time, but it does it.

    Any idea?

    Thanks, lauro

    Monday, August 20, 2012 8:38 PM
  • Hi,

    I tried to reproduce in a simpler manner my problem.

    I charged the 4 simple files on skydrive.

    1. an access database "database1", with a table and a form (3 fields: ID, path, word Doc OLE)
    2. a simple file "normal.docx"
    3. a file "with code.docx" based on the following template
    4. a template with this only code on ThisDocument
    Private Sub Document_Open() 
    Debug.Print ActiveDocument.Name 
    End Sub

    In the database I inserted the 2 files. (Right click on the doc OLE, Insert Object, create from file, browse, linked).

    When I double click on the file, it opens in a new word application.

    BUT. The file based on the template arise the infamous error:

    Run-time error '4248':
    This command is not available because no document is open.

    (Actually I also had problem to insert it the first time, but it does it.)

    Of course I have no problems to open the file "with code" directly or by code from Access.

    Any idea?

    Thanks, lauro

    Monday, August 20, 2012 8:52 PM
  • Hi lauro,

    Thanks for your clearly description on the issue and the sample you provided. It greatly help on understanding the problem. :)

    After trial and error, the Run-time error '4248' occurs when Macro Settings is set to "Enable all macros(not recommended; ..." in the Trust Center. (Word-> File->Options->Trust Center-> Trust Center Settings). 

    When we set the Macro Settings to other options, the word file opens as expected without any error.

    Also, when I set the VBA code in the template to  

    Private Sub Document_Open()
       Debug.Print Now
    End Sub
    

    The word "with code.docx" opens without any error too.

    So, in my humble opinion, when using the ActiveDocument.Name in the Open event, when open the document from Access Form, it seems the ActiveDocument hasn't got a value yet. 

    I will report the behavior internally to see if there is any workaround/solution. I will let you know if there is any progress.

    Have a good day.


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, August 21, 2012 6:41 AM
    Moderator
  • Hi,

    thanks for the interest in my problem.

    I reproduced the two scenarios you described, and I confirm that with a more "innocent" code (debug.print Now) there is no problem and also that disabling the trusted path, my previous setting, the two (innocent and not innocent codes) behave in the same manner (strange enough!).

    I'm waiting with confidence a solution/workaround of this problem.

    Lauro
    Wednesday, August 22, 2012 6:17 AM
  • Hi Lauro

    It seems that YoYo Jiang is going to be able to continue helping with this? It seems, at least, that the origin of the error message has been tracked down :-)

    <<Why when I open the doc by code the word Application.UserControl is TRUE and when I double click in the form is FALSE?>>

    Application.UserControl tells Office whether the user initialized an application, or another application. When you double-click something in the Access field, behind the scenes Access is taking care of starting Word up and loading the document. So, even if the user did the double-click, it doesn't look like that to Word.

    Actually, this is one of the few times I've encountered Word having this property set to False. Typically, as soon as the Word.Application is made visible, the UserControl = True - unlike Excel or Access. So this is interesting :-)

    Another aspect of this property is that, when the code that initiated loading the document goes out of process, it theoretically could end up closing the document and quitting the Word application. If you'd like to test this, you might try quitting Access while the document is open in Word and see whether that also closes the document...


    Cindy Meister, VSTO/Word MVP

    Wednesday, August 22, 2012 1:28 PM
  • Hi Lauro

    Concerning whether/when ActiveDocument becomes available:

    What happens if you do something like this:

      Dim nrDocs as Long
      Do While nrDocs < 1
        nrDocs = Documents.Count
      Loop
      ActiveDocument.... 'do something here

    One issue you'll definitely have with Word 2007/2010 is the trust issue. In order for macros to be able to run, the Trust Center settings have to be correct - and I mean more than just enabling macros to run. Starting with version 2007, things become much more complex than simply allowing macros to run!

    If you (your company) has a digital signature for signing code, it would make most sense to sign the template's VBA project. Then the IT department can make sure all the users are set up to trust that digital signature and the code should run more reliably.

    Another approach is to make sure the location where the template is saved is a "trusted location". Again, the IT department can make sure this is set for all users - and they will need to do that if the location is a network drive.


    Cindy Meister, VSTO/Word MVP

    Wednesday, August 22, 2012 1:34 PM
  • Hi Cindy, Hi Yoyo,

        thanks to both for your assistance...

    I've done some more tests. I found that:

    • with the following code I still get the error (Document.Count is 1 from the beginning of the procedure) code:

               Dim nrDocs as Long
               Do While nrDocs < 1
                   nrDocs = Documents.Count
               Loop
               Debug.print ActiveDocument.Name

    • If I simply put explicitly the application to true, like the following code, I get NO ERRORS (strange, isn't it?):

              Application.Visible = True
              Debug.Print ActiveDocument.Name

    • Using Debug.print Application.UserControl, I always get TRUE (doesn't matter if I launch the file normally or from my database (Access should do the job).

    Thanks, again, Lauro

    Friday, August 24, 2012 11:23 AM
  • Hi Yoyo,

    Any further updates on this issue?

    I'm also suffering from the similar behavior. When I try to open the document from my SharePoint Site, it throws this run-time error 4248. This document contains a Macro wherein the VBA Script fetches document metadata and update the header of the document accordingly. But as the ActiveDocument don't have any value that's why the header isn't getting updated after clicking "End" button of run-time dialog.

    Also the work-around scripts given on the kb articles does help me in suppressing the run-time error, but still the document header is not updated as the script execution was interrupted because of run-time error.

    I would highly appreciate if you can please guide me on how to resolve this issue?

    Best Regards,

    Vishwajit

    --------------------------------

    MCTS, MCPD - SharePoint 2010


    Vishwajit Walke

    Tuesday, November 27, 2012 9:19 AM