473,801 Members | 2,361 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Word Document

I am trying to insert text into a Word document and then apply a style to the
paragraph I just wrote. My problem is that the range stays set to the entire
document and I can't figure out how to reset it to the end of the document so
whatever style I apply applies to just the last paragraph I wrote. Right now
the style gets applied to the entire document. Here is the class:

Imports System.IO
Imports System.Runtime. InteropServices
Imports Microsoft.Offic e.Interop.Word
Imports Microsoft.Offic e.Interop.Power Point

Public Class CR_Document_Wri ter
Private _Word As New Microsoft.Offic e.Interop.Word. Application
Private _doc As Microsoft.Offic e.Interop.Word. Document
Public Sub Create()
Try
_doc = _Word.Documents .Add("Test.dot" )
_doc.Activate()
_Word.Visible = True
Append("Title Of The Document", "Title")
Append("This is a line of text.", "List Bullet")
_doc.SaveAs("c: \test.doc")
Catch ex As COMException
MessageBox.Show ("Error accessing Word document.")
End Try
End Sub
Private Sub Append(ByVal TextString As String, ByVal inStyle As String)
Try
_doc.Range.Coll apse(WdCollapse Direction.wdCol lapseEnd)
_doc.Range.Inse rtAfter(TextStr ing)
_doc.Range.Styl e = inStyle
_doc.Range.Inse rtParagraphAfte r()
Catch ex As Exception
MessageBox.Show ("Error in writing to Word document.")
End Try
End Sub
End Class

What am I doing wrong?

Thanks,
Craig
Feb 10 '07 #1
1 1990
I suggest you should post to: microsoft.publi c.word.vba.gene ral
"craig" <cr***@discussi ons.microsoft.c omwrote in message
news:40******** *************** ***********@mic rosoft.com...
>I am trying to insert text into a Word document and then apply a style to
the
paragraph I just wrote. My problem is that the range stays set to the
entire
document and I can't figure out how to reset it to the end of the document
so
whatever style I apply applies to just the last paragraph I wrote. Right
now
the style gets applied to the entire document. Here is the class:

Imports System.IO
Imports System.Runtime. InteropServices
Imports Microsoft.Offic e.Interop.Word
Imports Microsoft.Offic e.Interop.Power Point

Public Class CR_Document_Wri ter
Private _Word As New Microsoft.Offic e.Interop.Word. Application
Private _doc As Microsoft.Offic e.Interop.Word. Document
Public Sub Create()
Try
_doc = _Word.Documents .Add("Test.dot" )
_doc.Activate()
_Word.Visible = True
Append("Title Of The Document", "Title")
Append("This is a line of text.", "List Bullet")
_doc.SaveAs("c: \test.doc")
Catch ex As COMException
MessageBox.Show ("Error accessing Word document.")
End Try
End Sub
Private Sub Append(ByVal TextString As String, ByVal inStyle As String)
Try
_doc.Range.Coll apse(WdCollapse Direction.wdCol lapseEnd)
_doc.Range.Inse rtAfter(TextStr ing)
_doc.Range.Styl e = inStyle
_doc.Range.Inse rtParagraphAfte r()
Catch ex As Exception
MessageBox.Show ("Error in writing to Word document.")
End Try
End Sub
End Class

What am I doing wrong?

Thanks,
Craig

Feb 11 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
16191
by: Microsoft | last post by:
I'm trying to display a word document inside a web page, but everytime I do I get this error: Error Type: Microsoft VBScript runtime (0x800A0046) Permission denied: 'CreateObject' Does anybody know the correct way to do this? I don't want to link to a document, but rather display a word file inside the asp page.
4
15858
by: Otis Hunter | last post by:
I have been given an Access Database which contains a table that has an OLE object field that contains a Word document. That table contains hundreds of records. I would like to find out how I can write a VB script, to be executed either within Access or executed at the CMD prompt, which will loop through all the records and open the document object and save it to a Word document that I can access from Windows Explorer. An additional info...
7
6253
by: Dave | last post by:
Apologies for the newbie question. I have created a vb.net program for my company that is designed to work with Word Templates (about forty of them that we commonly use) that are selected by the user and populated (with info from an Access database) at run-time, then saved as Word documents. The program I have coded works fine -- it does what I need it to do. But it has two problems: (1) it runs very slowly, and (2) it does not seem to...
3
30821
by: Adam Faulkner via DotNetMonster.com | last post by:
I want to create a method within a class that opens a Microsoft Word 2000 Document and has the facility to Create a new word document and then extract a Page that exists within the original Word Document and save it to a new Word Document. I would need to generate a loop for each page found within a word document to create a new word document and insert the existing page into the new word document and then save as a new word document. ...
1
7654
by: Adam Faulkner via DotNetMonster.com | last post by:
I had a problem before extracting pages from an existing word document and then inserting the content into a new word document. The following code below works with Microsoft Word 2000 Function ParseWordDoc(ByVal Filename As String) As String Dim sNewFileName As String Dim WordApp As Word.Application = New Word.Application Dim BaseDoc As Word.Document Dim DestDoc As Word.Document
3
2998
by: Niyazi | last post by:
Hi, Its like a I am searching alot but still not found or satisfy what I found it. My question is that I had SQL server that contains some data. I also have a application folder call CUSTDOCUMENT. Once user cliks a button to retrive a document about the customer I need to program in this order:
2
3380
by: Colin Halliday | last post by:
I have a Word 2003 mail merge main document (form letter) that is linked to another Word document data source for the mail merge. If I open this doc using the Word GUI, it first asks me to confirm that I want to run a query to select the data from the data source file, then it opens the form letter fine. I can preview the merged records and complete a merge to a new document. I have a VB 2006 project (.net framework 2.0) which opens the...
0
3233
by: Niyazi | last post by:
Hi, I created application that store the data in SQL SERVER that reside on network. The client also use this application to access the resources provided with application. But is the client want to register new customer or companies they will enter the information in Windows Form and the program automaticaly creates the WORD document under specific folder under application path. Once the empty word file created than ask user if they want...
0
8682
southoz
by: southoz | last post by:
Good ay all , I'm fairly new to access(a little over 5 weeks now). Since I'v started I have picked up a lot of useful information from forums such as this and in doing so will share that information with others. I have seen many request for information on mail merging an how to send data to word documents this is something I have put together with the answers given by others and I hope this will help a few newbies , I know there are easier ways...
4
12452
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is this: can Access create the document and place it as an OLE object to the relevant table? Any help is greatly appreciated. Ricky
0
9697
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9555
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10291
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10260
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6827
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5616
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4156
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3771
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2956
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.