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

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.Office.Interop.Word
Imports Microsoft.Office.Interop.PowerPoint

Public Class CR_Document_Writer
Private _Word As New Microsoft.Office.Interop.Word.Application
Private _doc As Microsoft.Office.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.Collapse(WdCollapseDirection.wdCollapse End)
_doc.Range.InsertAfter(TextString)
_doc.Range.Style = inStyle
_doc.Range.InsertParagraphAfter()
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 1963
I suggest you should post to: microsoft.public.word.vba.general
"craig" <cr***@discussions.microsoft.comwrote in message
news:40**********************************@microsof t.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.Office.Interop.Word
Imports Microsoft.Office.Interop.PowerPoint

Public Class CR_Document_Writer
Private _Word As New Microsoft.Office.Interop.Word.Application
Private _doc As Microsoft.Office.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.Collapse(WdCollapseDirection.wdCollapse End)
_doc.Range.InsertAfter(TextString)
_doc.Range.Style = inStyle
_doc.Range.InsertParagraphAfter()
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
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...
4
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...
7
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...
3
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...
1
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...
3
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...
2
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...
0
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...
0
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...
4
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...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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
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.