Results 1 to 6 of 6

Thread: Need to open text file for use in Excel Backstage/Ribbon component

  1. #1

    Smile Need to open text file for use in Excel Backstage/Ribbon component



    Register for a FREE account, and/
    or Log in to avoid these ads!

    Hi
    I am looking to assign dynamic text to the label property a labelControl object in a custom Backstage/Ribbon project(in Excel), this text needs to come from an external text file.

    I am looking for a method similar to the one which currently works when I want to reference an image file per below:

    Sub getImage(control As IRibbonControl, index As Integer, ByRef image)
    Set image = LoadPicture("c:\temp\image1.jpg")
    End Sub

    The labelControl object has a callback property (getLabel), which I intend to use in order to feed the text from the external text file to it.

    So something like:

    Sub getExternalText(control As IRibbonControl, ByRef textfile)
    Set textfile= LoadFile("c:\temp\test.text")
    End Sub

    ..would be great if it worked.

    Appreciate any suggestions

    Thanks a lot
    Fergal

  2. #2
    Administrator Ken Puls's Avatar
    Join Date
    Mar 2011
    Location
    Nanaimo, BC, Canada
    Posts
    2,276
    Articles
    57
    Blog Entries
    14
    Excel Version
    Excel Office 365 Insider
    What you're going to want to do is write a utility to open the file and retrieve the info you need, then close the file again.

    I'm curious why you settled on a text file? Are you doing one label, and that's it? If you've got more than one, I'd store them in a database, personally.

    I've got code to pull items from a database if you'd like to go that route. If you want to go from a text file though, we can certainly do that, but I'd like to see a sample of the text file so that I can see the interal structure.
    Ken Puls, FCPA, FCMA, MS MVP

    Learn to Master Your Data at the Power Query Academy (the world's most comprehensive online Power Query training), with my book M is for Data Monkey, or our new Power Query Recipe cards!

    Main Site: http://www.excelguru.ca -||- Blog: http://www.excelguru.ca/blog -||- Forums: http://www.excelguru.ca/forums
    Check out the Excelguru Facebook Fan Page -||- Follow Me on Twitter

    If you've been given VBA code (a macro) for your solution, but don't know where to put it, CLICK HERE.

  3. #3
    Hi Ken
    Thanks for the reply
    I found the below solution - which seems to work fine for my (fairly basic) purpose.
    Storing the text in a database isn't something I thought of - but it does sound like a good idea, especially if I'm going to be adding more dynamic-text references.
    We use SQL server for all of our data/reporting needs, so I'm thinking I could try creating a small table in SQL and attempting to reference a column from it - do you have any links/resources I could look at in order to get me started on something basic like this?


    Sub importTextFile(control As IRibbonControl, ByRef labeltext)
    Dim fname As String
    Dim sVal As String
    fname = "C:\temp\Test.txt"
    labeltext = OpenTextFileToString(fname)
    'Debug.Print sVal
    'MsgBox sVal
    End Sub

    Function OpenTextFileToString(ByVal strFile As String) As String
    Dim hFile As Long
    hFile = FreeFile
    Open strFile For Input As #hFile
    OpenTextFileToString = Input$(LOF(hFile), hFile)
    Close #hFile
    End Function

  4. #4
    Administrator Ken Puls's Avatar
    Join Date
    Mar 2011
    Location
    Nanaimo, BC, Canada
    Posts
    2,276
    Articles
    57
    Blog Entries
    14
    Excel Version
    Excel Office 365 Insider
    Sure...

    This article is actually written to pull data back from access. It obviously pulls back entire tables, but I think it should be enough to walk you through connecting to the database. From there you can just modify the strSQL to pull back the SQL query you need. You will need to update the glob_sConnect portion, and you can find the info to do so here: http://www.carlprothman.net/Default.aspx?tabid=87

    I actually use a class module to control my database connections, which makes this really easy. Challenge is that I also use a centralized error handler, so it's actually two class modules. I need to re-write it without that aspect so that I can easily post/share it.

    Hopefully this helps you get started, but if you need any help with it, let me know.
    Ken Puls, FCPA, FCMA, MS MVP

    Learn to Master Your Data at the Power Query Academy (the world's most comprehensive online Power Query training), with my book M is for Data Monkey, or our new Power Query Recipe cards!

    Main Site: http://www.excelguru.ca -||- Blog: http://www.excelguru.ca/blog -||- Forums: http://www.excelguru.ca/forums
    Check out the Excelguru Facebook Fan Page -||- Follow Me on Twitter

    If you've been given VBA code (a macro) for your solution, but don't know where to put it, CLICK HERE.

  5. #5
    great stuff
    The class module to control the database connections sounds very useful
    Thanks for that Ken, I'll check it out next week and advise you if I've any questions etc..

    Fergal

  6. #6
    Administrator Ken Puls's Avatar
    Join Date
    Mar 2011
    Location
    Nanaimo, BC, Canada
    Posts
    2,276
    Articles
    57
    Blog Entries
    14
    Excel Version
    Excel Office 365 Insider
    Hi Fergal,

    I've gone through this and ripped out all the centralized error handler components, setting it back to locally handled. The error handling isn't robust, by any means, and the code is a work in progress. Original intention is to allow it to be robust enough that it can connect to Access (mdb or accdb) files, SQL Server, or SQL express. I've also tried to make it as easy as possible to throw basic SQL in there.

    To use this, import the project into your module. You'd then call the routine with something like this:
    Code:
    Sub Retrieve()
    Dim Db As New clsDBLink
    Dim rcdSet As Recordset
    With Db
        .dbType = dbSQL
        .dbServer = "//servername"
        
    End With
    Set rcdSet = Db.sqlSelectToRecordset("Column Name", "Table Name")
    End Sub
    You will need to set a reference (Tools|References) to the ActiveX Data Objects 2.x Library before you can run the code. The above sample will return whatever query you put in to a recordset that you can parse.

    Other methods in the class are:
    sqlDeleteRecord(FromTable As String, Where As String) As Boolean
    Returns TRUE if record is successfully deleted
    Code:
    bSuccess = sqlDeleteRecord("Employees","Name='Joe Bloggs'")
    If bSuccess Then msgbox("Employee deleted!")
    sqlInsertRecord(ToTable As String, Columns As String, Values As String) As Boolean
    Returns TRUE if record successfully insert

    sqlSelectToArray(sColumns As String, FromTable As String, Optional Where As String, Optional OrderBy As String) As Variant
    Returns an array of data or an array of with 0,0 elements if no data retreived

    sqlSelectToRange(clTrgt As Range, Columns As String, FromTable As String, Optional Where As String, Optional OrderBy As String) As Boolean
    Returns the recordset directly to a range and returns TRUE if successful, FALSE if not

    sqlSelectToRecordset(Columns As String, FromTable As String,Optional Where As String, Optional OrderBy As String) As ADODB.Recordset
    Returns the recordset to an adodb recordset

    sqlUpdateRecord(ToTable As String, SetColumnTo As String, Where As String) As Boolean
    Updates a recordset, returns TRUE if successful, FALSE if not

    sqlUserDefined(sSQL As String) As Boolean
    Put in whatever SQL query you want. Returns TRUE if executed succesfully, FALSE if not.

    I use those TRUE/FALSE states to toggle:
    TransactionProcessing(tpState)
    Between tpBegin, tpCommit and tpRollBack

    Hope this helps,
    Attached Files Attached Files
    Ken Puls, FCPA, FCMA, MS MVP

    Learn to Master Your Data at the Power Query Academy (the world's most comprehensive online Power Query training), with my book M is for Data Monkey, or our new Power Query Recipe cards!

    Main Site: http://www.excelguru.ca -||- Blog: http://www.excelguru.ca/blog -||- Forums: http://www.excelguru.ca/forums
    Check out the Excelguru Facebook Fan Page -||- Follow Me on Twitter

    If you've been given VBA code (a macro) for your solution, but don't know where to put it, CLICK HERE.

Tags for this Thread

Posting Permissions

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