473,467 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How do I maintain paragraph numbers into a new word document

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
Try
Dim intNumberOfPages As Integer
Dim intNumberOfChars As String
Dim intPage As Integer

'Word Constants
Const wdGoToPage = 1
Const wdStory = 6
Const wdExtend = 1
Const wdCharacter = 1

'Show WordApp
WordApp.ShowMe()

'Load Base Document
BaseDoc = WordApp.Documents.Open(Filename)
BaseDoc.Repaginate()

'Loop through pages
intNumberOfPages = BaseDoc.BuiltInDocumentProperties("Number of
Pages").Value
intNumberOfChars = BaseDoc.BuiltInDocumentProperties("Number of
Characters").Value

BaseDoc.ShowRevisions = False

If intNumberOfPages > 1 Then
For intPage = 1 To intNumberOfPages
If intPage = intNumberOfPages Then
WordApp.Selection.EndKey(wdStory)
Else
WordApp.Selection.GoTo(What:=Word.WdGoToItem.
wdGoToPage, _
Which:=Word.WdGoToDirection.wdGoToAbsolute, _
Count:=2)
Application.DoEvents()

WordApp.Selection.MoveLeft(Unit:=wdCharacter, Count:
=1)
End If

Application.DoEvents()

WordApp.Selection.HomeKey(wdStory, wdExtend)
Application.DoEvents()

WordApp.Selection.Copy()
Application.DoEvents()

'Create New Document
DestDoc = WordApp.Documents.Add
DestDoc.Activate()

' set margins
If BaseDoc.PageSetup.TopMargin <= 1584 Then
DestDoc.PageSetup.TopMargin = BaseDoc.PageSetup.
TopMargin
End If
If BaseDoc.PageSetup.BottomMargin <= 1584 Then
DestDoc.PageSetup.BottomMargin = BaseDoc.PageSetup.
BottomMargin
End If
If BaseDoc.PageSetup.LeftMargin <= 1584 Then
DestDoc.PageSetup.LeftMargin = BaseDoc.PageSetup.
LeftMargin
End If
If BaseDoc.PageSetup.RightMargin <= 1584 Then
DestDoc.PageSetup.RightMargin = BaseDoc.PageSetup.
RightMargin
End If
If Not BaseDoc.PageSetup.Orientation = Word.WdOrientation.
wdOrientLandscape And Not BaseDoc.PageSetup.Orientation = Word.WdOrientation.
wdOrientPortrait Then
DestDoc.PageSetup.Orientation = WordApp.Selection.
Orientation
Else
DestDoc.PageSetup.Orientation = BaseDoc.PageSetup.
Orientation
End If
DestDoc.PageSetup.PaperSize = Word.WdPaperSize.wdPaperA4
DestDoc.PageSetup.PageHeight = BaseDoc.PageSetup.
PageHeight
DestDoc.PageSetup.PageWidth = BaseDoc.PageSetup.PageWidth
DestDoc.ShowRevisions = False

' paste selecton
WordApp.Selection.Paste()
sNewFileName = Filename
sNewFileName = sNewFileName.Replace(".doc", "_Page" &
intPage.ToString & ".doc")
DestDoc.SaveAs(sNewFileName)
DestDoc.Close()
DestDoc = Nothing

WordApp.Selection.GoTo(wdGoToPage, 2)
Application.DoEvents()

WordApp.Selection.HomeKey(wdStory, wdExtend)
Application.DoEvents()

WordApp.Selection.Delete()
Application.DoEvents()
Next
BaseDoc.Close(False)
BaseDoc = Nothing
Else
BaseDoc.Close(False)
BaseDoc = Nothing
Return "OK"
End If

Return "OK"
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
WordApp.Quit()
WordApp = Nothing
End Try
End Function

However, there is a problem if I have a word document with numbered
paragraphs.

On Page 1 of the document has paragraphs numbered from 1 to 2.9
On Page 2 of the document has paragraphs numbered from 2.10 to 2.16

After running the page extraction sample, the Page 2 document now has
numbered paragraphs from 1 to 1.7 because Word is changing the paragraph
numbering to start from 1 rather than maintain the initial value from where
the numbered paragraphs were copied from.

Is there a method within Word Interop for VB.NET to override the value of the
paragraph number in a new document to take the initial value of what it is
copied from a previous document.

Regards
Adam Faulkner
Croner Software

--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...b-net/200601/1
Jan 27 '06 #1
1 7613
Adam,

As you can set the paragraph in Word with VBA you can set it in VB.NET. The
numbering is context sensetive. It lives in the original document. If you
extract the text you have to set the "context" in which the paragraph
numbering can exist. You can set the start number for each level. Look how
it is done in a macro within Word and extrapolate it to VB. The Word object
model is the same, it is imported from Word by reference.

Regards,

Marcel

"Adam Faulkner via DotNetMonster.com" <u3667@uwe> schreef in bericht
news:5afd59f1e703a@uwe...
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
Try
Dim intNumberOfPages As Integer
Dim intNumberOfChars As String
Dim intPage As Integer

'Word Constants
Const wdGoToPage = 1
Const wdStory = 6
Const wdExtend = 1
Const wdCharacter = 1

'Show WordApp
WordApp.ShowMe()

'Load Base Document
BaseDoc = WordApp.Documents.Open(Filename)
BaseDoc.Repaginate()

'Loop through pages
intNumberOfPages = BaseDoc.BuiltInDocumentProperties("Number of
Pages").Value
intNumberOfChars = BaseDoc.BuiltInDocumentProperties("Number of
Characters").Value

BaseDoc.ShowRevisions = False

If intNumberOfPages > 1 Then
For intPage = 1 To intNumberOfPages
If intPage = intNumberOfPages Then
WordApp.Selection.EndKey(wdStory)
Else
WordApp.Selection.GoTo(What:=Word.WdGoToItem.
wdGoToPage, _
Which:=Word.WdGoToDirection.wdGoToAbsolute, _
Count:=2)
Application.DoEvents()

WordApp.Selection.MoveLeft(Unit:=wdCharacter,
Count:
=1)
End If

Application.DoEvents()

WordApp.Selection.HomeKey(wdStory, wdExtend)
Application.DoEvents()

WordApp.Selection.Copy()
Application.DoEvents()

'Create New Document
DestDoc = WordApp.Documents.Add
DestDoc.Activate()

' set margins
If BaseDoc.PageSetup.TopMargin <= 1584 Then
DestDoc.PageSetup.TopMargin = BaseDoc.PageSetup.
TopMargin
End If
If BaseDoc.PageSetup.BottomMargin <= 1584 Then
DestDoc.PageSetup.BottomMargin = BaseDoc.PageSetup.
BottomMargin
End If
If BaseDoc.PageSetup.LeftMargin <= 1584 Then
DestDoc.PageSetup.LeftMargin = BaseDoc.PageSetup.
LeftMargin
End If
If BaseDoc.PageSetup.RightMargin <= 1584 Then
DestDoc.PageSetup.RightMargin = BaseDoc.PageSetup.
RightMargin
End If
If Not BaseDoc.PageSetup.Orientation =
Word.WdOrientation.
wdOrientLandscape And Not BaseDoc.PageSetup.Orientation =
Word.WdOrientation.
wdOrientPortrait Then
DestDoc.PageSetup.Orientation = WordApp.Selection.
Orientation
Else
DestDoc.PageSetup.Orientation = BaseDoc.PageSetup.
Orientation
End If
DestDoc.PageSetup.PaperSize =
Word.WdPaperSize.wdPaperA4
DestDoc.PageSetup.PageHeight = BaseDoc.PageSetup.
PageHeight
DestDoc.PageSetup.PageWidth =
BaseDoc.PageSetup.PageWidth
DestDoc.ShowRevisions = False

' paste selecton
WordApp.Selection.Paste()
sNewFileName = Filename
sNewFileName = sNewFileName.Replace(".doc", "_Page" &
intPage.ToString & ".doc")
DestDoc.SaveAs(sNewFileName)
DestDoc.Close()
DestDoc = Nothing

WordApp.Selection.GoTo(wdGoToPage, 2)
Application.DoEvents()

WordApp.Selection.HomeKey(wdStory, wdExtend)
Application.DoEvents()

WordApp.Selection.Delete()
Application.DoEvents()
Next
BaseDoc.Close(False)
BaseDoc = Nothing
Else
BaseDoc.Close(False)
BaseDoc = Nothing
Return "OK"
End If

Return "OK"
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
WordApp.Quit()
WordApp = Nothing
End Try
End Function

However, there is a problem if I have a word document with numbered
paragraphs.

On Page 1 of the document has paragraphs numbered from 1 to 2.9
On Page 2 of the document has paragraphs numbered from 2.10 to 2.16

After running the page extraction sample, the Page 2 document now has
numbered paragraphs from 1 to 1.7 because Word is changing the paragraph
numbering to start from 1 rather than maintain the initial value from
where
the numbered paragraphs were copied from.

Is there a method within Word Interop for VB.NET to override the value of
the
paragraph number in a new document to take the initial value of what it is
copied from a previous document.

Regards
Adam Faulkner
Croner Software

--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...b-net/200601/1

Mar 25 '06 #2

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

Similar topics

1
by: Alan T | last post by:
If I place a table or graphic at the beginning of a Word document, does the wordDoc.Paragraphs returns that?
15
by: Yogi | last post by:
Hi there, I have a quick question. In my html document, I want to make a new paragraph whenever I have a blank line in the html source. Using <p> and </pevery time is kind of cumbersome (I want...
3
by: jonniethecodeprince | last post by:
Hello everyone. I want to change the style of a HTML element. I can change the paragraph alignment to left right, justify etc. But I can seem to find the code for any other style change, for...
5
by: lim4801 | last post by:
I am currently in doing a program which is given by my tutor: Contemplate that you are working for the phone company and want to sell "special" phone numbers to companies. These phone numbers are...
3
by: gomsi | last post by:
hi, i have a paragraph where i have to insert <markand </marktags to highlight the required words.i am implementing it in c++ i am currently doing it as follows.. i take the corresponding...
14
by: wshaer | last post by:
Hi all, I have an assignment and I need some help with it. This is the assignment // Research reports are often required to conform to a given standard such as APA or MLA. These standards...
2
by: uamusa | last post by:
I am Dynamically generating a proposal(report) in MS Word. By default the Paragraph Alignment is "Left". For the First 6 Paragraphs I set the Alignment to "Center", and then when attempting to...
1
by: pavanip | last post by:
Hi, I am developing one windows application in that i am trying to integrate microsoft word programatically. I used web browser control to enter text,saving it as a word document but the saved...
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
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
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...
1
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.