+ Reply to Thread
Results 1 to 17 of 17

Rename/Relocate ThisWorkbook while open. Can't get to work with AddIns

  1. #1
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Rename/Relocate ThisWorkbook while open. Can't get to work with AddIns

    I'm trying to work out how to relocate an installed Add-In.

    I have worked out how to relocate ThisWorkbook on-the-fly (see code below - it's messy but it works) but only when it is not an Add-In.

    When I alter this code for an Add-In, it only creates a COPY of the add-in at the new location.

    It errors on the
    Please Login or Register  to view this content.
    (because Debug.Print ThisWorkbook.FullName shows that ThisWorkbook is still set to the ORIGINAL location not the NEW location)

    Any ideas on how this can be achieved?

    I'm open to alternative solutions (e.g. load & install the copied add-in and then uninstall/unload/file kill the original add-in)



    Please Login or Register  to view this content.
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

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

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Rename/Relocate ThisWorkbook while open. Can't get to work with AddIns

    Change ThisWorkbook to ActiveWorkbook
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Rename/Relocate ThisWorkbook while open. Can't get to work with AddIns

    Quote Originally Posted by AlphaFrog View Post
    Change ThisWorkbook to ActiveWorkbook
    Use ActiveWorkbook instead? I want to alter this code to work on an Add-In.


    Tested and it doesn't work.

    When I run the code on a Workbook, the .SaveAs causes ThisWorkbook to MOVE to the new location (While leaving a copy at the original location - which can then be deleted with Kill)

    When I alter the code so it can be run on an Add-In, the .SaveAs causes the ThisWorkbook to COPY to the new location. ThisWorkbook is still set to the original file - which causes an error message on the Kill line.


    The difference is subtle but critical.

    The easiest way to explain/prove this difference is to add this code just after the SaveAs line
    Please Login or Register  to view this content.
    The Workbook will return the new location/filepath. The Add-In will return the original location/filepath.

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Rename/Relocate ThisWorkbook while open. Can't get to work with AddIns

    I misunderstood what you were doing. Sorry. I understand now I think, but I don't have a solution.

  5. #5
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Rename/Relocate ThisWorkbook while open. Can't get to work with AddIns

    You want an add-in to move itself while loaded? I'm not going to ask why.

    Have you tried setting its Isaddin property to false?
    Remember what the dormouse said
    Feed your head

  6. #6
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Rename/Relocate ThisWorkbook while open. Can't get to work with AddIns

    Quote Originally Posted by romperstomper View Post
    You want an add-in to move itself while loaded?
    You got it

    Quote Originally Posted by romperstomper View Post
    I'm not going to ask why.
    Heh.

    It's no secret so I'll tell you. I like to see add-ins loaded from the Application.UserLibraryPath. I can't prevent users from installing them from another location. So what I want to do is have the Add-In check it's TWB path on open. If the path is different, it will background move itself to the UserLibraryPath. Ideally I want to achieve this with no interruption to the end user (i.e. the add-in should continue running, the user doesn't need to close & reopen Excel etc.)

    Quote Originally Posted by romperstomper View Post
    Have you tried setting its Isaddin property to false?
    No I hadn't. That sounds promising. I will try it and let you know.

  7. #7
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Rename/Relocate ThisWorkbook while open. Can't get to work with AddIns

    @Rory: Tried IsAddin = False. I thought it would work but it didn't.

    I'm puzzled as to why the XLSM version never has this problem? Unless it has something to do with the XLAM being installed?
    (I don't suppose there is any way possible to continue running code in a XLAM after Install=False?)

    FYI I have my AddIn version of the code below. To Anyone testing this - this will error on the Kill line so you will need to call the Disable_SpeedHack_Full after you have finished testing.

    Please Login or Register  to view this content.
    Last edited by mc84excel; 05-01-2014 at 06:50 PM.

  8. #8
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Rename/Relocate ThisWorkbook while open. Can't get to work with AddIns

    It looks like you reset objWorkbook.IsAddin = True before killing. Try killing while it's set to False. Then set it to True after.

    I'm curious to see if you can get this to work.

  9. #9
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Rename/Relocate ThisWorkbook while open. Can't get to work with AddIns

    Quote Originally Posted by AlphaFrog View Post
    It looks like you reset objWorkbook.IsAddin = True before killing. Try killing while it's set to False. Then set it to True after.
    Interesting. I didn't think it would matter when I wrote this. Because after the saveas either the new location or the old location is set as ThisWorkbook. If the new location then it won't make any difference. If the old location then we still have the same problem.

    I tried it and it didn't work. Errored on Kill. Debug.Print ThisWorkbook.FullName shows that the open XLAM is still set in the old location.

    Quote Originally Posted by AlphaFrog View Post
    I'm curious to see if you can get this to work.
    Thanks Maybe I shouldn't be so restrictive on where the add-ins are installed. But I'm curious as to why the XLSM version will work and the XLAM doesn't.

  10. #10
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Rename/Relocate ThisWorkbook while open. Can't get to work with AddIns

    I'm now trying to install the copy and then delete the original file. But I can't get the copy to install. I run into a 1004 error on the Installed line.

    Please Login or Register  to view this content.

  11. #11
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Rename/Relocate ThisWorkbook while open. Can't get to work with AddIns

    With an add-in using SaveAs creates a copy of the workbook but leaves the original one open and running.

    I didn't have a problem installing the copy and then deleting and uninstalling the current version - you have to use the current workbook's ChangeFileAccess method before you can delete it though.

  12. #12
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Rename/Relocate ThisWorkbook while open. Can't get to work with AddIns

    Quote Originally Posted by romperstomper View Post
    I didn't have a problem installing the copy and then deleting and uninstalling the current version
    You didn't?! Which code were you using?! In my latest version (post #10) I've been getting 1004 error on the installed line.

    Quote Originally Posted by romperstomper View Post
    you have to use the current workbook's ChangeFileAccess method before you can delete it though.
    Never used it before. I'll look into it and let you know.

  13. #13
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Rename/Relocate ThisWorkbook while open. Can't get to work with AddIns

    I was using post #10. Perhaps you need to post the add-in you were testing with.

  14. #14
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Rename/Relocate ThisWorkbook while open. Can't get to work with AddIns

    Quote Originally Posted by romperstomper View Post
    I was using post #10. Perhaps you need to post the add-in you were testing with.
    Finally got round to testing this (I'm distracted by the WMA & C++ projects that I'm slowly sinking in) and I get an error message on the Add of the copy?

    I'm testing this by opening a workbook, opening add-ins manager and then using browse to install the xlam. (The AddIn install event calls the post10 code).
    Attached Files Attached Files

  15. #15
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Rename/Relocate ThisWorkbook while open. Can't get to work with AddIns

    You're missing a path in the Add line:
    Please Login or Register  to view this content.
    also your ChangeFileAccess is wrong:
    Please Login or Register  to view this content.
    You need the open copy to be read only so you can delete the original.

  16. #16
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Rename/Relocate ThisWorkbook while open. Can't get to work with AddIns

    Quote Originally Posted by romperstomper View Post
    You need the open copy to be read only so you can delete the original.
    But the workbook you are setting as readonly is the original file, not the copy?!

    I tried the code and it does work (albeit with some unusual side effects). I couldn't have got this far without your help. Thanks Rory +1
    Last edited by mc84excel; 05-13-2014 at 08:53 PM.

  17. #17
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Rename/Relocate ThisWorkbook while open. Can't get to work with AddIns

    Side effects:
    1. The original XLAM still appears as an open WB in VBA. (Even though the file no longer exists in Windows!)
    2. The original XLAM still appears as installed in AddIns manager. (Generally the AddIns Manager doesn't refresh until you completely close & reopen Excel and you would get an error message that the AddIn no longer exists)
    3. The copied AddIn appears as installed and the WB is open (which is correct). However I haven't tested that the copied addin has triggered install/wb open events (which would be needed to load controls). Based on other addin experiments I'm doing, I doubt that it does. (see thread link further down this post)


    For this experiment to have practical use in the real world, these side effects need to be solved.

    I'm focused on other projects at the moment but below is my plan for solving this. I think that all of below can be done but most of the code would need to be called from the copied file:
    1. to solve how to trigger an install/WB open event in the copied add-in (see link to thread at bottom of this post)
    2. from the event in the copy - remove original controls (by calling removecontrols sub in the original file)
    3. from the event in the copy - close original WB
    4. from the event in the copy - delete original file
    5. from the event in the copy - remove original from appearing in Addin Manager (I have code for this. But it requires the XLAM to be deleted first)



    Wishful thinking: Ideally (but I doubt this will ever be possible)
    1. the copied XLAM shouldn't need to change it's filename (I need the filename to remain the same to maintain compatibility with another addin project - version auto updater - which works based on the addins filename) and
    2. the original XLAM should only be uninstalled & removed from AddIn Manager (not file deleted)
    I don't think 2 will ever happen. IIRC AddIns Manager refuses to allow the install of two add-ins with the same filename. And besides my "force refresh AddIns Manager" requires the XLAM to be deleted first - unless I copy the XLAM to another location, force AIM refresh and then copy back?




    Link to thread testing install of controls on copied addin: UPDATE-This is now solved. http://www.excelforum.com/excel-prog...sworkbook.html
    Last edited by mc84excel; 05-14-2014 at 10:54 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] I want to set ThisWorkbook.Path inside a variable in order to open a file.
    By grid in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-21-2013, 11:07 AM
  2. Can't get .presentations.Open ThisWorkbook.Path & "\Pres.ppt" to work
    By gcservi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-08-2012, 09:59 AM
  3. Workbooks.Open() fails in ThisWorkbook.Workbook_Open()
    By GordonRehling in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-03-2011, 08:17 AM
  4. ThisWorkbook.Saved at Open()
    By foxguy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-26-2011, 06:44 PM
  5. Thisworkbook.close closes all open books??
    By FRIEL in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 01-14-2010, 08:59 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1