Results 1 to 4 of 4

Thread: [RESOLVED] [VB6] Using Variables with objWS.Range.BorderAround

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Posts
    2

    Resolved [RESOLVED] [VB6] Using Variables with objWS.Range.BorderAround

    I am looking for assistance in using variables provided by a text file with the objWS.Range(start, end).BorderAround function. My issue is that I receive a "Type Mismatch" error with the current method I use. I am using VB 6 Enterprise edition and MS Excel 2000.

    The purpose of my program is to create templates in a text file that VB will call to create a spreadsheet. This allows me to create new templates (text files) without hard coding them into my VB application.

    The text file contains many lines but the one I am concerned with is:
    ran,border,A4,G4,xlContinuous,xlThick,xlColorIndexAutomatic

    The actual command that works if hard coded in VB is:
    vbcode Code:
    1. objWS.Range("A4", "G4").BorderAround xlContinuous, xlThick, xlColorIndexAutomatic

    The code I am using that does NOT work is:
    vbcode Code:
    1. Public Function xlFeed(strInputFile As String, strOutputFile As String)
    2. Dim i As Integer
    3. Dim sngTemp As Single
    4. Dim strLine As String
    5. Dim strWord() As String
    6. Dim objXL As Object
    7. Dim objWB As Excel.Workbook
    8. Dim objWS As Excel.Worksheet
    9. Dim objRange As Excel.Range
    10.  
    11. Set objXL = CreateObject("Excel.Application")
    12. objXL.Visible = True
    13. Set objWB = objXL.Workbooks.Add()
    14. Set objWS = objWB.Worksheets(1)
    15. Open strInputFile For Input As #71
    16. Do Until EOF(71)
    17.     Line Input #71, strLine
    18.     strLine = Pack(strLine)
    19.     strLine = Trim(strLine)
    20.     strWord = Split(strLine, ",")
    21.     If UBound(strWord) >= 6 Then
    22.         If LCase(strWord(0)) = "ran" Then
    23.             If LCase(strWord(1)) = "border" Then
    24.                 objWS.Range(strWord(2), strWord(3)).BorderAround strWord(4), strWord(5), strWord(6)
    25.             End If
    26.         End If
    27.     End If
    28. Loop
    29. objWB.ActiveSheet.SaveAs (strOutputFile)
    30. objWB.Close
    31. objXL.Quit
    32. Set objRange = Nothing
    33. Set objWS = Nothing
    34. Set objWB = Nothing
    35. End Function

    strInputFile is the text file; strOutputFile is a new Excel spreadsheet. When I comment out the tail of the command in question the error disappears but I do not get the border I want:

    vbcode Code:
    1. objWS.Range(strWord(2), strWord(3)).BorderAround 'strWord(4), strWord(5), strWord(6)

    Any help would be greatly appreciated. Thank You,

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [VB6] Using Variables with objWS.Range.BorderAround

    Welcome to VBForums

    The problem is that the values like xlContinuous (and anything else starting with "xl") are constants that contain numbers, and you are passing strings (which happen to contain the names of the constants).

    I don't know if there is a way to get the value of a constant using a string that contains its name - but as you are using late-binding, that wouldn't be an option anyway (because if it is possible, it would use the object library somehow).

    There are a few alternatives I can think of ..
    • Write a function that contains a massive Select Case, which does the conversion. This would be large and take a long time to create - and would be prone to mistakes/omissions.
    • Instead of storing the name of the constant to the text file, store the value instead (and use CLng(strWord(4)) etc). This would make it harder to read/update tho.
    • Instead of storing info about the template and creating the file each time.. simply store a pre-created file, which you then copy.
    In most cases, I would pick the last option.

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Posts
    2

    Red face Re: [VB6] Using Variables with objWS.Range.BorderAround

    Si,

    Thanks for the information and thanks for the welcome! I have decided to place the numeric values in my external template files. I only use a few of the xlNAME parameters and is easy to do. I am amazed at the lack of information on the numeric values of constants on the web. I looked forever until I figured out I could retrieve them with a simple "msgbox xlContinuous"! As you can tell I am relatively new to VB and still learning. Again thanks for your time and response.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] [VB6] Using Variables with objWS.Range.BorderAround

    No problem.. a quicker alternative to the MsgBox idea is to go to the Immediate window (press Ctrl-G in the VB editor), then enter this:
    Code:
    ? xlContinuous
    ..and press enter - the value will be printed to the next line (as the values of some constants are like -4302 , being able to copy+paste them is nice!).

Posting Permissions

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



Click Here to Expand Forum to Full Width