locked
Opening a file as administrator

    Question

  • Hi there,

    I am developing an Excel workbook at work and I've got a problem. I'd like to save information entered in the workbook and save it in another one (called List, for example purposes), using a macro.

    The problem is that the file in which I want to save the information is located on a server. When people open List.xls, it's Read Only. So, when my macro executes, it opens a Read Only workbook...

    I'd like to do "Open As", just like you could do by right-clicking on any file in Windows XP. The result would be that the macro would log on as administrator only to open List.xls, write in it and save it under the same name.

    In case it changes anything, we are using Office 2007 and Windows Vista.

    Sorry if I'm not clear. English is not my main language and I recently started programming, so...

    Thanks!
    Friday, August 21, 2009 4:06 PM

Answers

  • You can make your program open Excel file type (.xls) by registered your program with that extension(.xls), registered your program with particular extension, will affect all files with that extension.In your case, You have to let your program to accept List.xls file when windows explorer passed the file name as argument to your program and reject any excel file without the name "List.xls"

     You can try to google for "file associations in VBA"


    kaymaf
    I hope this helps, if that is what you want, just mark it as answer so that we can move on
    Friday, August 21, 2009 6:11 PM
  • Thank you All for your friendly help.

    Hi alepetit,

    Welcome to MSDN forums!

    Tutorial:  How to associate a File Extension with your application?

     

    All the file type-app associations are stored in the Registry. To create a file extension associated with your application, you need to write Registry to register it.

     

    For example: Creating such file extension as .Hello.

    My.Computer.Registry.ClassesRoot.CreateSubKey(".Hello").SetValue("", "Hello", Microsoft.Win32.RegistryValueKind.String)     My.Computer.Registry.ClassesRoot.CreateSubKey("Hello\shell\open\command").SetValue("", Application.ExecutablePath & " ""%l"" ", Microsoft.Win32.RegistryValueKind.String)

    Explanation to above code sample:

    Code

    What it does

    CreateSubKey(".Hello")

    Creates a registry key in ClassesRoot for the .Hello extension. Notice that you must include the beginning period.

    .SetValue("", "Hello"...

    1.     "" (Or Nothing) sets the default value of the key.

    2.     "Hello" is like the "txtfile" we saw earlier, it tells which registry key contains the information about the .Hello extension.

    CreateSubKey("Hello" & _ "\shell\open\command")

    This creates the "Hello" sub-key and the "store\open\command" subkey that is needed to store the path to the application that will open this file type.

    .SetValue("", Application.ExecutablePath & _ " ""%l"" ",...

    1.     Again, "" tells the application to set the key's default value.

    2.     Application.ExecutablePath tells the code to associate the currently running executable
    with this file type.

    3.     " ""%1"" " passes the opened file's location to your program. The quotes around it are
    optional, but if you have more than one argument, you must put them around each.

     

    Detailed walkthrough and demo project are here:

    http://www.codeproject.com/useritems/VBFileAssociation.asp



    Additionally, you can manually create file extension via Windows Explorer:

    Tools menu -> Folder Options -> File Types -> Create New Extension by specifying File Extension and Associated File Type.

    Related thread:

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/4d8760c9-b2c9-4016-b75e-1b3189b0c30b/

     

     

    For VBA (Visual Basic for Applications), Macros or Excel Questions, you will get better support in the VBA Forum:

    http://social.msdn.microsoft.com/forums/en-US/isvvba/threads/


    Best regards,
    Martin Xie


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback on our support, please contact msdnmg@microsoft.com
    • Marked as answer by alepetit Tuesday, September 01, 2009 3:23 PM
    Friday, August 28, 2009 6:42 AM

All replies

  • Hi there,

    I am developing an Excel workbook at work and I've got a problem. I'd like to save information entered in the workbook and save it in another one (called List, for example purposes), using a macro.

    The problem is that the file in which I want to save the information is located on a server. When people open List.xls, it's Read Only. So, when my macro executes, it opens a Read Only workbook...

    I'd like to do "Open As", just like you could do by right-clicking on any file in Windows XP. The result would be that the macro would log on as administrator only to open List.xls, write in it and save it under the same name.

    In case it changes anything, we are using Office 2007 and Windows Vista.

    Sorry if I'm not clear. English is not my main language and I recently started programming, so...

    Thanks!

    you have to change as read/write. its the way you open it. and you need to open your serv as well, conect to your save and save the information there....

    for VBA Excel try here:   http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/717e3fc3-8840-446d-8b7a-55a4052f48d6

    http://social.msdn.microsoft.com/forums/en-US/isvvba/threads/

    all the best for you
    DeepF1
    Friday, August 21, 2009 4:49 PM
  • You can make your program open Excel file type (.xls) by registered your program with that extension(.xls), registered your program with particular extension, will affect all files with that extension.In your case, You have to let your program to accept List.xls file when windows explorer passed the file name as argument to your program and reject any excel file without the name "List.xls"

     You can try to google for "file associations in VBA"


    kaymaf
    I hope this helps, if that is what you want, just mark it as answer so that we can move on
    Friday, August 21, 2009 6:11 PM
  • Thank you All for your friendly help.

    Hi alepetit,

    Welcome to MSDN forums!

    Tutorial:  How to associate a File Extension with your application?

     

    All the file type-app associations are stored in the Registry. To create a file extension associated with your application, you need to write Registry to register it.

     

    For example: Creating such file extension as .Hello.

    My.Computer.Registry.ClassesRoot.CreateSubKey(".Hello").SetValue("", "Hello", Microsoft.Win32.RegistryValueKind.String)     My.Computer.Registry.ClassesRoot.CreateSubKey("Hello\shell\open\command").SetValue("", Application.ExecutablePath & " ""%l"" ", Microsoft.Win32.RegistryValueKind.String)

    Explanation to above code sample:

    Code

    What it does

    CreateSubKey(".Hello")

    Creates a registry key in ClassesRoot for the .Hello extension. Notice that you must include the beginning period.

    .SetValue("", "Hello"...

    1.     "" (Or Nothing) sets the default value of the key.

    2.     "Hello" is like the "txtfile" we saw earlier, it tells which registry key contains the information about the .Hello extension.

    CreateSubKey("Hello" & _ "\shell\open\command")

    This creates the "Hello" sub-key and the "store\open\command" subkey that is needed to store the path to the application that will open this file type.

    .SetValue("", Application.ExecutablePath & _ " ""%l"" ",...

    1.     Again, "" tells the application to set the key's default value.

    2.     Application.ExecutablePath tells the code to associate the currently running executable
    with this file type.

    3.     " ""%1"" " passes the opened file's location to your program. The quotes around it are
    optional, but if you have more than one argument, you must put them around each.

     

    Detailed walkthrough and demo project are here:

    http://www.codeproject.com/useritems/VBFileAssociation.asp



    Additionally, you can manually create file extension via Windows Explorer:

    Tools menu -> Folder Options -> File Types -> Create New Extension by specifying File Extension and Associated File Type.

    Related thread:

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/4d8760c9-b2c9-4016-b75e-1b3189b0c30b/

     

     

    For VBA (Visual Basic for Applications), Macros or Excel Questions, you will get better support in the VBA Forum:

    http://social.msdn.microsoft.com/forums/en-US/isvvba/threads/


    Best regards,
    Martin Xie


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback on our support, please contact msdnmg@microsoft.com
    • Marked as answer by alepetit Tuesday, September 01, 2009 3:23 PM
    Friday, August 28, 2009 6:42 AM