473,386 Members | 1,815 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,386 software developers and data experts.

Automation need help

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

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


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

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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Robert | last post by:
---EN--- This message has been crossposted in a french speaking newsgroup, english version is at the end. Thanks a lot for your help... --/EN--- Bonjour, Je développe une application...
1
by: mickeydisn | last post by:
Sub: C++ Word automation Extract text hello. I want extact text form a word document using a visual c++ programme. I have see a lot of documentation. and my analysis is that I must use a...
15
by: qwweeeit | last post by:
Hi all, Elliot Temple on the 1 June wrote: > How do I make Python press a button on a webpage? I looked at > urllib, but I only see how to open a URL with that. I searched > google but no...
25
by: Neil Ginsberg | last post by:
I have a strange situation with my Access 2000 database. I have code in the database which has worked fine for years, and now all of a sudden doesn't work fine on one or two of my client's...
12
by: Cheval | last post by:
Has anyone had any problems with inter-office automation between MS Word and MS Access in Office 2003? I have recently installed office 2003 in a new folder and have left the older office 2000...
1
by: Lee Seung Hoo | last post by:
hi~ :) I need all information of "Automation" or "Automation Object" what is that ? why is it useful ? How can I use that by C# or .Net Framework ?
6
by: Frank X | last post by:
Excel 2002 introduced a capability to add custom worksheet functions to Excel direct from a COM/ActiveX object. I can use C# to develop a COM object which I can use fine from Excel/VBA, however...
17
by: Mansi | last post by:
I need to do some research on how to use excel automation from c#. Does anyone know of any good books related to this subject? Thanks. Mansi
6
by: a.theil | last post by:
Please help! I need a simple excel automation, just 2 write some files into excel. I do: Dim oXL As Excel.Application Dim oWB As Excel.Workbook Dim oSheet As Excel.Worksheet Dim oRng As...
1
by: electrixnow | last post by:
I need some help on automation for office. I found some code I want to use, it looks like this: Variant wordTables = wordActiveDocument.OlePropertyGet( "Tables" ); long table_count =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.