Call WORD via VB.Net

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I want to try call/run Ms. Word via my VB.Net
First I added reference : Microsoft Word 11.0 Object Library. Then I got 2 references in my reference list : Word and Microsoft.Office.Core

When I typed :
Dim objWord as Word.Application
Dim objDocument as Word.Document

I got an error (underline at Word.Application and Word.Document).
What is actually happen ?
Can anybody tell how to do what I want ?

Thanks in advance.
Joachim
 
Joachim said:
I want to try call/run Ms. Word via my VB.Net
First I added reference : Microsoft Word 11.0 Object Library. Then I got 2 references in my reference list : Word and Microsoft.Office.Core

When I typed :
Dim objWord as Word.Application
Dim objDocument as Word.Document

I got an error (underline at Word.Application and Word.Document).
What is actually happen ?
Can anybody tell how to do what I want ?

Hi Joachim,

seems you have Office XP PIAs installed
http://www.microsoft.com/downloads/details.aspx?FamilyId=C41BD61E-3060-4F71-A6B4-01FEBA508E52

It should be

Microsoft.Office.Interop.Word.ApplicationClass
Microsoft.Office.Interop.Word.DocumentClass

Understanding the Word Object Model from a .NET Developer's Perspective:
http://msdn.microsoft.com/vstudio/office/?pull=/library/en-us/odc_vsto2003_ta/html/wordobject.asp

Cheers

Arne Janning
 
Thanks Arne,

It works now.
I have one more problem/question. I hope you can help me out.

I have a templete (WORD templete) : MyTemplete.dot
This is a 1 page templete with several bookmarks.
I want to "print out" all my data/record with MyTemplete file via VB.Net.
1 page for 1 record, but all of the pages should be in 1 document.
Ex. : 50 records mean 50 pages WORD document.
The problem is : What should I do to make 1 record 1 page ?

Thanks in advance,
Regards,
Joachim.


Below is my code :
====
Private sub PrintwithWord()
Dim objWord As Word.Application
Dim objDocument As Word.Document
objWord = CType(CreateObject("Word.Application"), Word.Application)
objDocument = CType(objWord.Documents.Open(AppPath & "MyExamen.dot"), Word.Document)

