以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  使用vba 生成的word报表总是会多一行空行  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=149670)

--  作者:nuonuo384
--  发布时间:2020/5/9 1:40:00
--  使用vba 生成的word报表总是会多一行空行
老师:
我的问题有三个:

1:我按照下面的代码生成的报表总是会多一行空行,怎么删除?  
2:如下的代码有多个,如何封装到函数里面,通过传递参数调用。我尝试了传递 doc,
代码实现不了,是不是还要传递app? 如果要传递怎么传递呢?
3:每次生成的报表是直接在模板的基础上填充了数据,如何每次生成的都是单独的另外一张表,而不会覆盖模板或者历史生成的报表呢?


谢谢老师解答疑问!!

Dim app As New MSWord.Application
try
    Dim doc = app.Documents.Open("G:\\系统\\Attachments\\周计划汇总.doc")
Functions.Execute("各部门周报","IT","IT信息技术部")
\'\'--------------------------------IT信息部----------------------------------------------------------------------------------------
\'\'--------------------------------IT信息部----------------------------------------------------------------------------------------
If app.ActiveWindow.Selection.Find.Execute("IT已完成") = True Then
        Dim drs As List(Of DataRow) = DataTables("计划汇总_Table计划汇总").Select("部门 = \'IT信息技术部\' and 状态 = \'已完成\'")
        Dim li() As String = {"序号","实施项目","责任人","管理项目","目标值","计划开始时间","计划完成时间","完成情况","计划来源"}
        doc.Tables.Add(Range:=app.Selection.Range,NumRows:=1, NumColumns:= li.Length)
        With app.Selection.Tables(1)
            .ApplyStyleHeadingRows = True
            .ApplyStyleLastRow = True
            .ApplyStyleFirstColumn = True
            .ApplyStyleLastColumn = True
        End With
        For Each dc As String In li
            app.Selection.TypeText(Text:=dc)
            app.Selection.MoveRight(Unit:=12)
        Next
        Dim i As Integer = 1
        For Each dr As DataRow In drs
            For Each dc As String In li
                If dc = "序号" Then
                    app.Selection.TypeText(Text:= CStr(i))
                ElseIf dc <> "序号" Then
                    app.Selection.TypeText(Text:=dr(dc) )
                End If
                app.Selection.MoveRight(Unit:=12)
                i = i + 1
            Next
        Next
    End If



图片点击可在新窗口打开查看此主题相关图片如下:8f5e1edd-49aa-47eb-bab8-e3fb8792a29e.png
图片点击可在新窗口打开查看

[此贴子已经被作者于2020/5/9 1:44:03编辑过]

--  作者:有点蓝
--  发布时间:2020/5/9 9:28:00
--  
1、        For Each dr As DataRow In drs
            For Each dc As String In li
                If dc = "序号" Then
                    app.Selection.TypeText(Text:= CStr(i))
                ElseIf dc <> "序号" Then
                    app.Selection.TypeText(Text:=dr(dc) )
                End If
if i < drs.count
                app.Selection.MoveRight(Unit:=12)
end if
                i = i + 1
            Next
        Next
2、传递模板文件路径、表格数据如drs、表格列名如li
3、使用另存saveas
doc.SaveAs("c:\\123.doc")