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
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