For i = 1 To TotalRecords
ReadRecord()
objDocument = CType(objWord.Documents.Open(AppPath & "MyTempelte.dot"), Word.Document)
objWord.Visible = True
If objDocument Is Nothing Then
MsgBox("The Templete file not fond !", MsgBoxStyle.Critical, "Error")
Exit Sub
End If
objWord.ActiveDocument.Bookmarks.Item("Bookmarks1").Range.Text = MyField1
objWord.ActiveDocument.Bookmarks.Item("Bookmarks2").Range.Text = MyField2
objWord.ActiveDocument.Bookmarks.Item("Bookmarks3").Range.Text = MyField3
objWord.ActiveDocument.Bookmarks.Item("Bookmarks4").Range.Text = MyField4
objWord.ActiveDocument.Bookmarks.Item("Bookmarks5").Range.Text = MyField5
objDocument.PrintPreview()
objDocument.SaveAs(UserPath & "\" & "-Text" & ".doc")
objDocument.Close()
Next
objWord.Visible = False
CLoseWord()
End Sub
=====
 
Joachim wrote:

hi Joachim,

insert a pagebreak after each record:

objWord.Selection.InsertBreak (Type:=wdPageBreak)
or
objWord.Range.InsertBreak (Type:=wdPageBreak)

Cheers
Arne Janning
 
Thanks Arne,

There is no "objWord.Range.InsertBreak (Type:=wdPageBreak)"
I did add : "objWord.Selection.InsertBreak (Type:=wdPageBreak)"
But I got error (underline) at (Type:=wdPageBreak)

What I have done is :
I inserted "objWord.Selection.InsertBreak (Type:=wdPageBreak)" after bookmark5.
I relocated :
objDocument.PrintPreview()
objDocument.SaveAs(UserPath & "\" & "-Text" & ".doc")
objDocument.Close()
to after NEXT statement.

Why I got an error ?

Regards,
Joachim
 
Joachim said:
It should like this :
objWord.Selection.InsertBreak(Word.WdBreakType.wdPageBreak)

But the result is not what I like.
I have 5 records (just for trying). The first 4 pages are empty.
The last page gives me the result, but like this :
(The first bookmark is Name)
Name : Name1NAme2Name3Name4Name5
Address : Adr1Adr2Adr3Adr4Adr5
They stick together in one line.

What I should do now ?
Please help me....

What is the code you're actually using?

Cheers

AJ
 
What is the code you're actually using?
Cheers

AJ

Hi, Arne.

Here is my code :
======
Private sub PrintwithWord()
Dim objWord As Word.Application
Dim objDocument As Word.Document
objWord = CType(CreateObject("Word.Application"), Word.Application)
objDocument = CType(objWord.Documents.Open(AppPath & "MyExamen.dot"), Word.Document)

For i = 1 To TotalRecords
ReadRecord()
objDocument = CType(objWord.Documents.Open(AppPath & "MyTempelte.dot"), Word.Document)
objWord.Visible = True
If objDocument Is Nothing Then
MsgBox("The Templete file not fond !", MsgBoxStyle.Critical, "Error")
Exit Sub
End If
objWord.ActiveDocument.Bookmarks.Item("Bookmarks1").Range.Text = MyField1
objWord.ActiveDocument.Bookmarks.Item("Bookmarks2").Range.Text = MyField2
objWord.ActiveDocument.Bookmarks.Item("Bookmarks3").Range.Text = MyField3
objWord.ActiveDocument.Bookmarks.Item("Bookmarks4").Range.Text = MyField4
objWord.ActiveDocument.Bookmarks.Item("Bookmarks5").Range.Text = MyField5

' I have tried all of two, but got the same result.
'objWord.Selection.InsertBreak(Word.WdBreakType.wdPageBreak)
objDocument.ActiveWindow.Selection.InsertBreak()
Next
objWord.Visible = False
objDocument.PrintPreview()
objDocument.SaveAs(UserPath & "\" & "-Text" & ".doc")
objDocument.Close()
CLoseWord()
End Sub
 
Joachim said:
Hi, Arne.

Here is my code :
======
Private sub PrintwithWord()
Dim objWord As Word.Application
Dim objDocument As Word.Document
objWord = CType(CreateObject("Word.Application"), Word.Application)
objDocument = CType(objWord.Documents.Open(AppPath & "MyExamen.dot"), Word.Document)

For i = 1 To TotalRecords
ReadRecord()
objDocument = CType(objWord.Documents.Open(AppPath & "MyTempelte.dot"), Word.Document)
objWord.Visible = True
If objDocument Is Nothing Then
MsgBox("The Templete file not fond !", MsgBoxStyle.Critical, "Error")
Exit Sub
End If
objWord.ActiveDocument.Bookmarks.Item("Bookmarks1").Range.Text = MyField1
objWord.ActiveDocument.Bookmarks.Item("Bookmarks2").Range.Text = MyField2
objWord.ActiveDocument.Bookmarks.Item("Bookmarks3").Range.Text = MyField3
objWord.ActiveDocument.Bookmarks.Item("Bookmarks4").Range.Text = MyField4
objWord.ActiveDocument.Bookmarks.Item("Bookmarks5").Range.Text = MyField5

' I have tried all of two, but got the same result.
'objWord.Selection.InsertBreak(Word.WdBreakType.wdPageBreak)
objDocument.ActiveWindow.Selection.InsertBreak()
Next
objWord.Visible = False
objDocument.PrintPreview()
objDocument.SaveAs(UserPath & "\" & "-Text" & ".doc")
objDocument.Close()
CLoseWord()
End Sub


Hi Joachim,

2 thoughts:

1. objWord is never saved? You only save objDocument?
2. objDocument.ActiveWindow.Selection.InsertBreak(). Where is the
Selection during runtime? At the end of the page? At the top? Don't you
have to set the selection to the end of the page before calling
InsertBreak()? You will only get empty pages otherwise.

Cheers

Arne Janning
 
Great to see you.
Finally I get your reply, I have waited for this.
As a beginner, I don't know how to code it, I just try myself.
What I want is 1 record 1 page in WORD and all record in one .DOC file.
So Arne, how should I code it ?
Will you change my code to meet what I want, please ?
Please help me out Arne !
 
Back
Top