Results 1 to 4 of 4

Thread: [RESOLVED] Load a word file from a Form

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2006
    Posts
    320

    Resolved [RESOLVED] Load a word file from a Form

    Code:
    Imports System.IO
    Public Class testform
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            FilePathtxt.Text = Nothing
    
    
            OpenFileDialog1.ShowDialog()
    
    
            FilePathtxt.Text = OpenFileDialog1.FileName
    
            Process.Start("winword", FilePathtxt.Text)
    
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Me.Close()
        End Sub
    End Class
    Here is what thought would be a simple procedure. I just want to open a word file using Openfiledialog box for a search. If the file is one word name it appears ok, but if it has multiple word in the file name it errors.
    If the file name is " public buildings Unit.docx"

    the error box comes up 3 times each with an error for each word in the file name
    And it does not open.

    Thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Load a word file from a Form

    The issue is that multiple commandline arguments are separated by spaces so, if your file path contains spaces, it will be interpreted as multiple arguments. The way to force a string containing spaces to be interpreted as a single argument is to wrap it in double-quotes. That's how you do it in a console window so that's how you do it in code too:
    vb.net Code:
    1. Process.Start("winword", $"""{FilePathtxt.Text}""")
    Note that that code uses string interpolation. It is equivalent to:
    vb.net Code:
    1. Process.Start("winword", String.Format("""{0}""", FilePathtxt.Text))
    or:
    vb.net Code:
    1. Process.Start("winword", """" & FilePathtxt.Text & """")
    or:
    vb.net Code:
    1. Process.Start("winword", ControlChars.Quote & FilePathtxt.Text & ControlChars.Quote)

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Load a word file from a Form

    The alternative would be to simply specify the data file path as the file name instead of the argument:
    vb.net Code:
    1. Process.Start(FilePathtxt.Text)
    Doing that, the specified file will be opened in its default application, whatever that may be. If you have restricted the OFD to just Word files, that default application will obviously be Word. Spaces included in the file name are just interpreted as part of the file name, because there can only be one file name.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2006
    Posts
    320

    Re: Load a word file from a Form

    Thanks

    I like the last item best

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