|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
下面是小白我编写的一个代码,出现问题的行都标注了颜色,主要有两个问题,请高手指正。
一是逐句执行到标红颜色语句时,VBA就不再逐句执行,直接运行完整个程序;
二是橙色语句哪里错了,为什么VBA不报错但是也不按照我编写的执行,整个那段语句都没发挥作用,具体原因不明。自己感觉好像是.tables.count返回的值是零,请高手帮忙啊!
Option Explicit
Dim word程序 As Word.Application, 测试文档 As Word.Document
Dim 业务属性表格, 附加属性表格 As Range
Dim i, x, y, z As Integer
Sub xls转换word()
Set word程序 = New Word.Application
word程序.Visible = True
Set 测试文档 = word程序.Documents.Add(Template:="Normal")
测试文档.SaveAs ThisWorkbook.Path & "\" & "指标测试手册(测试1).doc"
x = 0
For i = 3 To 5 'Sheet1.[a65536].End(xlUp).Row
With Sheet8
.[b2].Value = Sheet1.Range("aj" & i).Value
......
End With
Set 业务属性表格 = Sheet8.Range("a1:f7")
Set 附加属性表格 = Sheet8.Range("a9:f12")
x = x + 1
With word程序.Selection
.TypeText Text:=x & "." & Sheet1.Range("d" & i).Value & "/" & Sheet1.Range("e" & i).Value
.ParagraphFormat.CharacterUnitFirstLineIndent = 0 '设置首行字符缩进的语句有很多,但是这个是以字符为单位的
.ParagraphFormat.FirstLineIndent = 0 '为了确保不是首字符缩进2个字符,这里也要设置,确保万无一失
.MoveUp unit:=wdParagraph, Count:=1, Extend:=wdExtend
.Font.Bold = True
.Font.Name = "楷体_GB2312"
.Font.Size = 13
.InsertAfter (vbCrLf)
.MoveDown unit:=wdParagraph, Count:=1, Extend:=wdMove
.HomeKey unit:=wdLine
.ParagraphFormat.CharacterUnitFirstLineIndent = 2
.Font.Name = "宋体"
.Font.Size = 9
.Font.Bold = False
.TypeText Text:=Sheet1.Range("ai" & i).Value
.InsertAfter (vbCrLf)
.MoveDown unit:=wdParagraph, Count:=1, Extend:=wdMove
.HomeKey unit:=wdLine
.ParagraphFormat.CharacterUnitFirstLineIndent = 0
.ParagraphFormat.FirstLineIndent = 0
业务属性表格.Copy
.PasteExcelTable False, False, False
.InsertAfter (vbCrLf)
.MoveDown unit:=wdParagraph, Count:=1, Extend:=wdMove
附加属性表格.Copy
.PasteExcelTable False, False, False
.InsertAfter (vbCrLf)
.MoveDown unit:=wdParagraph, Count:=1, Extend:=wdMove
.InsertBreak Type:=wdPageBreak
End With
Next
With word程序.Selection
For y = 1 To .Tables.Count
.Tables(y).Select
.Tables(y).Rows.LeftIndent = CentimetersToPoints(0)
.Tables(y).PreferredWidthType = wdPreferredWidthPoints
.Tables(y).PreferredWidth = CentimetersToPoints(15.03)
.Tables(y).Rows.HeightRule = wdRowHeightExactly
.Tables(y).Rows.Height = CentimetersToPoints(1)
Next
For z = 1 To .Tables.Count Step 2
.Tables(z).Cell(7, 2).Select
.Tables(z).Rows.HeightRule = wdRowHeightAuto
Next
End With
测试文档.Close
Set word程序 = Nothing
Set 测试文档 = Nothing
Set 业务属性表格 = Nothing
Set 附加属性表格 = Nothing
End Sub
|
|