|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Opening a MS Word document through a button clickclicked, 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 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 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. Show quoteHide quote "Charlie Brookhart" <charliebrookhar***@hotmail.com> escribió en el mensaje news:JtudnWUo5by1wPDZRVn-pw@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 > > 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. Show quoteHide quote "Fred Hedges" <dontlike@spammuch.com> wrote in message news:e4kam0$3da$1$8302bc10@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_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" <charliebrookhar***@hotmail.com> escribió en el mensaje > news:JtudnWUo5by1wPDZRVn-pw@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 > > > > > > 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: Show quoteHide quote > 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" <dontlike@spammuch.com> wrote in message > news:e4kam0$3da$1$8302bc10@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_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" <charliebrookhar***@hotmail.com> escribió en el > > mensaje > >>news:JtudnWUo5by1wPDZRVn-pw@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 >>> >>> >> >> > >
2 dimensional array-->1 dimensional array
Help with deleting a Row in a database Datagrid - recognizing changes made Download a file from a secured Https Server about example code FTP routines programmed by VB .net SharpDevelop vs M'soft IDE textbox currency VS 2005 Form Appearance / "Theme" Check File Permissions and Usage |
|||||||||||||||||||||||