WORD应用的怪问题

wulang 2000-06-01 09:52:00
Private Sub Command1_Click()
Dim myDoc As Document
Dim myRange As Range

Set myDoc = New Document
Set myDoc = Documents.Add
Set myRange = myDoc.Content

myDoc.SaveAs App.Path & "\Test.Doc"

With myRange
.Collapse wdCollapseEnd
.InsertAfter "标题"
.InsertParagraphAfter
.Font.Name = "黑体"
.Font.Size = 24
.Paragraphs(1).Alignment = wdAlignParagraphCenter

.Collapse Direction:=wdCollapseEnd
.InsertAfter "内容"
End With

myDoc.Close wdSaveChanges
End Sub

以上这段代码运行起来并没有什么问题,但是只能运行一次,运行第二次(同一次执行期间)时就会出现“远程服务器不存在或不能使用”的错误!
我自己试了一下,如果没有打开WORD,只要用了Close方法,再使用set Doc=new Documents.Add就会出错,如果打开了WORD就没有任何问题。在Delphi中也有类似情况。
我要问各位的是,怎样在不打开WORD的情况下,重复使用Add 和 Close ?
...全文
85 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
太极狮头 2000-06-02
  • 打赏
  • 举报
回复
mydoc.quit
大概是吧,记不清了

oWord=CREATEOBJECT("Word.Application")
WITH oWord
* Uncomment this line to open different BMPS with GETFILE() function
* pic=GETFILE("bmp")
* Comment this line below if using the GETFILE() function
* Change the path according to your installed TASTRADE directory
pic="C:\VFP\SAMPLE\TASTRADE\BITMAPS\BEVERAGE.BMP"
futuredate=DATE()+14
.Documents.Add
.ActiveDocument.Shapes.AddPicture(pic)
.ActiveDocument.Shapes("Picture 2").Select
.Selection.ShapeRange.RelativeHorizontalPosition = 2
.Selection.ShapeRange.RelativeVerticalPosition = 0
.Selection.ShapeRange.Left=4.2*72 && Move 4.2 inches x 72 points/inch
.Selection.ShapeRange.Top = 0
.Selection.ShapeRange.LockAnchor = .T.
.Visible=.T.
.Selection.MoveRight
.Selection.InsertParagraphAfter
.Selection.MoveDown
.Selection.Font.Name = "Arial"
.Selection.Font.Bold = .T.
.Selection.Font.Size = 8
.Selection.InsertDateTime("dddd, MMMM dd, yyyy", 1)
.Selection.ParagraphFormat.Alignment = 1 && Center date
.Selection.InsertParagraphAfter
.Selection.MoveDown
.Selection.ParagraphFormat.Alignment = 3 && Justify
.Selection.InsertParagraphAfter
.Selection.MoveDown
.Selection.InsertAfter("Dear ")
.Selection.MoveRight
.Selection.Fields.Add(oWord.Selection.Range,-1,"author")
.Selection.MoveRight
.Selection.InsertAfter(":")
.Selection.InsertParagraphAfter
.Selection.InsertParagraphAfter
.Selection.MoveDown
.Selection.InsertAfter("This letter is to inform you that ")
.Selection.InsertAfter("you have been selected as the")
.Selection.InsertAfter("grand prizewinner in our contest. ")
.Selection.InsertAfter("To claim your prize, please complete ")
.Selection.InsertAfter("the entry form and return it no later than ")
.Selection.InsertAfter(futuredate)
.Selection.InsertAfter(".")
.Selection.InsertParagraphAfter
.Selection.InsertParagraphAfter
.Selection.InsertAfter("If we have not received your entry within ")
.Selection.InsertAfter("fourteen days, that is, on or before ")
.Selection.InsertAfter(cmonth(futuredate))
.Selection.InsertAfter(" ")
.Selection.InsertAfter(day(futuredate))
.Selection.InsertAfter(", ")
.Selection.InsertAfter((year(futuredate)))
.Selection.InsertAfter(", we will remove your name from the ")
.Selection.InsertAfter("list of eligible winners.")
.Selection.InsertParagraphAfter
.Selection.InsertParagraphAfter
.Selection.InsertAfter("Thank you,")
.Selection.InsertParagraphAfter
.selection.Moveright
.Selection.InsertAfter("The Prize Selection Committee")
.Selection.Font.Name = "Lucida Handwriting"
.selection.Font.Bold=.T.
.Selection.MoveRight
.Application.Activate && Activate Word.
=INKEY(3) && Wait a couple seconds.
* Uncomment the line below to Save the document
*.QUIT(-1) && Quit with saving.
* Comment the line below Save the document.
.QUIT(0) && Quit w/o saving.
ENDWITH


7,762

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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