yenling
asked on
Create Word in VB
Hi Experts,
Can anyone tell me how to activate Word document when end user clicked on the command button in VB environment?
Operations:
1)Ability in writing a string of text into word document and saved it in the background job.
2)Need to preview the word document and print.
Pls advise!
Can anyone tell me how to activate Word document when end user clicked on the command button in VB environment?
Operations:
1)Ability in writing a string of text into word document and saved it in the background job.
2)Need to preview the word document and print.
Pls advise!
Hi, In addition, you need to create an instance of word object using either 1 of the method below:
1. Working Directly with Objects
Dim Doc as Word.Document
set Doc = CreateObject("Word.Documen t")
...do whatever with the document...
Doc.PrintOut
Doc.SaveAs filename
Set Doc = Nothing
2. Working Through the Application
Dim WordApp as Word.Application
Dim Doc as Word.Document
Set WordApp = new Word.Application
Set Doc = WordApp.Documents.Add
...do whatever with the document...
Set Doc = Nothing
Set WordApp = Nothing
1. Working Directly with Objects
Dim Doc as Word.Document
set Doc = CreateObject("Word.Documen
...do whatever with the document...
Doc.PrintOut
Doc.SaveAs filename
Set Doc = Nothing
2. Working Through the Application
Dim WordApp as Word.Application
Dim Doc as Word.Document
Set WordApp = new Word.Application
Set Doc = WordApp.Documents.Add
...do whatever with the document...
Set Doc = Nothing
Set WordApp = Nothing
ASKER
Can someone give me a working example?
I'm new to this!
I'm new to this!
Hi, firstly i'm not used in Vb / Word integration:
You can set the content of a word document by:
myDoc.Content.Text = "Hello World"
For the inset picture: (Don't Know but here is a stupid alternative method)
Firstly copy your image in ClipBoard , then
myDoc.Content.PasteSpecial
To Preview the document:
myDoc.PrintPreview
To Print the document:
myDoc.PrintOut
'Hope help little.
You can set the content of a word document by:
myDoc.Content.Text = "Hello World"
For the inset picture: (Don't Know but here is a stupid alternative method)
Firstly copy your image in ClipBoard , then
myDoc.Content.PasteSpecial
To Preview the document:
myDoc.PrintPreview
To Print the document:
myDoc.PrintOut
'Hope help little.
ASKER
I want to add text to an existing word document.Pls advise!
ASKER
It don't work.Can u show me an example on adding text to an existing word document?Thanks!
Private Sub Command3_Click()
Dim WordApp As Word.Application
Dim Doc As Word.Document
Set WordApp = New Word.Application
Set Doc = WordApp.Documents.Add
Doc.Content.Text = "Hello World"
Doc.SaveAs "D:\temp\a.doc"
Set Doc = Nothing
Set WordApp = Nothing
End Sub
Private Sub Command3_Click()
Dim WordApp As Word.Application
Dim Doc As Word.Document
Set WordApp = New Word.Application
Set Doc = WordApp.Documents.Add
Doc.Content.Text = "Hello World"
Doc.SaveAs "D:\temp\a.doc"
Set Doc = Nothing
Set WordApp = Nothing
End Sub
Private Sub Command3_Click()
Selection.TypeText Text:="I am typing a text."
end sub
Selection.TypeText Text:="I am typing a text."
end sub
Since I assumed that you are creating a macro in a word documents, you do not have to create instance of word.
Private Sub Command3_Click()
Documents.Open FileName:="C:\my documents\document.doc"
Selection.TypeText Text:="I am typing a text."
ActiveDocument.PrintPrevie w
ActiveDocument.Printout
end sub
Private Sub Command3_Click()
Documents.Open FileName:="C:\my documents\document.doc"
Selection.TypeText Text:="I am typing a text."
ActiveDocument.PrintPrevie
ActiveDocument.Printout
end sub
ASKER
How to create macro in word document?
Open word
"Tools"
"Macro"
"Macros..."
"create new"
"Tools"
"Macro"
"Macros..."
"create new"
ASKER
What are the steps I need to do?Can you teach me step by step?
Hi yenling, try:
modify
myDoc.Content.Text = "Hello World"
to
myDoc.Content.Text = myDoc.Content.Text & vbcrlf & "Hello World"
?
modify
myDoc.Content.Text = "Hello World"
to
myDoc.Content.Text = myDoc.Content.Text & vbcrlf & "Hello World"
?
yenling!
You know how to create a command button right? We start with creating a command button in word.
("View" , "Toolbars" and check "Control toolbox". That will give you option to create a command button)
When you are done, double click on commandbutton. That should open VB. Now add these lines to VB:
'-----------
Private Sub CommandButton1_Click()
Documents.Open FileName:="C:\my documents\document.doc"
Selection.TypeText Text:="I am typing a text."
ActiveDocument.PrintPrevie w
ActiveDocument.Printout
End Sub
'----------------
Now close VB and go back to your command button that you had created earlier.
Make sure that "design mode" is not active(not pushed down) or the button will not work.
You know how to create a command button right? We start with creating a command button in word.
("View" , "Toolbars" and check "Control toolbox". That will give you option to create a command button)
When you are done, double click on commandbutton. That should open VB. Now add these lines to VB:
'-----------
Private Sub CommandButton1_Click()
Documents.Open FileName:="C:\my documents\document.doc"
Selection.TypeText Text:="I am typing a text."
ActiveDocument.PrintPrevie
ActiveDocument.Printout
End Sub
'----------------
Now close VB and go back to your command button that you had created earlier.
Make sure that "design mode" is not active(not pushed down) or the button will not work.
Hi yenling, to confirm the development environment, you use VBA (macro in word) or Visual Basic?
ASKER
Which is the better method?Pls advise!
ASKER
I need to insert text to word document in the next line whenever inserting operation is done.
No, it's depending on what you want to do.
If you want to create a separate program from MS Word, then choose Microsof Visual Basic, else choose Microsoft Word which includes VBA that perform 'like' VB.
If you want to create a separate program from MS Word, then choose Microsof Visual Basic, else choose Microsoft Word which includes VBA that perform 'like' VB.
ASKER
automation error!!!help
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sub EditWord()
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Set wrdApp = CreateObject("Word.Applica tion")
Set wrdDoc = wrdApp.Documents.Open("C:\ LogBk.doc" )
With wrdDoc
.Content.InsertAfter "Here is a example test line#"
.Content.InsertParagraphAf ter
.SaveAs ("C:\LogBk.doc")
.Close
End With
wrdApp.Quit
Set wrdDoc = Nothing
Set wrdApp = Nothing
End Sub
ERROR:AUTOMATION ERROR!!!
PLS HELP!
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Set wrdApp = CreateObject("Word.Applica
Set wrdDoc = wrdApp.Documents.Open("C:\
With wrdDoc
.Content.InsertAfter "Here is a example test line#"
.Content.InsertParagraphAf
.SaveAs ("C:\LogBk.doc")
.Close
End With
wrdApp.Quit
Set wrdDoc = Nothing
Set wrdApp = Nothing
End Sub
ERROR:AUTOMATION ERROR!!!
PLS HELP!
What's your set up. 95, 98, NT, 2000?
ASKER
win 98
Do you have DCOM98 installed?
ASKER
nope.How?
I think its probably on the CD, if not you can obtain it from MS site. You have to install this to use OLE automation.
ASKER
I will download DCOM98....now....
ASKER
In the references,I had already include OLE automation.What is the problem now?Pls advise!
ASKER
it still don't work...automation error again
Did you install DCOM98?
ASKER
I had run the exe file of DCOM98....what other steps have i left out?pls advise......
Try this...
Add a reference Microsoft Word Object Library to a VB project, then
Option Explicit
Private objWordApp As Word.Application
Private Sub Command1_Click()
Set objWordApp = New Word.Application
objWordApp.Visible = True
End Sub
Anything?
If still no go, what's the exact error message?
Add a reference Microsoft Word Object Library to a VB project, then
Option Explicit
Private objWordApp As Word.Application
Private Sub Command1_Click()
Set objWordApp = New Word.Application
objWordApp.Visible = True
End Sub
Anything?
If still no go, what's the exact error message?
Since you are so new with VB. I suggest you to start with recording Macros in Word. You can edit the recorded Macros and learn.
What nigelrowe and ryancys are suggesting is to create a word object in a pure VB invirement. I think it is way too hard to start with for a newbie which doese not know anything about VB and is looking for such a simple commands. You can not use the pure VB commands in Macros created in Word.
Just listen to me please:
Open a new word document. Open "Tools" > "Macro" > "Record a Macro"
Now do what ever you like. Add a text,printview,Print and so on. When you have done everything you want this Macro to do for you. Again go to "Tools" > "Macro" > "Stop recording"
Now your Macro is ready. Just Make a command button as I described earlier and connect the button to the Macro.
Start with this my friend. Later can you edit macros and when you have started to understand what these commands do, you can create your own Macro or VB script in a pure VB invirement.
If somebody wants to learn how to drive a car, it is not such a good idea to ask him start with driving a bus.
What nigelrowe and ryancys are suggesting is to create a word object in a pure VB invirement. I think it is way too hard to start with for a newbie which doese not know anything about VB and is looking for such a simple commands. You can not use the pure VB commands in Macros created in Word.
Just listen to me please:
Open a new word document. Open "Tools" > "Macro" > "Record a Macro"
Now do what ever you like. Add a text,printview,Print and so on. When you have done everything you want this Macro to do for you. Again go to "Tools" > "Macro" > "Stop recording"
Now your Macro is ready. Just Make a command button as I described earlier and connect the button to the Macro.
Start with this my friend. Later can you edit macros and when you have started to understand what these commands do, you can create your own Macro or VB script in a pure VB invirement.
If somebody wants to learn how to drive a car, it is not such a good idea to ask him start with driving a bus.
ASKER
I've already added a reference Microsoft Word Object Library to a VB project.What is causing the problem?
Reference: Microsoft Word 9.0 Object Library
Here are some basics commands you can use. As Navid sais, you can go in word, record a macro and see the code that has been written in the macro with the Visual Basic editor that is in Word. The code is 100% compatible from VBA (for Word) and Visual Basic. All you have to to is to put your Word object in front of the command you want to do.
'Declare My Word Object
Dim GoWord as Word.Application
Set goWord = New Word.Application
'Add a new document in Word
GoWord.Documents.Add
'Go to a specified bookmark in the document
goWord.Selection.GoTo What:=wdGoToBookmark, Name:=yourbookmark
'Type some text:
GoWord.Selection.TypeText Text:=your text
Almost all the commands I use in a Word document is under the Word Object.Selection. You should find what you want there.
Hope it helps.
Here are some basics commands you can use. As Navid sais, you can go in word, record a macro and see the code that has been written in the macro with the Visual Basic editor that is in Word. The code is 100% compatible from VBA (for Word) and Visual Basic. All you have to to is to put your Word object in front of the command you want to do.
'Declare My Word Object
Dim GoWord as Word.Application
Set goWord = New Word.Application
'Add a new document in Word
GoWord.Documents.Add
'Go to a specified bookmark in the document
goWord.Selection.GoTo What:=wdGoToBookmark, Name:=yourbookmark
'Type some text:
GoWord.Selection.TypeText Text:=your text
Almost all the commands I use in a Word document is under the Word Object.Selection. You should find what you want there.
Hope it helps.
What is the exact error message, copy and paste it to your next post!!!
I appreciate your concern, but I must cite an an English proverb; Nothing ventured, nothing gained. The Word object model is very well documented if you take care to install VBA help when you install Word.
Oops, that was intended for Navid
ASKER
Run-time error -2147417846 (80001010a)
Automation error.
Pls advise!
Automation error.
Pls advise!
yenling...
I'll get back to you on monday, when I'm at work!! I'm at home right now, and I haven't got access to MDSN.
I'll get back to you on monday, when I'm at work!! I'm at home right now, and I haven't got access to MDSN.
ASKER
My solution as follows(Microsoft bug with Norton AntiVirus 2000):
http://support.microsoft.com/support/kb/articles/q246/0/18.asp
it's resolved.
Thanks everyone,especially nigelrowe for his helpfulness!I'm sorry that I need to give points to ryancys who answered my doubts first by recommending me to an useful site.
http://support.microsoft.com/support/kb/articles/q246/0/18.asp
it's resolved.
Thanks everyone,especially nigelrowe for his helpfulness!I'm sorry that I need to give points to ryancys who answered my doubts first by recommending me to an useful site.
Sub open_documents()
Documents.Open FileName:="C:\my documents\document.doc"
end sub
Q2:
sub type_a_text()
Selection.TypeText Text:="I am typing a text."
end sub
Q3:
sub preview_and_printout ()
ActiveDocument.PrintPrevie
ActiveDocument.Printout
end sub