By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,761 Members | 1,183 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,761 IT Pros & Developers. It's quick & easy.

Automation need help

P: n/a
HS1
Hello all

In my window application, I have a button that can open a Word template
(letter). After add some details, I click "Save as" in this Word document.
I can insert a name for this file, then when I close this word file, I want
to print this file name (and its path).
Could you please tell me a method to do that. Below is what I did but it
does seem to work
I created a WithEvents in the module level:
--------
Public WithEvents objWdDoc As Word.Document
------

Then in a button events, I have
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim objWdApp As Object

objWdApp = CreateObject("Word.Application")

objWdApp.Visible = True

objWdDoc = objWdApp.Documents.Open(Filename:="c:\wordTemplate .doc")

-------------------------------------------

Then I have function to handle its Close event.
------

Private Sub objWdApp_Close()
Debug.Write("objWdDoc.FullName")
set objWdDoc = nothing
End Sub

However, the event seems not work
Could you please help
Many thanks
SH


Nov 21 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
A file with the extension of 'doc' is a Word Document. Word templates have
the file extension 'dot'

What type of information do you want to add?

As soon as you answer me the above question, I will post the cose you want.

Awaiting your reply.

"HS1" wrote:
Hello all

In my window application, I have a button that can open a Word template
(letter). After add some details, I click "Save as" in this Word document.
I can insert a name for this file, then when I close this word file, I want
to print this file name (and its path).
Could you please tell me a method to do that. Below is what I did but it
does seem to work
I created a WithEvents in the module level:
--------
Public WithEvents objWdDoc As Word.Document
------

Then in a button events, I have
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim objWdApp As Object

objWdApp = CreateObject("Word.Application")

objWdApp.Visible = True

objWdDoc = objWdApp.Documents.Open(Filename:="c:\wordTemplate .doc")

-------------------------------------------

Then I have function to handle its Close event.
------

Private Sub objWdApp_Close()
Debug.Write("objWdDoc.FullName")
set objWdDoc = nothing
End Sub

However, the event seems not work
Could you please help
Many thanks
SH


Nov 21 '05 #2

P: n/a

Thanks for you reply.

Yes, you're right. THe file is wordTemplate.dot.

I'm waiting for your further response. THanks.

SH
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #3

P: n/a
Hi

Sorry, I forgot to poste the code a few hours ago. After just checking my
e-mail I realised that.

Ok. This is what I have done:

Start a new Windows application

Add reference to the 'Microsoft Word object library' (my library version is
11.0)

Add a button to the form (Button1) & double-click it.

Paste in the following code:

Dim objWord As Word.Application = New Word.Application
Dim objDoc As New Word.Document
objWord.Documents.Open(Filename:="C:\WordTemplate. doc")
objDoc.Range.InsertAfter("Word Question" & ControlChars.CrLf)
objDoc.Range.InsertAfter("Solved by Crouchie1998")
objWord.Visible = True
objDoc.SaveAs(Application.StartupPath & "\Test.doc")
Dim strFilename As String = objDoc.FullName
'MessageBox.Show(strFilename)
Console.WriteLine(strFilename)
objWord.Quit()
objWord = Nothing

---------------------------------

Once again, sorry to keep you waiting

I hope this helps

"john helen" wrote:

Thanks for you reply.

Yes, you're right. THe file is wordTemplate.dot.

I'm waiting for your further response. THanks.

SH
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 21 '05 #4

P: n/a


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #5

P: n/a

Thank you for your help

In your code, the file is saved as "Test.doc". Could you please tell me
how to change the default "Test.doc". That means I can input a file name
in "Save As" dialog. when I close this file, I the name of the file is
printed in Console

Thank you
SH1
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #6

P: n/a
Do you want MS Word to quit each time you've finished entering a filename or
not?

"john helen" wrote:

Thank you for your help

In your code, the file is saved as "Test.doc". Could you please tell me
how to change the default "Test.doc". That means I can input a file name
in "Save As" dialog. when I close this file, I the name of the file is
printed in Console

Thank you
SH1
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 21 '05 #7

P: n/a
Hi John,

I have re-written the code.

1) Start a new Windows application

2) Add two buttons (Button1 & Button2 respectively)

3) Add a SaveFileDialog (SaveFileDialog1) control by double-clicking on it
from the toolbox

4) Add a reference to the 'Microsoft Word Object Model'

5) Go to the code view of Form1.

6) At the top, paste in the following line:

Imports Word = Microsoft.Office.Interop.Word

7) Paste in the following declarations under the 'form's designer generated
code':

Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim strTemplate As String = "C:\WordTemplate.doc"
Dim strFilename As String

8) Switch to form view & double-click 'Button1' & paste in the following
code in the 'click:

If objWord Is Nothing Then
objWord = CreateObject("Word.Application")
End If
objDoc = objWord.Documents.Open(strTemplate)
Console.WriteLine(objDoc.FullName)
objWord.Visible = True
objDoc.Range.InsertAfter("Word Automation Text" & ControlChars.CrLf)
objDoc.Range.InsertAfter("Created By Crouchie1998")

9) Switch to form view & double-click 'Button2' & paste in the following
code in the 'click:

With SaveFileDialog1
.InitialDirectory =
Environment.GetFolderPath(Environment.SpecialFolde r.Desktop)
.DefaultExt = "doc"
.Filter = "Microsoft Word Docs (*.doc)|*.doc|All Files (*.*)|*.*"
.FilterIndex = 0
If .ShowDialog = DialogResult.OK Then
strFilename = .FileName
End If
End With
objDoc.SaveAs(strFilename)
'MessageBox.Show(strFilename)
Console.WriteLine(strFilename)
CloseWord()

10) Paste in the the following sub:

Private Sub CloseWord()
If Not objWord Is Nothing Then
objWord.Quit()
objWord = Nothing
End If
End Sub

11) Lastly, go to code view, choose 'Form1 Events' from the drop down list.
In the right drop down list choose the 'Form Closing' Enent & paste in this
line of code:

CloseWord()

Now, save the application & run it (F5)

---------------------------------------

I hope this have solved your question
Nov 21 '05 #8

P: n/a


Thank you very very much for your help
I am applying the code
SH

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.