none
Filename variable not declared RRS feed

  • Question

  • Hi! 

    Observe the below snippet of code. The last line which says "Filename = Dir(path)"    "Filename" is not previously declared. Is "Filename" a reserved word? Or, is "Filename declared and initialized with this single last line? Thanks! -  John 

    Dim swApp As Object

    Dim Part As Object
    Dim boolstatus As Boolean
    Dim longstatus As Long, longwarnings As Long

    Dim folderPath As String, newFolderPath As String, path As String, count As Integer

    Sub main()

    folderPath = "\\SRVP-SC-FILES01\Common\All\PENG\Test Engineering\stp Files\"
    newFolderPath = "\\SRVP-SC-FILES01\Common\All\PENG\Test Engineering\stp Files\SLDPRT Files\"

    path = folderPath & "*.stp"

    Filename = Dir(path)

    Monday, June 11, 2018 3:26 PM

All replies

  • Hi

    If want to use 'Filename' you would need to declare it.

    For example, Dim Filename as string = folderPath & "*.stp"

    Better still, maybe  Dim Filename As String = IO.Path.Combine(folderPath, "*.stp") would be better.

    What are you trying to achieve, as I think you may be on the wrong track. Are you wanting to list all.stp files from a directory?


    Regards Les, Livingston, Scotland

    Monday, June 11, 2018 3:41 PM
  • Hi Les! Thanks for your assistance. I am taking ownership of the code pasted below. To my knowledge, this subroutine is complete as shown. If there is a declaration for "Filename" perhaps it is declared as a module level var within a different subroutine? I am not sure. Here's the code:

    Dim swApp As Object

    Dim Part As Object
    Dim boolstatus As Boolean
    Dim longstatus As Long, longwarnings As Long

    Dim folderPath As String, newFolderPath As String, path As String, count As Integer

    Sub main()

    folderPath = "\\SRVP-SC-FILES01\Common\All\PENG\Test Engineering\stp Files\"
    newFolderPath = "\\SRVP-SC-FILES01\Common\All\PENG\Test Engineering\stp Files\SLDPRT Files\"

    path = folderPath & "*.stp"

    Filename = Dir(path)

    count = 0


    Set swApp = Application.SldWorks

    Do While Filename <> ""
        
        'Open stp file
        boolstatus = swApp.LoadFile2(folderPath & Filename, "r")
        'MsgBox ("Load and Edit: " & folderPath & FileName)
        
        'Complete SolidWorks functions
        Set Part = swApp.ActiveDoc
        Dim myModelView As Object
        Set myModelView = Part.ActiveView
        myModelView.FrameState = swWindowState_e.swWindowMaximized
        
        'Get rid of .stp extension
        tempSplitArray = Split(Filename, ".")
        'MsgBox ("Naked Name: " & tempSplitArray(0))
        
        'Save as SLDPRT file to new folder
        longstatus = Part.SaveAs3(newFolderPath & tempSplitArray(0) & ".SLDPRT", 0, 2)
        'MsgBox ("Save As: " & folderPath & tempSplitArray(0) & ".SLDPRT")
        
        'Close part files
        swApp.CloseDoc tempSplitArray(0)
        
        'Go to next file
        Filename = Dir()
        
    Loop

    'Tell user that the loop has been completed
    MsgBox ("All Files Have Been Converted")

    End Sub

    Monday, June 11, 2018 3:44 PM
  • Is there no error with that code? Are you saying it does not give you an error saying that Filename is not declared?

    Click on Filename (the name) then press F12; that will take you to the declaration.



    Sam Hobbs
    SimpleSamples.Info

    Monday, June 11, 2018 4:51 PM
  • FileName is indeed not declared. Sam provided the best way to validate this and Les is spot on. So both in their own way have answered your question.

    As per your post, note FileName is never declared.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, June 11, 2018 5:24 PM
    Moderator
  • Actually I am assuming it is declared and I described how to find the declaration.


    Sam Hobbs
    SimpleSamples.Info

    Monday, June 11, 2018 5:43 PM
  • Hi Sam! Thanks for your assistance! It compiles and runs as is. This snippet of code was generated within a Solidworks embedded Visual Basic compiler. I'm not sure what effects this would have on the code shown. Any thoughts?  Thanks! -  John
    Monday, June 11, 2018 5:58 PM
  • I might not understand what you are saying. Are you able to go to the declaration as I described (using F12)?

    You say "code was generated within a Solidworks embedded Visual Basic compiler" but it is not clear what code that is. If the relevant use of Filename is in code compiled by the other compiler then we can't help you here.



    Sam Hobbs
    SimpleSamples.Info

    Monday, June 11, 2018 6:17 PM
  • Hi Sam! Thanks for your assistance! It compiles and runs as is. This snippet of code was generated within a Solidworks embedded Visual Basic compiler. I'm not sure what effects this would have on the code shown.

    If the compiler looks at the code as a snippet on its own, then FileName is not declared.   If the snippet is inserted into some code where FileName has already been declared, it will work (although it's not great coding).

    If it was generated by a SolidWorks utility then the utility has assumed that some generic initialization code is always going to be available when the snippet is inserted.  Or, it is provided as an example only, and requires the user to declare Filename before trying to use the snippet.

    Monday, June 11, 2018 10:18 PM
  • Hi! I will look for a declaration in other locations. Thanks! I will post up my findings. -  John
    Tuesday, June 12, 2018 12:37 PM
  • Hi! I will look for a declaration in other locations. Thanks! I will post up my findings. -  John

    You also need to check the state of Option Infer in the project properties - compile tab as that will change how the variable is declared.

    I suggest it be OFF (and set Option Strict On) then you will need to do each declare yourself instead of letting vs assume what it is you want it to be.

    PS you can also just put them at the top of the form

    Option Infer Off
    Option Strict On
    
    Public Class Form5
    With option strict on you will get every variable that is not declared properly underlined red in the IDE and the project wont run until fixed. Often you can put the mouse pointer over the error and you get a popup menu with suggestions of how to fix the error (intellisense).





    Tuesday, June 12, 2018 12:44 PM