Opening a MS Word document through a button click

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Charlie Brookhart

    #1

    Opening a MS Word document through a button click

    I am trying to write a code for a button click event. When the button is
    clicked, it is supposed to bring up an open file dialog box to allow the
    user to select the document they which to open. That word doucment will then
    be opened and displayed in MS Word. The code I have below doesn't quite work
    the way I expected it to.

    First, VB is complaining that the variable readOnly is not declared. How can
    that be when it is declared as Dim readOnly as Object = False? The next
    thing VB is complaining about is that As is not valid as a keyword
    identifier. If I comment out the read only line, it seems to work.

    The next thing that doesn't quite work as expected is the open file dialog
    box displays no file types. How would those be added?

    Private Sub button1_Click(s ender As Object, e As System.EventArg s)
    ' Use the open file dialog to choose a word document
    If Me.openFileDial og1.ShowDialog( ) = DialogResult.OK Then
    ' set the file name from the open file dialog
    Dim fileName As Object = openFileDialog1 .FileName
    Dim readOnly As Object = False
    Dim isVisible As Object = True
    ' Here is the way to handle parameters you don't care about in .NET
    Dim missing As Object = System.Reflecti on.Missing.Valu e
    ' Make word visible, so you can see what's happening
    WordApp.Visible = True
    ' Open the document that was chosen by the dialog
    Dim aDoc As Word.Document = WordApp.Documen ts.Open(fileNam e, missing,
    [readOnly], missing, missing, missing, missing, missing, missing, missing,
    missing, isVisible) ' Activate the document so it shows up in front
    aDoc.Activate() ;
    ' Add the copyright text and a line break
    WordApp.Selecti on.TypeText("Co pyright C# Corner")
    WordApp.Selecti on.TypeParagrap h()
    End If
    End Sub 'button1_Click


  • Dragon

    #2
    Re: Opening a MS Word document through a button click

    Hello,

    1. ReadOnly is a keyword in VB .NET so in order to use it as identifier
    you must enclose it in square brackets each time. (Or choose another
    name since it'd be very annoying.)
    2. If I understood you, you need to examine Filter property. See
    "FileDialog.Fil ter property" topic in MSDN for syntax and an example.

    Roman


    Comment

    • Fred Hedges

      #3
      Re: Opening a MS Word document through a button click


      Would like to point out that you don't need to handle "missing" parameters.
      For example:

      m_Document = m_Word.Document s.Add(Template: =CType(m_Templa tePath,
      Object))

      Here using "Parameter := Variable", so state which variable should be passed
      into which parameter, all others automatically being "missing" by default.

      Secondly, the word enumerations won't automatically be in your namespace, so
      ..NET won't be able to find them. I use the fully qualified name in order to
      access them. For example, the wdCollapseEnd value is found in the Word.
      namespace:

      theRange.Collap se(Word.WdColla pseDirection.wd CollapseEnd)


      Hope this helps.


      "Charlie Brookhart" <charliebrookha rt46@hotmail.co m> escribió en el mensaje
      news:JtudnWUo5b y1wPDZRVn-pw@adelphia.com ...[color=blue]
      >I am trying to write a code for a button click event. When the button is
      > clicked, it is supposed to bring up an open file dialog box to allow the
      > user to select the document they which to open. That word doucment will
      > then
      > be opened and displayed in MS Word. The code I have below doesn't quite
      > work
      > the way I expected it to.
      >
      > First, VB is complaining that the variable readOnly is not declared. How
      > can
      > that be when it is declared as Dim readOnly as Object = False? The next
      > thing VB is complaining about is that As is not valid as a keyword
      > identifier. If I comment out the read only line, it seems to work.
      >
      > The next thing that doesn't quite work as expected is the open file dialog
      > box displays no file types. How would those be added?
      >
      > Private Sub button1_Click(s ender As Object, e As System.EventArg s)
      > ' Use the open file dialog to choose a word document
      > If Me.openFileDial og1.ShowDialog( ) = DialogResult.OK Then
      > ' set the file name from the open file dialog
      > Dim fileName As Object = openFileDialog1 .FileName
      > Dim readOnly As Object = False
      > Dim isVisible As Object = True
      > ' Here is the way to handle parameters you don't care about in .NET
      > Dim missing As Object = System.Reflecti on.Missing.Valu e
      > ' Make word visible, so you can see what's happening
      > WordApp.Visible = True
      > ' Open the document that was chosen by the dialog
      > Dim aDoc As Word.Document = WordApp.Documen ts.Open(fileNam e, missing,
      > [readOnly], missing, missing, missing, missing, missing, missing, missing,
      > missing, isVisible) ' Activate the document so it shows up in front
      > aDoc.Activate() ;
      > ' Add the copyright text and a line break
      > WordApp.Selecti on.TypeText("Co pyright C# Corner")
      > WordApp.Selecti on.TypeParagrap h()
      > End If
      > End Sub 'button1_Click
      >
      >[/color]


      Comment

      • Charlie Brookhart

        #4
        Re: Opening a MS Word document through a button click

        I'm still not quite understanding what to do.

        The first reply indicated that I needed to enclose readOnly in brackets to
        be able to use it.

        The second reply indicated that I don't need to handle "missing" parameters.

        Where would the line of code: m_Document =
        m_Word.Document s.Add(Template: =CType(m_Templa tePath, Object)) be placed?

        What I am trying to accomplish is clicking a button to open the program
        documentation which is in the MS Word format. The program documentation will
        explain how the program works and how it has met the requirements of the
        user who requested the application. Additionally, it will list system
        requirements. The goal is that this documentation should not be able to be
        changed by the user.

        "Fred Hedges" <dontlike@spamm uch.com> wrote in message
        news:e4kam0$3da $1$8302bc10@new s.demon.co.uk.. .[color=blue]
        >
        > Would like to point out that you don't need to handle "missing"[/color]
        parameters.[color=blue]
        > For example:
        >
        > m_Document = m_Word.Document s.Add(Template: =CType(m_Templa tePath,
        > Object))
        >
        > Here using "Parameter := Variable", so state which variable should be[/color]
        passed[color=blue]
        > into which parameter, all others automatically being "missing" by default.
        >
        > Secondly, the word enumerations won't automatically be in your namespace,[/color]
        so[color=blue]
        > .NET won't be able to find them. I use the fully qualified name in order[/color]
        to[color=blue]
        > access them. For example, the wdCollapseEnd value is found in the Word.
        > namespace:
        >
        > theRange.Collap se(Word.WdColla pseDirection.wd CollapseEnd)
        >
        >
        > Hope this helps.
        >
        >
        > "Charlie Brookhart" <charliebrookha rt46@hotmail.co m> escribió en el[/color]
        mensaje[color=blue]
        > news:JtudnWUo5b y1wPDZRVn-pw@adelphia.com ...[color=green]
        > >I am trying to write a code for a button click event. When the button is
        > > clicked, it is supposed to bring up an open file dialog box to allow the
        > > user to select the document they which to open. That word doucment will
        > > then
        > > be opened and displayed in MS Word. The code I have below doesn't quite
        > > work
        > > the way I expected it to.
        > >
        > > First, VB is complaining that the variable readOnly is not declared. How
        > > can
        > > that be when it is declared as Dim readOnly as Object = False? The next
        > > thing VB is complaining about is that As is not valid as a keyword
        > > identifier. If I comment out the read only line, it seems to work.
        > >
        > > The next thing that doesn't quite work as expected is the open file[/color][/color]
        dialog[color=blue][color=green]
        > > box displays no file types. How would those be added?
        > >
        > > Private Sub button1_Click(s ender As Object, e As System.EventArg s)
        > > ' Use the open file dialog to choose a word document
        > > If Me.openFileDial og1.ShowDialog( ) = DialogResult.OK Then
        > > ' set the file name from the open file dialog
        > > Dim fileName As Object = openFileDialog1 .FileName
        > > Dim readOnly As Object = False
        > > Dim isVisible As Object = True
        > > ' Here is the way to handle parameters you don't care about in .NET
        > > Dim missing As Object = System.Reflecti on.Missing.Valu e
        > > ' Make word visible, so you can see what's happening
        > > WordApp.Visible = True
        > > ' Open the document that was chosen by the dialog
        > > Dim aDoc As Word.Document = WordApp.Documen ts.Open(fileNam e, missing,
        > > [readOnly], missing, missing, missing, missing, missing, missing,[/color][/color]
        missing,[color=blue][color=green]
        > > missing, isVisible) ' Activate the document so it shows up in front
        > > aDoc.Activate() ;
        > > ' Add the copyright text and a line break
        > > WordApp.Selecti on.TypeText("Co pyright C# Corner")
        > > WordApp.Selecti on.TypeParagrap h()
        > > End If
        > > End Sub 'button1_Click
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • R. MacDonald

          #5
          Re: Opening a MS Word document through a button click

          Hello, Charlie,

          What they're trying to say is that the name "readOnly" is a bad choice
          for a variable name, and you should just get rid of the "readOnly" and
          "missing" variables and replace your "Dim aDoc" line with:

          Dim aDoc As Word.Document = WordApp.Documen ts.Open(fileNam e, _
          ReadOnly:=True)

          I'm a bit confused about your code, because I don't see the usual
          "Handles" at the end of "Private Sub Button1_Click.. .". Are you setting
          this code to be the event handler elsewhere in your code?

          Cheers,
          Randy


          Charlie Brookhart wrote:
          [color=blue]
          > I'm still not quite understanding what to do.
          >
          > The first reply indicated that I needed to enclose readOnly in brackets to
          > be able to use it.
          >
          > The second reply indicated that I don't need to handle "missing" parameters.
          >
          > Where would the line of code: m_Document =
          > m_Word.Document s.Add(Template: =CType(m_Templa tePath, Object)) be placed?
          >
          > What I am trying to accomplish is clicking a button to open the program
          > documentation which is in the MS Word format. The program documentation will
          > explain how the program works and how it has met the requirements of the
          > user who requested the application. Additionally, it will list system
          > requirements. The goal is that this documentation should not be able to be
          > changed by the user.
          >
          > "Fred Hedges" <dontlike@spamm uch.com> wrote in message
          > news:e4kam0$3da $1$8302bc10@new s.demon.co.uk.. .
          >[color=green]
          >>Would like to point out that you don't need to handle "missing"[/color]
          >
          > parameters.
          >[color=green]
          >>For example:
          >>
          >> m_Document = m_Word.Document s.Add(Template: =CType(m_Templa tePath,
          >>Object))
          >>
          >>Here using "Parameter := Variable", so state which variable should be[/color]
          >
          > passed
          >[color=green]
          >>into which parameter, all others automatically being "missing" by default.
          >>
          >>Secondly, the word enumerations won't automatically be in your namespace,[/color]
          >
          > so
          >[color=green]
          >>.NET won't be able to find them. I use the fully qualified name in order[/color]
          >
          > to
          >[color=green]
          >>access them. For example, the wdCollapseEnd value is found in the Word.
          >>namespace:
          >>
          >>theRange.Coll apse(Word.WdCol lapseDirection. wdCollapseEnd)
          >>
          >>
          >>Hope this helps.
          >>
          >>
          >>"Charlie Brookhart" <charliebrookha rt46@hotmail.co m> escribió en el[/color]
          >
          > mensaje
          >[color=green]
          >>news:JtudnWUo 5by1wPDZRVn-pw@adelphia.com ...
          >>[color=darkred]
          >>>I am trying to write a code for a button click event. When the button is
          >>>clicked, it is supposed to bring up an open file dialog box to allow the
          >>>user to select the document they which to open. That word doucment will
          >>>then
          >>>be opened and displayed in MS Word. The code I have below doesn't quite
          >>>work
          >>>the way I expected it to.
          >>>
          >>>First, VB is complaining that the variable readOnly is not declared. How
          >>>can
          >>>that be when it is declared as Dim readOnly as Object = False? The next
          >>>thing VB is complaining about is that As is not valid as a keyword
          >>>identifier . If I comment out the read only line, it seems to work.
          >>>
          >>>The next thing that doesn't quite work as expected is the open file[/color][/color]
          >
          > dialog
          >[color=green][color=darkred]
          >>>box displays no file types. How would those be added?
          >>>
          >>>Private Sub button1_Click(s ender As Object, e As System.EventArg s)
          >>>' Use the open file dialog to choose a word document
          >>>If Me.openFileDial og1.ShowDialog( ) = DialogResult.OK Then
          >>>' set the file name from the open file dialog
          >>>Dim fileName As Object = openFileDialog1 .FileName
          >>>Dim readOnly As Object = False
          >>>Dim isVisible As Object = True
          >>>' Here is the way to handle parameters you don't care about in .NET
          >>>Dim missing As Object = System.Reflecti on.Missing.Valu e
          >>>' Make word visible, so you can see what's happening
          >>>WordApp.Visi ble = True
          >>>' Open the document that was chosen by the dialog
          >>>Dim aDoc As Word.Document = WordApp.Documen ts.Open(fileNam e, missing,
          >>>[readOnly], missing, missing, missing, missing, missing, missing,[/color][/color]
          >
          > missing,
          >[color=green][color=darkred]
          >>>missing, isVisible) ' Activate the document so it shows up in front
          >>>aDoc.Activat e();
          >>>' Add the copyright text and a line break
          >>>WordApp.Sele ction.TypeText( "Copyright C# Corner")
          >>>WordApp.Sele ction.TypeParag raph()
          >>>End If
          >>>End Sub 'button1_Click
          >>>
          >>>[/color]
          >>
          >>[/color]
          >
          >[/color]

          Comment

          Working...