Consulting

Results 1 to 12 of 12

Thread: Sleeper: Rationalize my import / export code

  1. #1
    VBAX Regular
    Joined
    Aug 2004
    Location
    Manchester UK
    Posts
    16
    Location

    Sleeper: Rationalize my import / export code

    Guys,

    I have this code that I am using to export and import modules into a workbook to be upgraded.

    2 things.

    1 - This line - Application.Dialogs(xlDialogActivate).Show IS NOT making the selected workbook acitive. I need to do this so I can define TWB as the target workbook

    2 - I'm sure I could put a couple of loops in my code to reduce it a bit.

    Thanks in advance . .
    Bob

    Private Sub upgrade()
    'Select target workbook
    Dim TWB As Workbook 'Target Workbook
    Dim UWB As Workbook 'Upgrade Workbook
    Dim FName As String 'temp filename for export / import
    Application.ScreenUpdating = False
    Set UWB = ActiveWorkbook
    MsgBox "Please select the Workbook to upgrade" & vbNewLine & "from the following list . . ." & vbNewLine & "(Click OK to show)"
    Application.Dialogs(xlDialogActivate).Show
    Set TWB = ActiveWorkbook
    'delete existing modules in Target WB
    Dim VBComp As VBComponent
    On Error Resume Next
    With TWB
        Set VBComp = ThisWorkbook.VBProject.VBComponents("frmsavewr")
        ThisWorkbook.VBProject.VBComponents.Remove VBComp
        Set VBComp = ThisWorkbook.VBProject.VBComponents("frmsavecr")
        ThisWorkbook.VBProject.VBComponents.Remove VBComp
        Set VBComp = ThisWorkbook.VBProject.VBComponents("Mdl_Sendmail")
        ThisWorkbook.VBProject.VBComponents.Remove VBComp
        Set VBComp = ThisWorkbook.VBProject.VBComponents("Mdl_spellcheck")
        ThisWorkbook.VBProject.VBComponents.Remove VBComp
        Set VBComp = ThisWorkbook.VBProject.VBComponents("Mdl_Toolbar")
        ThisWorkbook.VBProject.VBComponents.Remove VBComp
    End With
    'import / export code
    With UWB
        FName = .Path & "\code.txt"
        .VBProject.VBComponents("frmsavewr").Export FName
    End With
    TWB.VBProject.VBComponents.Import FName
    With UWB
        FName = .Path & "\code.txt"
        .VBProject.VBComponents("frmsavecr").Export FName
    End With
    TWB.VBProject.VBComponents.Import FName
    With UWB
        FName = .Path & "\code.txt"
        .VBProject.VBComponents("Mdl_Sendmail").Export FName
    End With
    TWB.VBProject.VBComponents.Import FName
    With UWB
        FName = .Path & "\code.txt"
        .VBProject.VBComponents("Mdl_spellcheck").Export FName
    End With
    TWB.VBProject.VBComponents.Import FName
    With UWB
        FName = .Path & "\code.txt"
        .VBProject.VBComponents("Mdl_Toolbar").Export FName
    End With
    TWB.VBProject.VBComponents.Import FName
    Application.ScreenUpdating = True
    End Sub

  2. #2
    Answer to your questions:

    1. It is making the other workbook active, you just can't see it because you have set ScreenUpdating to false. (set screenupdating to true then run your code to confirm this)

    2. What is this piece of code supposed to do? The "With TWB" part is doing nothing because you keep referring to "ThisWorkbook".


    With TWB 
    Set VBComp = ThisWorkbook.VBProject.VBComponents("frmsavewr") 
    ThisWorkbook.VBProject.VBComponents.Remove VBComp 
    Set VBComp = ThisWorkbook.VBProject.VBComponents("frmsavecr") 
    ThisWorkbook.VBProject.VBComponents.Remove VBComp 
    Set VBComp = ThisWorkbook.VBProject.VBComponents("Mdl_Sendmail") 
    ThisWorkbook.VBProject.VBComponents.Remove VBComp 
    Set VBComp = ThisWorkbook.VBProject.VBComponents("Mdl_spellcheck") 
    ThisWorkbook.VBProject.VBComponents.Remove VBComp 
    Set VBComp = ThisWorkbook.VBProject.VBComponents("Mdl_Toolbar") 
    ThisWorkbook.VBProject.VBComponents.Remove VBComp 
    End With

    Another general note. Get rid of the "On Error Resume Next". This is not helping you debug your code. Chances are, Excel will tell you where you problem is if you don't tell it to ignore the errors.

  3. #3
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by Mark O'Brien
    Answer to your questions:

    1. It is making the other workbook active, you just can't see it because you have set ScreenUpdating to false. (set screenupdating to true then run your code to confirm this)

    2. What is this piece of code supposed to do? The "With TWB" part is doing nothing because you keep referring to "ThisWorkbook".


    With TWB 
    Set VBComp = ThisWorkbook.VBProject.VBComponents("frmsavewr") 
    ThisWorkbook.VBProject.VBComponents.Remove VBComp 
    Set VBComp = ThisWorkbook.VBProject.VBComponents("frmsavecr") 
    ThisWorkbook.VBProject.VBComponents.Remove VBComp 
    Set VBComp = ThisWorkbook.VBProject.VBComponents("Mdl_Sendmail") 
    ThisWorkbook.VBProject.VBComponents.Remove VBComp 
    Set VBComp = ThisWorkbook.VBProject.VBComponents("Mdl_spellcheck") 
    ThisWorkbook.VBProject.VBComponents.Remove VBComp 
    Set VBComp = ThisWorkbook.VBProject.VBComponents("Mdl_Toolbar") 
    ThisWorkbook.VBProject.VBComponents.Remove VBComp 
    End With

    Another general note. Get rid of the "On Error Resume Next". This is not helping you debug your code. Chances are, Excel will tell you where you problem is if you don't tell it to ignore the errors.
    Do NOT get rid of necessary On Error statements.
    In this case, the statement is absolutely needed.

  4. #4
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Quote Originally Posted by Howard Kaikow
    Do NOT get rid of necessary On Error statements.
    In this case, the statement is absolutely needed.
    I think he was referring to when Debugging your code. Sometimes the Resume Next is not your best friend when developing/debugging, although useful for any kind of (final) product. It seems you two are on the same page, different paragraphs.

  5. #5
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by firefytr
    I think he was referring to when Debugging your code. Sometimes the Resume Next is not your best friend when developing/debugging, although useful for any kind of (final) product. It seems you two are on the same page, different paragraphs.
    I was referring to necessary error statements.
    In this case, the error statement is necessary because of what is being done.

    One could argue that an On Error GoTo 0 should be inserted as soon as the On Error Resume Next is no longer required.

    On Error Resume Next can be useful even in VB .Net.

  6. #6
    Site Admin
    The Princess
    VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Mark!!



    ~Anne Troy

  7. #7
    VBAX Mentor XL-Dennis's Avatar
    Joined
    May 2004
    Location
    ?stersund, Sweden
    Posts
    499
    Location
    Hi all,

    Well, for debugging purpose it's obvious that the "On Error Resume Next" is not good as it ignore any kind of problems.

    In general I prefer and recommend to to catch the underlying errors enabling us to find out what's wrong.


    '.....
    On Error Goto Error_Handling
    '.....
    Exit_Here:
    Exit Sub
    Error_Handling:
    MsgBox "Error: " & Err.Number & vbCrLf _
    & "Description: " & Err.Description
    Resume Exit_Here


    For production-code that has been debugged and tested and where we know what kind of error that may occur and it's not subject to any actions by the end-users we may use the Resume Next-statement or better use an If-statement/Case-select to singel out unexpected errors.

    Nowadays with so many different kind of configurations and so many versions of Windows as well as Excel in use it's a must to take error-handling seriously

    Kind regards,
    Dennis
    Kind regards,
    Dennis

    ExcelKB | .NET & Excel | 2nd edition PED


  8. #8
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    To get back to the original question here, what symptom(s) indicate that the dialog is not activating the workbook. There is nothing obviously wrong with that bit of the code.

    As Mark has said, though, the With TWB block doesn't actually make use of TWB. It does rather look as if that is the problem. And I would agree that if the On Error statement were disabled during debugging it would probably help.

    And, yes, a loop would shorten your code. Something along the lines of:

    FName = .Path & "\code.txt" 
    For Each mod in Array("frmsavewr","frmsavecr","Mdl_Sendmail", etc.)
    Set VBComp = TWB.VBProject.VBComponents(mod) 
    TWB.VBProject.VBComponents.Remove VBComp 
    UWB.VBProject.VBComponents(mod).Export FName 
    TWB.VBProject.VBComponents.Import FName 
    Next
    I am assuming TWB is correct where I have it shown in red, and I have not checked your code thoroughly - it just looks like that is what you are doing
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  9. #9
    Quote Originally Posted by Howard Kaikow
    Do NOT get rid of necessary On Error statements.
    In this case, the statement is absolutely needed.
    I have never found error trapping necessary or useful while debugging code. The same could be said for your contributions to this thread.

  10. #10
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by XL-Dennis
    Hi all,

    Well, for debugging purpose it's obvious that the "On Error Resume Next" is not good as it ignore any kind of problems.

    In general I prefer and recommend to to catch the underlying errors enabling us to find out what's wrong.

    '.....
    On Error Goto Error_Handling
    '.....
    Exit_Here:
    Exit Sub
    Error_Handling:
    MsgBox "Error: " & Err.Number & vbCrLf _
    & "Description: " & Err.Description
    Resume Exit_Here
    For production-code that has been debugged and tested and where we know what kind of error that may occur and it's not subject to any actions by the end-users we may use the Resume Next-statement or better use an If-statement/Case-select to singel out unexpected errors.

    Nowadays with so many different kind of configurations and so many versions of Windows as well as Excel in use it's a must to take error-handling seriously

    Kind regards,
    Dennis
    I don't disagree, but I was referring to keeping only "necessary" on error resume next statements.

    If a programmer does not know up front that such statements are needed in particular places, they are going to get in trouble anyway.

    You would not believe some the sloppy error handling in code that I've had to fix for clients.

  11. #11

  12. #12
    VBAX Mentor XL-Dennis's Avatar
    Joined
    May 2004
    Location
    ?stersund, Sweden
    Posts
    499
    Location
    You would not believe some the sloppy error handling in code that I've had to fix for clients.
    Me too but the good news is that I charge the clients for it

    I believe we all agree about the need of error-statements and I regret that this subject is not viewed on public forums as this aspect set the final quality of the solutions.
    Kind regards,
    Dennis

    ExcelKB | .NET & Excel | 2nd edition PED


Posting Permissions

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