|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
写了一个将excel中数据导出到word的程序,第一运行正常。但是第二次运行就会报错。请大神们帮忙看看代码需要怎么处理下?
- Sub EXCEL转Word()
- Application.ScreenUpdating = False '关闭屏幕刷新
- Application.DisplayAlerts = False '关闭提示
- ' On Error Resume Next '忽略错误
- Dim wApp As Word.Application
- Dim wDoc As Word.Document
- Set wApp = CreateObject("Word.Application")
- Dim rownum, colnum, i, j, k, l, tabnum, icon As Integer
- Dim val1, val2, oldval, doctitile As String
- doctitile = Sheet9.Range("B2").Value & "工艺单元设计方案"
- wApp.Visible = True
- Set wDoc = wApp.Documents.Add(Template:=ThisWorkbook.Path & "" & "工艺单体设计模板.dotx", NewTemplate:=False, DocumentType:=0)
- Excel.Application.Sheets("设备选型表").Activate '切换当前电子表格的表bell为当前激活表
- row1 = Range("B65535").End(xlUp).Row
- For i = 3 To row1
- val1 = Excel.Application.Sheets("设备选型表").Range("a" & i)
-
-
-
- '读取工艺功能描述
-
- Dim find1 As String
- Dim rownum2, lnum1 As Integer
-
- rownum2 = Sheet15.[A65536].End(xlUp).Row
-
- For lnum1 = 3 To rownum2
-
- If Excel.Application.Sheets("工艺单元功能描述").Range("A" & lnum1).Value = val1 Then
-
- find1 = Excel.Application.Sheets("工艺单元功能描述").Range("B" & lnum1).Value
-
- Exit For
-
- End If
-
- Next
-
-
- '读取构建筑物部分
-
- Dim find2 As String
-
- Dim totalrow2, lnum2, findnum As Integer
-
- totalrow2 = Sheet4.[B65536].End(xlUp).Row
-
- For lnum2 = 3 To totalrow2
-
- If Excel.Application.Sheets("6-1 土建投资").Range("B" & lnum2).Value = val1 Then
-
- findnum = lnum2
- Exit For
-
- End If
- Next
-
- '读取工艺设计参数部分
-
- Dim find3 As String
-
- Dim totalrow3, lnum3, findnum2, totalcol As Integer
-
- totalcol = Sheets("工艺单元参数设计").Range("ZZ" & i).End(xlToLeft).Column
-
- totalrow3 = Sheet16.[A65536].End(xlUp).Row
-
-
- For lnum3 = 3 To totalrow3
-
- If Excel.Application.Sheets("工艺单元参数设计").Range("A" & lnum3).Value = val1 Then
-
- findnum2 = lnum3
- Exit For
-
- End If
- Next
-
-
- '读取设备名称部分
-
- If val1 <> oldval Then
-
-
- With wApp.Application
-
-
- .Activate '激活WORD软件
- .Selection.EndKey Unit:=wdStory
-
-
- .Selection.Style = .ActiveDocument.Styles("标题 2")
-
- .Selection.TypeText Text:=val1
-
- .Selection.TypeParagraph
-
-
- .Selection.Style = .ActiveDocument.Styles("标题 3")
- .Selection.TypeText Text:="功能描述"
- .Selection.TypeParagraph
- .Selection.TypeText Text:=find1
- .Selection.TypeParagraph
-
-
-
- .Selection.Style = .ActiveDocument.Styles("标题 3")
- .Selection.TypeText Text:="构建筑物参数"
- .Selection.TypeParagraph
-
- Sheets("6-1 土建投资").Range("C" & findnum & ":D" & findnum).Copy
- .Activate
- .Selection.PasteExcelTable False, False, False
-
- Sheets("6-1 土建投资").Range("J" & findnum & ":K" & findnum).Copy
- .Activate
- .Selection.PasteExcelTable False, False, False
- .Selection.MoveLeft Unit:=wdCharacter, Count:=2
- .Selection.TypeText Text:=Sheets("6-1 土建投资").Range("L" & findnum).Value
- .Selection.MoveDown Unit:=wdLine, Count:=1
-
- Sheets("6-1 土建投资").Range("O" & findnum & ":P" & findnum).Copy
- .Activate
- .Selection.PasteExcelTable False, False, False
-
-
-
-
- .Selection.Style = .ActiveDocument.Styles("标题 3")
- .Selection.TypeText Text:="工艺技术参数"
- .Selection.TypeParagraph
-
- For k = 2 To totalcol Step 2
-
- Excel.Application.Sheets("工艺单元参数设计").Activate
-
- Sheets("工艺单元参数设计").Range(Cells(i, k), Cells(i, k + 1)).Copy
- .Activate
- .Selection.PasteExcelTable False, False, False
-
- Next
-
-
-
-
-
- .Selection.Style = .ActiveDocument.Styles("标题 3")
- .Selection.TypeText Text:="主要设备参数"
- .Selection.TypeParagraph
-
-
-
-
- val2 = Excel.Application.Sheets("设备选型表").Range("c" & i)
-
- j = 1
-
- colnum = Sheets("设备选型表").Range("ZZ" & i).End(xlToLeft).Column - 6
-
-
- .Selection.TypeText Text:="(" & j & ")" & val2 & " 数量:" & Sheets("设备选型表").Range("E" & i).Value & Sheets("设备选型表").Range("F" & i).Value '插入设备名称
-
- .Selection.TypeParagraph
-
-
-
-
- For m = 7 To colnum + 6 Step 2
-
- Excel.Application.Sheets("设备选型表").Activate
-
- Sheets("设备选型表").Range(Cells(i, m), Cells(i, m + 1)).Copy
-
-
- .Activate
- .Selection.PasteExcelTable False, False, False
-
-
- Next
-
- End With
-
-
-
- Else
-
-
- With wApp.Application
-
- .Activate '激活WORD软件
-
- .Selection.EndKey Unit:=wdStory '定位至末尾行
-
- val2 = Excel.Application.Sheets("设备选型表").Range("c" & i)
-
- wApp.ActiveWindow.Selection.TypeText Text:="(" & j & ")" & val2 & " 数量:" & Range("E" & i).Value & Range("F" & i).Value '插入设备名称
- .Selection.TypeParagraph
-
- End With
-
- ' Excel.Application.Sheets("设备选型表").Activate
-
- colnum = Range("ZZ" & i).End(xlToLeft).Column - 6
-
- For k = 7 To colnum + 6 Step 2
-
-
- Sheets("设备选型表").Range(Cells(i, k), Cells(i, k + 1)).Copy
-
- wApp.Application.Selection.PasteExcelTable False, False, False
-
- Next
- End If
-
- oldval = val1
-
- j = j + 1
-
- Next
-
-
- '—————— 调整表格格式————————
-
- Dim mytable As Table
-
- For Each mytable In wDoc.Tables
- With mytable
- .Style = "Sheet Style"
复制代码
|
-
|