473,403 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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(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
May 19 '06 #1
4 8302
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
May 19 '06 #2

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_TemplatePat h,
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.wdColla pseEnd)
Hope this helps.
"Charlie Brookhart" <ch****************@hotmail.com> escribió en el mensaje
news:Jt********************@adelphia.com...
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

May 19 '06 #3
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_TemplatePat h, 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" <do******@spammuch.com> wrote in message
news:e4*******************@news.demon.co.uk...

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_TemplatePat h,
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.wdColla pseEnd)
Hope this helps.
"Charlie Brookhart" <ch****************@hotmail.com> escribió en el mensaje news:Jt********************@adelphia.com...
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


May 19 '06 #4
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 Brookhart wrote:
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_TemplatePat h, 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" <do******@spammuch.com> wrote in message
news:e4*******************@news.demon.co.uk...
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_TemplatePat h,
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.wdCol lapseEnd)
Hope this helps.
"Charlie Brookhart" <ch****************@hotmail.com> escribió en el


mensaje
news:Jt********************@adelphia.com...
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



May 20 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: D. Alvarado | last post by:
Hello, I am trying to open a window containing an image and I would like the image to be flush against the window -- i.e. have no padding or border. Can I make this happen with a single call to a...
1
by: Andy Davis | last post by:
Hi I've written the following code for a button on a form to open a mail merge document in MS Word. The code opens the document fine but does not display the usual connection details to open the...
3
by: Andy Davis | last post by:
I have set up a mail merge document in Word 2003 which gets its data from my Access 2000 database. I want to set up a button on a form that: 1. runs the query to provide the dat for the merge...
4
by: Yohancef Chin | last post by:
Hi, Being fairly new to .NET I am looking for a way to call MS Word from an event on a webform, and after the user is finished save that created document to an SQL Server database. Has anyone...
3
by: Yohancef Chin | last post by:
Hi, Being fairly new to .NET I am looking for a way to call MS Word from an event on a webform, and after the user is finished save that created document to an SQL Server database. Has anyone...
0
by: dew | last post by:
How do I open a word document programmatically from a button, (1) with no Word prompts such as Save As, and (2) have it jump to a bookmark. I have tried two ways: at the btnShowDocument_click...
3
by: Niyazi | last post by:
Hi, Its like a I am searching alot but still not found or satisfy what I found it. My question is that I had SQL server that contains some data. I also have a application folder call...
1
by: asampath | last post by:
When you open a word document within Internet Explorer, the only toolbar displayed is the Reviewing toolbar. Is there a way to force IE to display the standard toolbar by default (without needing...
0
by: aarthiraaj | last post by:
In my user screen I have view link, if I click this link it will go to servlet and open one word document. It is opening properly, but while opening the attachment it generate one dialog box like...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.