Change .xla File with MSBuild

后端 未结 3 1639
离开以前
离开以前 2021-01-13 20:54

I\'m trying to create a build script for my current project, which includes an Excel Add-in. The Add-in contains a VBProject with a file modGlobal with a variable version_N

相关标签:
3条回答
  • 2021-01-13 21:29

    You can modify the code in the xla programmatically from within Excel. You will need a reference to the 'Microsoft Visual Basic for Applications Extensibility..' component.

    The examples on Chip Pearson's excellent site should get you started.

    0 讨论(0)
  • 2021-01-13 21:40

    An alternative way of handling versioning of an XLA file is to use a custom property in Document Properties. You can access and manipulate using COM as described here: http://support.microsoft.com/?kbid=224351.

    Advantages of this are:

    • You can examine the version number without opening the XLA file

    • You don't need Excel on your build machine - only the DsoFile.dll component

    Another alternative would be to store the version number (possibly other configuration data too) on a worksheet in the XLA file. The worksheet would not be visible to users of the XLA. One technique I have used in the past is to store the add-in as an XLS file in source control, then as part of the build process (e.g. in a Post-Build event) run the script below to convert it to an XLA in the output directory. This script could be easily extended to update a version number in a worksheet before saving. In my case I did this because my Excel Add-in used VSTO, and Visual Studio doesn't support XLA files directly.

    '
    '   ConvertToXla.vbs
    '
    '   VBScript to convert an Excel spreadsheet (.xls) into an Excel Add-In (.xla)
    '
    '   The script takes two arguments:
    '
    '   - the name of the input XLS file.
    '
    '   - the name of the output XLA file.
    '
    Option Explicit
    Dim nResult
    On Error Resume Next
    nResult = DoAction
    If Err.Number <> 0 Then 
        Wscript.Echo Err.Description
        Wscript.Quit 1
    End If
    Wscript.Quit nResult
    
    Private Function DoAction()
    
        Dim sInputFile, sOutputFile
    
        Dim argNum, argCount: argCount = Wscript.Arguments.Count
    
        If argCount < 2 Then
            Err.Raise 1, "ConvertToXla.vbs", "Missing argument"
        End If
    
        sInputFile = WScript.Arguments(0)
        sOutputFile = WScript.Arguments(1)
    
        Dim xlApplication
    
        Set xlApplication = WScript.CreateObject("Excel.Application")
        On Error Resume Next 
        ConvertFileToXla xlApplication, sInputFile, sOutputFile
        If Err.Number <> 0 Then 
            Dim nErrNumber
            Dim sErrSource
            Dim sErrDescription
            nErrNumber = Err.Number
            sErrSource = Err.Source
            sErrDescription = Err.Description
            xlApplication.Quit
            Err.Raise nErrNumber, sErrSource, sErrDescription
        Else
            xlApplication.Quit
        End If
    
    End Function
    
    Public Sub ConvertFileToXla(xlApplication, sInputFile, sOutputFile)
    
        Dim xlAddIn
        xlAddIn = 18 ' XlFileFormat.xlAddIn
    
        Dim w
        Set w = xlApplication.Workbooks.Open(sInputFile,,,,,,,,,True)
        w.IsAddIn = True
        w.SaveAs sOutputFile, xlAddIn
        w.Close False
    End Sub
    
    0 讨论(0)
  • 2021-01-13 21:49

    I'm not 100% sure how to do exactly what you have requested. But guessing the goal you have in mind there are a few possibilities.

    1) Make part (or all) of your Globals a separate text file that is distributed with the .XLA I would use this for external references such as the version of the rest of your app. Write this at build time and distribute, and read on the load of the XLA.

    2) I'm guessing your writing the version of the main component (ie: the non XLA part) of your application. If this is tru why store this in your XLA? Why not have the main part of the app allow certain version of the XLA to work. Version 1.1 of the main app could accept calls from Version 7.1 - 8.9 of the XLA.

    3) If you are just looking to update the XLA so it gets included in your version control system or similar (i'm guessing here) maybe just touch the file so it looks like it changed.

    If it's the version of the rest of the app that you are controlling i'd just stick it in a text file and distribute that along with the XLA.

    0 讨论(0)
提交回复
热议问题