首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

,100分,VB与Word有关问题

2012-01-18 
在线等,100分,VB与Word问题!DimABBwordAsNewWord.ApplicationABBword.Documents.Add加表格1ABBword.Active

在线等,100分,VB与Word问题!
Dim   ABBword   As   New   Word.Application
ABBword.Documents.Add
                                  ' '加表格       1
                                 
              ABBword.ActiveDocument.Tables.Add   Range:=Selection.Range,   NumRows:=1,   NumColumns:=   _
                                  2,   DefaultTableBehavior:=wdWord9TableBehavior,   AutoFitBehavior:=   _
                                  wdAutoFitFixed
                          With   Selection.Tables(1)
                                  If   .Style   <>   "网格型 "   Then
                                          .Style   =   "网格型 "
                                  End   If
                                  .ApplyStyleHeadingRows   =   True
                                  .ApplyStyleLastRow   =   True
                                  .ApplyStyleFirstColumn   =   True
                                  .ApplyStyleLastColumn   =   True
                          End   With
                          ' ' ' ' '设置边框                          
                          Selection.Tables(1).Select
                          With   Selection.Tables(1)
                                  With   .Borders(wdBorderLeft)
                                          .LineStyle   =   wdLineStyleTriple
                                          .LineWidth   =   wdLineWidth050pt
                                          .Color   =   wdColorAutomatic
                                  End   With


                                  With   .Borders(wdBorderRight)
                                          .LineStyle   =   wdLineStyleTriple
                                          .LineWidth   =   wdLineWidth050pt
                                          .Color   =   wdColorAutomatic
                                  End   With
                                  With   .Borders(wdBorderTop)
                                          .LineStyle   =   wdLineStyleTriple
                                          .LineWidth   =   wdLineWidth050pt
                                          .Color   =   wdColorAutomatic
                                  End   With
                                  With   .Borders(wdBorderBottom)
                                          .LineStyle   =   wdLineStyleTriple
                                          .LineWidth   =   wdLineWidth050pt
                                          .Color   =   wdColorAutomatic
                                  End   With
                                  With   .Borders(wdBorderVertical)
                                          .LineStyle   =   wdLineStyleTriple
                                          .LineWidth   =   wdLineWidth050pt
                                          .Color   =   wdColorAutomatic
                                  End   With
                                  .Borders(wdBorderDiagonalDown).LineStyle   =   wdLineStyleNone


                                  .Borders(wdBorderDiagonalUp).LineStyle   =   wdLineStyleNone
                                  .Borders.Shadow   =   False
                          End   With
                         
                          With   Options
                                  .DefaultBorderLineStyle   =   wdLineStyleTriple
                                  .DefaultBorderLineWidth   =   wdLineWidth050pt
                                  .DefaultBorderColor   =   wdColorAutomatic
                          End   With
                              ' '单元格大小设置
                          Selection.Tables(1).Select
                          Selection.Rows.HeightRule   =   wdRowHeightAtLeast
                          Selection.Rows.Height   =   CentimetersToPoints(1.76)
                          Selection.Columns.PreferredWidthType   =   wdPreferredWidthPoints
                          Selection.Columns.PreferredWidth   =   CentimetersToPoints(1.76)
                          Selection.Collapse   Direction:=wdCollapseStart
                          Selection.Move   Unit:=wdColumn,   Count:=-1
                          Selection.SelectColumn
                          Selection.Columns.PreferredWidthType   =   wdPreferredWidthPoints
                          Selection.Columns.PreferredWidth   =   CentimetersToPoints(14)
                          Selection.Move   Unit:=wdColumn,   Count:=1
                          Selection.SelectColumn
                          Selection.Columns.PreferredWidthType   =   wdPreferredWidthPoints
                          Selection.Columns.PreferredWidth   =   CentimetersToPoints(4.8)
                                                   


                          Selection.Tables(1).Select
                          Selection.Font.Size   =   20
                          Selection.MoveLeft   Unit:=wdCharacter,   Count:=1
                          Selection.TypeText   Text:=ABBHand1
                          Selection.ParagraphFormat.Alignment   =   wdAlignParagraphCenter
                          Selection.TypeParagraph
                          Selection.TypeText   Text:= "ppppp "
                          Selection.ParagraphFormat.Alignment   =   wdAlignParagraphCenter
                          Selection.MoveRight   Unit:=wdCharacter,   Count:=1
                          Selection.Font.Size   =   12
                            Selection.TypeText   Text:= "ooooo "
                          Selection.ParagraphFormat.Alignment   =   wdAlignParagraphCenter
                          '将Word的存盘路径改为VB程序所在的目录
                          ABBword.ChangeFileOpenDirectory   (App.Path   &   "\ABB_doc\ ")
                            '保存文档
                          ABBword.ActiveDocument.SaveAs   FileName:= "ABB_Sa_ "   &   CStr(i)   &   ".doc ",   FileFormat:=wdFormateDocumenr,   LockComments:=False,   Password:= " ",   AddToRecentFiles:=True,   WritePassword:= " ",   ReadOnlyRecommended:=False,   EmbedTrueTypeFonts:=False,   SaveNativePictureFormat:=False,   SaveFormsData:=False,   SaveAsAOCELetter:=False                          
                          '关闭文档
                          ABBword.ActiveDocument.Close   SaveChanges:=wdDoNotSaveChange
                         
                          '关闭Word应用程序
                          ABBword.Application.Quit
                          'ABBword.Quit        
        '清除对象变量的值
        Set   ABBword   =   Nothing



