+ Reply to Thread
Results 1 to 8 of 8

OnOpen event

Hybrid View

  1. #1
    Registered User
    Join Date
    03-20-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    16

    OnOpen event

    Hi All

    I regularly send out .xls files to clients, and (for reasons you might guess, but which aren't relevant here) I need to have a button on Sheet1 of the excel file that, when clicked by the client, will save a copy of the current workbook to the same directory as the current workbook, but with the file extension amended from .xls to .abc . I would like the button to have the caption "Save this file as C:/My Documents/admin/June13/summary.abc" - but using the actual path and filename, which will vary.

    I'm pretty familiar with VBA coding in Access, and I've been googling how to do this, and I think I'm nearly there.

    So far I have:
    In the ThisWorkbook module
    Private Sub Workbook_Open()
    Dim NewFileName As String
    NewFileName = FindAndReplace(Application.ActiveWorkbook.FullName, "xls", "abc")
    Sheet1.Shapes("Button 1").TextFrame.Characters.Text = "Save this file as " & NewFileName
    End Sub
    and in Module1:
    Sub Button1_Click()
    Dim NewFileName As String
    
    ActiveWorkbook.Save
    NewFileName = FindAndReplace(Application.ActiveWorkbook.FullName, "xls", "abcd")
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs Filename:=NewFileName, FileFormat:= _
    xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
    , CreateBackup:=False
    Application.Quit
    End Sub
    (I've not shown the FindAndReplace function, which is also in Module1, and works fine)

    This all seems to work, but I don't understand why the Module1 code has to be in Module1 and not in Sheet1: it didn't work when I tried it there
    I also don't really get how the particular button (which will always be on Sheet1) is referenced. This needs to be right before I go any further, and I'm concerned that I'm not going about it the right way (if I am - great .

    In case it's relevant, the workbook is in fact created from an Access module (using a template), with Access VBA being used to populate some data and to create lots of dropdowns, validation rules, formatting, etc.

    Hope someone can help.
    Thanks in advance
    Les

  2. #2
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: OnOpen event

    Hi Les,

    I also don't really get how the particular button (which will always be on Sheet1) is referenced.
    It's referenced here:
    Sheet1.Shapes("Button 1")
    where "Sheet1" is the codename (not the name you see) of the sheet containing the shape "Button1 ".


    but I don't understand why the Module1 code has to be in Module1 and not in Sheet1: it didn't work when I tried it there
    If you want assign a sub to the shape (button in this case) you have to reference the sheet while calling the sub, e.g.
    Sheet1.Button1_Click
    You also could use the ActiveX Control button, this one has its event in the appropriate sheet only.

    Regards
    Please use [CODE]-TAGS
    When your problem is solved mark the thread SOLVED
    If an answer has helped you please click to give reputation
    Read the FORUM RULES

  3. #3
    Registered User
    Join Date
    03-20-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: OnOpen event

    Hi tehneXus
    Thanks for your reply. I obviously need to do some more homework on the referencing and assigning a sub, etc., and your input is helpful
    Les

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: OnOpen event

    I must admit, I'm intrigued what FindAndReplace() does and how it differs from Replace()

  5. #5
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: OnOpen event

    Hi, Kyle123,

    +1.

    @Les:
    What about using InStrRev for the position of the last . in the string and the using Left with this number if the user has chosen to save a xls as xlsm?
    From the immediate window:
    ?left("C:/My Documents/admin/June13/summary.abc",Instrrev("C:/My Documents/admin/June13/summary.xlsm", "."))&"abc"
    C:/My Documents/admin/June13/summary.abc

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  6. #6
    Registered User
    Join Date
    03-20-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: OnOpen event

    Hi Holger
    Good idea - reckon I'll do that - thanks
    Les

  7. #7
    Registered User
    Join Date
    03-20-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: OnOpen event

    Hi Kyle123
    I knew someone would ask
    Public Function FindAndReplace(ByVal strInString As String, _
            strFindString As String, _
            strReplaceString As String) As String
    Dim intPtr As Integer
        If Len(strFindString) > 0 And Len(strInString) > 0 Then  'catch if try to find empty string
            Do
                intPtr = InStr(strInString, strFindString)
                If intPtr > 0 Then
                    FindAndReplace = FindAndReplace & Left(strInString, intPtr - 1) & _
                                            strReplaceString
                        strInString = Mid(strInString, intPtr + Len(strFindString))
                End If
            Loop While intPtr > 0
        Else
            FindAndReplace = strInString
        End If
        FindAndReplace = FindAndReplace & strInString
    End Function
    I can't remember why I wrote this (it was in access, years ago) - can you see if it is any different from excel's Replace()?

  8. #8
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: OnOpen event

    The replace function exists in Access IIRC, I suspect it does exactly what yours does, although I suspect somewhat more efficiently

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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