Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Accessing a named range in Excel
Message
De
02/10/2019 11:49:41
 
 
À
Tous
Information générale
Forum:
Visual FoxPro
Catégorie:
COM/DCOM et OLE Automation
Titre:
Accessing a named range in Excel
Divers
Thread ID:
01671332
Message ID:
01671332
Vues:
43
Hi all,
This is a continuation of trying to stamp out some errors in Excel automation. I looked through the error logs in detail and most of them have to do with stuffing a cell with data in Excel. I am using named ranges as the spreadsheet changes over time (by the users) and using named ranges allowed me to not hard code in cell references. So my code first uses a function to get a range object and then it stuffs it with data e.g.:
loNamedRange.Value = "blatz"
I don't know yet but I think the range object is not "alive" yet so based on recommendations here, I am going to try accessing a common property of the range object in a loop to try to capture the error further up. So want to modify that method in my Excel "servers" object (method .GetNamedRange(tcRangeName,toDocument). Here is some of the code from that method - I am asking for input as maybe I am not even getting the range object correctly by using .RefersToRange property:
   TRY

      * check that there are indeed named ranges in the .Names collection
      IF toDocument.Names.Count = 0
         STORE .T. TO llNoNamedRanges, llErrorOccurred
      ENDIF

      *** Check For Specified Range ***

      * unfortunately, unlike Word, Excel does not have a .Names.Exists() method to easily test if the named range exists or not
      * so instead, will loop through the collection of names checking for a match; note that if this becomes slow, we might
      * have to remove this code and just attempt to use the name and trap with error code if not a valid name

      IF NOT llErrorOccurred

         * default flag whether range found
         STORE .F. TO llRangeFound

         * each child in this collection is called a "Name" (if you need to look up in help file)
         FOR EACH loNamedRange IN toDocument.Names

            * the comparison is case sensitive
            IF UPPER(loNamedRange.Name) == UPPER(tcNamedRange)

               STORE .T. TO llRangeFound

               * fetch the range that this name is set to; note that there is no .Range object (e.g. .Name.Range) like I thought
               * there might be but instead there is a property that returns the range for this object; see other notes at bottom

* NOTE: next line is what I am not sure about - it seems to return a range object - and it works 99.999% of the time

               STORE loNamedRange.RefersToRange TO loReturn
               EXIT

            ENDIF

         ENDFOR

         * if range found, try accessing the .Value property; if not found, could not be found so set overall error and specific error
         IF llRangeFound

            * not incorporated yet: try putting a loop here to try a few times with a slight delay between attempts; note: have
            * not tried this code yet; have not tested as wanted to see if I am even getting the range object properly above

            TRY
               lxJunk = loReturn.Value
            CATCH
                 * flags here etc
            ENDTRY

      ELSE  && for if named range not in list of names

            STORE .T. TO llErrorOccurred, llRangeNotFound

         ENDIF

      ENDIF  && for IF NOT llErrorOccurred

   CATCH TO loErrorObj

      * catch of all non-specific errors
      STORE .T. TO llGeneralError, llErrorOccurred

   ENDTRY

ENDIF  && for IF NOT llErrorOccurred
Répondre
Fil
Voir

Click here to load this message in the networking platform