VBA for Word/Long File names with spaces


  • I have a VBA/Word problem where I need a to run a shell command ("xelatex") on a file (say "f'") that is stored as an item in a preference file, i.e. I need to run something like "xelatex f" where "f" is something like


    C:\Documents and Settings\jonathan\My Documents\file.tex


    The problem is that the space in "Documents and Settings" is not working (if the file is at the root without spaces everything works).


    The VBA command CompileTex() is shown below.


    How do I run the shell command so that spaces in the file location work?






    Sub CompileTex()
        mainfile = getValue("mainfile")

        Shell "xelatex --src-specials " & mainfile & ".tex", vbNormalFocus

    End Sub


    Function fbase()
        Set fs = CreateObject("Scripting.FileSystemObject")
        mf = getValue("mainfile")
        r = fs.getparentfoldername(mf)
        ChDir r
        fbase = r
    End Function


    Function getValue(s)
        Open pfile & ".ini" For Input As #1
        Input #1, s1, v1
        Close #1
        getValue = v1
    End Function


    Function pfile()
        pfile = "c:\texdata\Preferences"
    End Function


    Sub SetAsMainFile()
        Set fs = CreateObject("Scripting.FileSystemObject")
        tfname = ActiveDocument.FullName
        pf = fs.getparentfoldername(tfname)
        bf = fs.getbasename(tfname)
        setValue "mainfile", pf & "\" & bf
    End Sub

    Monday, November 19, 2007 7:24 PM

All replies

  • Hi!

    I see two options for you:

    1. You nac use special environment variable "%USERPROFILE%" that equals "C:\Documents and Settings\jonathan\"

    2. You can use quotes in your folder path like this:

    Shell "xelatex --src-specials """ & mainfile & ".tex""", vbNormalFocus

    Tuesday, November 20, 2007 6:17 AM
  • I have seen the quotes used by many programmers.
    Tuesday, November 20, 2007 9:35 AM
  • When you use the batch programs like xelatex, they will take space as delimiter


    one option is to give them the short folder names.


    Here is a hint to do that:


    Code Block

    Sub Invoke_Non_Command_Interpreter()

    Dim sReturnFolder
    Dim sPath As String

    sPath = Environ("UserProfile")
    sPath = ""
    sReturnFolder = Shell(Environ("comspec") & " /c dir /x  " & sPath & "*  > c:\temp\c1.txt")

    End Sub





    The short names are stored in one text file. This can be read and made as input to your program




    Wednesday, November 21, 2007 11:10 AM