Opening a MS Word document through a button click

C

Charlie Brookhart

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(sender As Object, e As System.EventArgs)
' Use the open file dialog to choose a word document
If Me.openFileDialog1.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.Reflection.Missing.Value
' 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.Documents.Open(fileName, 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.Selection.TypeText("Copyright C# Corner")
WordApp.Selection.TypeParagraph()
End If
End Sub 'button1_Click
 
D

Dragon

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.Filter property" topic in MSDN for syntax and an example.

Roman
 
F

Fred Hedges

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

m_Document = m_Word.Documents.Add(Template:=CType(m_TemplatePath,
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.Collapse(Word.WdCollapseDirection.wdCollapseEnd)


Hope this helps.
 
C

Charlie Brookhart

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.Documents.Add(Template:=CType(m_TemplatePath, 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 said:
Would like to point out that you don't need to handle "missing" parameters.
For example:

m_Document = m_Word.Documents.Add(Template:=CType(m_TemplatePath,
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.Collapse(Word.WdCollapseDirection.wdCollapseEnd)


Hope this helps.


"Charlie Brookhart" <[email protected]> escribió en el mensaje
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(sender As Object, e As System.EventArgs)
' Use the open file dialog to choose a word document
If Me.openFileDialog1.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.Reflection.Missing.Value
' 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.Documents.Open(fileName, 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.Selection.TypeText("Copyright C# Corner")
WordApp.Selection.TypeParagraph()
End If
End Sub 'button1_Click
 
R

R. MacDonald

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.Documents.Open(fileName, _
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 said:
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.Documents.Add(Template:=CType(m_TemplatePath, 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.

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

For example:

m_Document = m_Word.Documents.Add(Template:=CType(m_TemplatePath,
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.Collapse(Word.WdCollapseDirection.wdCollapseEnd)


Hope this helps.


"Charlie Brookhart" <[email protected]> escribió en el
mensaje
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(sender As Object, e As System.EventArgs)
' Use the open file dialog to choose a word document
If Me.openFileDialog1.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.Reflection.Missing.Value
' 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.Documents.Open(fileName, 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.Selection.TypeText("Copyright C# Corner")
WordApp.Selection.TypeParagraph()
End If
End Sub 'button1_Click
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top