程序执行完后生成一个word文件,但是第二次执行是就出错了,提示为:实是错误“462”,远程服务器不存在或不能使用。
请各位大侠指点哦,,,                          


[解决办法]
不要用 Dim ... As New,分成两句
[解决办法]
在次触发前,是不是要先清空一下

[解决办法]
建议找个好例子看看。
google "VB Word.Application "
[解决办法]
是不是第二次执行的时候,上一次的Word.Application还没有完全释放?
比如说第二次执行的时候,在进程中还能看到Word的进程存在。
[解决办法]
试了一下楼主的代码,原因应该就是Word进程无法释放。

在使用Selection或其他word对象前,需要加上当前Application的名字,否则Word进程释放不掉。
在Selection、Options和CentimetersToPoints之前都加上ABBword就可以了,代码如下:

Dim ABBword As New Word.Application
ABBword.Documents.Add

ABBword.ActiveDocument.Tables.Add Range:=ABBword.Selection.Range, NumRows:=1, NumColumns:= _
2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed
With ABBword.Selection.Tables(1)
If .Style <> "网格型 " Then
.Style = "网格型 "
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With

ABBword.Selection.Tables(1).Select
With ABBword.Selection.Tables(1)
With .Borders(wdBorderLeft)
.LineStyle = wdLineStyleTriple
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderRight)
.LineStyle = wdLineStyleTriple
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderTop)
.LineStyle = wdLineStyleTriple
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderBottom)
.LineStyle = wdLineStyleTriple
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderVertical)
.LineStyle = wdLineStyleTriple
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
.Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
.Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
.Borders.Shadow = False
End With

With ABBword.Options
.DefaultBorderLineStyle = wdLineStyleTriple
.DefaultBorderLineWidth = wdLineWidth050pt
.DefaultBorderColor = wdColorAutomatic
End With
' '单元格大小设置
ABBword.Selection.Tables(1).Select
ABBword.Selection.Rows.HeightRule = wdRowHeightAtLeast
ABBword.Selection.Rows.Height = ABBword.CentimetersToPoints(1.76)
ABBword.Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
ABBword.Selection.Columns.PreferredWidth = ABBword.CentimetersToPoints(1.76)
ABBword.Selection.Collapse Direction:=wdCollapseStart
ABBword.Selection.Move Unit:=wdColumn, Count:=-1
ABBword.Selection.SelectColumn
ABBword.Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
ABBword.Selection.Columns.PreferredWidth = ABBword.CentimetersToPoints(14)


ABBword.Selection.Move Unit:=wdColumn, Count:=1
ABBword.Selection.SelectColumn
ABBword.Selection.Columns.PreferredWidthType = wdPreferredWidthPoints
ABBword.Selection.Columns.PreferredWidth = ABBword.CentimetersToPoints(4.8)

ABBword.Selection.Tables(1).Select
ABBword.Selection.Font.Size = 20
ABBword.Selection.MoveLeft Unit:=wdCharacter, Count:=1
ABBword.Selection.TypeText Text:=ABBHand1
ABBword.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
ABBword.Selection.TypeParagraph
ABBword.Selection.TypeText Text:= "ppppp "
ABBword.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
ABBword.Selection.MoveRight Unit:=wdCharacter, Count:=1
ABBword.Selection.Font.Size = 12
ABBword.Selection.TypeText Text:= "ooooo "
ABBword.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter

ABBword.ChangeFileOpenDirectory (App.Path & "\ABB_doc\ ")

ABBword.ActiveDocument.SaveAs FileName:= "ABB_Sa_ " & CStr(i) & ".doc ", FileFormat:=wdFormateDocumenr, LockComments:=False, Password:= " ", AddToRecentFiles:=True, WritePassword:= " ", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:=False

ABBword.ActiveDocument.Close SaveChanges:=wdDoNotSaveChange

ABBword.Application.Quit
ABBword.Quit

Set ABBword = Nothing
[解决办法]
^_^ 如果问题解决,楼主别忘了结帖给点分! ^_^

热点排行