web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

How can I check with the EXCEL file has been opened / access in AX2012

(0) ShareShare
ReportReport
Posted on by

I am writing the code to export the data to excel. When the excel file has been opened or accessed, meaningful error should occurred. I would like to know what kind of coding can be write to check with the excel file has been opened or accessed. Please advise as I am a developer in AX2012 for a few months. Thank you very much.

*This post is locked for comments

I have the same question (0)
  • André Arnaud de Calavon Profile Picture
    301,146 Super User 2025 Season 2 on at

    Hi Satoyuichi,

    Can you elaborate? Do you want to check whether a person did open the file in the (recent) past or if it is open at time of executing your coding?

    Can you explain your scenario with some examples?

  • Community Member Profile Picture
    on at

    The scenario should be when I run my program, before the data has been exported and the file has been generated. Please check the generated file has been opened or accessed by someone or not.

    If the excel file has been generated and it has been closed, the file will still be saved.

    If the excel file has been generated and the file has been opened or accessed by someone or myself, error should be throw out. Excel will cannot be generated and save with the corresponding file name. Only one file name can be saved. So the check whether the corresponding file has been opened or accessed must included. The pop up error may be "Excel file has been opened or accessed. Please close the file".

    In this case, I found the example in the internet. But no similar sample case has been shown. Please advise what can I do.

  • Community Member Profile Picture
    on at

    Reason to have this kind of code since I would like to check whether the generated excel file has been opened or accessed before the generated excel file has been saved. If the generated excel file has been opened or accessed, error should be throw out. Please advise which kind of excel method or code should be included. Thank you.

  • Martin Dráb Profile Picture
    237,976 Most Valuable Professional on at

    Why? Why don't you simply generate the file in the background and give it to users only when it's been generated in full?

  • André Arnaud de Calavon Profile Picture
    301,146 Super User 2025 Season 2 on at

    Like Martin said, you need to generate the file in the background. This is what all developers logically should do. If it is running visible for the user there is no check if it has been modified or not.

  • Community Member Profile Picture
    on at

    My problem is if the excel file has been generated into the corresponding location, and the user still generated  the excel program. Error will occurred when the corresponding excel file has been opened and locked by someone.

    My program want to handle the file locked part. Should I use WinApiServer::fileLocked(filename)?

    My logic

    filename = "C:\abc.xlsx"

    if (WinApiServer::fileLocked(filename))

    {

           throw error("The " + filename + " has been accessed. Please close the file and try again.");

    }

    else

    {

          code in here is to write the data into excel

    }

  • Community Member Profile Picture
    on at

    The error here is

    Request for the permission of type 'FileIOPermission' failed.

    (S)\Classes\FileIOPermission\demand

    (S)\Classes\WinAPIServer\fileExists - line 14

    (S)\Classes\WinAPIServer\fileLocked - line 21

    and the line 14 of Classes\WinAPIServer\fileExists is

    fileIOPerm.demand();

    how can I handle it? pls advise

  • Martin Dráb Profile Picture
    237,976 Most Valuable Professional on at

    I'm sorry, but I don't understand the description of your problem ("if the excel file has been generated into the corresponding location, and the user still generated  the excel program"). Can you reformulate it, please?

    And can you please explain why you can't use the solution suggested by us?

  • Community Member Profile Picture
    on at

    Full Explaination

    ******************

    User A : use MO number (ABC123) generated the excel file into the corresponding location and open the excel file (filename: ABC123)

    User B : hasn't check the corresponding location and use the same record (MO number: ABC123) to generate the same excel file in the same location with the same filename (filename: ABC123)

    P.S. : The excel filename has been set by the user with the MO no.. No other characters or time can be included in the filename.

    Program Logic

    [1]Check whether the file locked by other user

    if Yes >> Message "File Locked"

    if No >> Write / Replace the file.

    if your suggestion is suitable in my case, please explain how to "generate the file in the background and give it to users only when it's been generated in full?" in my case. I am so sorry that I don't understand how to use your solution in my case. So sorry.

  • Martin Dráb Profile Picture
    237,976 Most Valuable Professional on at

    Is your intention to allow both users to generate a file and work with it at the same time? If so, use different locations, such as put it into each user's documents folder, or to a temporary folder with a unique name.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans