处理 Word 对象的一个例子,你可以研究一下,对于控制 Word 的方法有个了解

alan520cf 2004-08-11 05:36:06
I think most people have read an article or tip about the benefits of creating reports from Lotus Notes in Microsoft Word, using LotusScript and OLE. One of the challenges I ran into, however, is how to navigate within a MSWord document while the report is being created. Specifically, when adding tables to a report, how do I move the cursor out of a table and to the next line of the report in order to add more information? And then, how do I create another table after the text I just entered?

The example I have included shows how to:
处理 Word 对象的一个例子,你可以研究一下,对于控制 Word 的方法有个了解

Create the MSWord OLE object.
Define a range at the end of whatever text has already been entered.
Add a simple table to the document, adding some text and basic table customization.
Navigate out of the table.
Then add some more text without a table.
And finally, loop through and add some more tables and text.
Of course, you could write a whole routine to create several Word documents one right after another. Check other tips on how to programmatically save your new Word document.

P.S. I've included extra constants to save you the work of finding them out!

Code



'OLE: Create Word Document:

Option Public
Option Declare

' WdGoToItem Constants
Const wdGoToLine% = 3
Const wdGoToLast% = -1

' WdTableFormat Constants
Const wdTableFormatClassic2% = 5

' WdColorIndex Constants
Const wdAuto% = 0
Const wdBlack% = 1
Const wdBlue% = 2
Const wdBrightGreen% = 4
Const wdByAuthor% = -1
Const wdDarkBlue% = 9
Const wdDarkRed% = 13
Const wdDarkYellow% = 14
Const wdGray25% = 16
Const wdGray50% = 15
Const wdGreen% = 11
Const wdNoHighlight% = 0
Const wdPink% = 5
Const wdRed% = 6
Const wdTeal% = 10
Const wdTurquoise% = 3
Const wdViolet% = 12
Const wdWhite% = 8
Const wdYellow% = 7

' WdParagraphAlignment Constants
Const wdAlignParagraphCenter% = 1
Const wdAlignParagraphLeft% = 0
Const wdAlignParagraphRight%= 2

Sub Initialize

' Set the Microsoft Word Object
Dim varWrdApp As Variant
Set varWrdApp = CreateObject( "Word.Application" )

' Show Word
varWrdApp.Visible = True

' Add a new document
varWrdApp.Documents.Add

' Set the Word Selection
Dim varWrdSelection As Variant
Set varWrdSelection = varWrdApp.Selection

' Start a loop to create sections
Dim varWrdRange As Variant
Dim varWrdTable As Variant
Dim intPos As Integer
Dim x As Integer
For x% = 1 To 5 ' loops this many times for example's sake

' Find the end of the Word selection
intPos = varWrdSelection.End

' Define the range to the end of the selection and add a new table
Set varWrdRange = varWrdApp.ActiveDocument.Range( intPos, intPos )
Set varWrdTable = varWrdApp.ActiveDocument.Tables.Add( varWrdRange, 1, 1 ) ' simple 1 x 1 table
varWrdSelection.TypeText "Heading " & x%
With varWrdTable
' Set the shading on the first row to light gray
.Rows( 1 ).Cells.Shading.BackgroundPatternColorIndex = wdGray25% ' could expand to multiple rows
' Bold the first row
.Rows( 1 ).Range.Bold = True
' Center the text in Cell (1,1)
.Cell( 1, 1 ).Range.Paragraphs.Alignment = wdAlignParagraphCenter%
End With

' Put the cursor at the end of the selection
varWrdSelection.GoTo wdGoToLine%, wdGoToLast%

' Add text to document
Call InsertLines( varWrdSelection, 1)
varWrdSelection.TypeText "Here's line one of Heading " & x% & "'s report."
Call InsertLines( varWrdSelection, 1)
varWrdSelection.TypeText "Here's line two of Heading " & x% & "'s report."
Call InsertLines( varWrdSelection, 1)
varWrdSelection.TypeText "Here's line three of Heading " & x% & "'s report."
Call InsertLines( varWrdSelection, 2)

Next


' Delete the objects
Set varWrdTable = Nothing
Set varWrdRange = Nothing
Set varWrdSelection = Nothing
Set varWrdApp = Nothing

End Sub

Sub InsertLines( varWrdSelection As Variant, intNumLine As Integer )

Dim intCount As Integer

' Insert the specified number of blank lines
For intCount = 1 To intNumLine
varWrdSelection.TypeParagraph
Next intCount

End Sub

...全文
232 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ricky168 2004-09-19
  • 打赏
  • 举报
回复
这段代码在C/S下可以使用,但在B/S下,就不行了。

但C/S下操控WORD,在好多年前我就发表了文章呀。

B/S下的控件,我也在前几年给客户做出来了,现在使用不错。
rocketliu8899 2004-09-18
  • 打赏
  • 举报
回复
xuexi
haoduoren123456789 2004-08-13
  • 打赏
  • 举报
回复
坐在酒吧里看报纸.
hotxia 2004-08-12
  • 打赏
  • 举报
回复
坐沙发上学习

535

社区成员

发帖
与我相关
我的任务
社区描述
企业开发 Exchange Server
社区管理员
  • 消息协作社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