Automation need help

  • Thread starter Thread starter HS1
  • Start date Start date
H

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
 
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.
 
Thanks for you reply.

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

I'm waiting for your further response. THanks.

SH
 
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
 
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
 
Do you want MS Word to quit each time you've finished entering a filename or
not?
 
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.SpecialFolder.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)
 

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

Back
Top