|
技巧242 将电子表格数据输出到Word新文档的部分代码如下:其中:objWord.Application.Visible = True一句,如果改为False则无法正常运行。请教高手如何才能在False状态下正常运行?
Sub WordTest()
Dim objWordApp As Word.Application
Dim objWord As Word.Document
Dim objSel As Word.Selection
Dim strTitle As String
On Error GoTo errHandle
Sheet2.Select
strTitle = Cells(1, 2)
Range(Cells(3, 2), Cells(11, 11)).Select
Selection.Copy
Set objWordApp = CreateObject("Word.Application")
'Set objWordApp = New Word.Application
Set objWord = objWordApp.Documents.Add
objWord.Application.Visible = True'如果改为False则出问题。
Set objSel = objWord.Application.Selection
With objSel
.InsertAfter Text:=strTitle & vbCrLf
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Font.Size = 16
.InsertAfter Text:=vbCrLf
.EndKey Unit:=wdStory
.PasteExcelTable False, False, False
Application.CutCopyMode = False
ThisWorkbook.Activate
Sheet2.Select
Sheet2.ChartObjects(1).CopyPicture
.Paste
End With
objWord.SaveAs Filename:="C:\Documents and Settings\Administrator\My Documents\wordtest.doc", _
FileFormat:=wdFormatDocument
objWord.Close
objWordApp.Quit
errExit:
Set objSel = Nothing
Set objWord = Nothing
Set objWordApp = Nothing
Exit Sub
errHandle:
MsgBox Err.Description
Resume errExit
End Sub
|
|