首页 > 解决方案 > VBA WordDoc 表的第二次运行添加未设置变量

问题描述

我在 Excel 中有一个打开 word.app 的 VBA 例程。除了一件事之外,所有设置的变量在多次运行中都可以正常工作。我希望你能帮帮我!

我认为的相关代码:

Dim WordApp As Word.Application
Dim WordDoc As Word.Document

'load format
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True
Set WordDoc = WordApp.Documents.Add(Template:=FormatLocation & FormatFile)

Call MakeTableFields(OHIBSingleEntity, WordDoc)

WordDoc.SaveAs FileName:=SaveFile, FileFormat:=wdFormatXMLDocument, AddtoRecentFiles:=False
WordDoc.Close

WordApp.Quit

Set OHIBSingleEntity = Nothing
Set WordDoc = Nothing
Set WordApp = Nothing

-----------------------------------------------------------------------------

Private Sub MakeTableFields(EntityFields As Recordset, WordDoc As Word.Document)
'make table sub
Dim PropTbl As Word.Table
Dim RangeCT As Word.Range

    Set RangeCT = WordDoc.Content
    With RangeCT.Find
        .Text = "#InvoegenTabelISB"
        .Format = False
        .Wrap = wdFindContinue
        .MatchWildcards = False
        .Execute
    End With
    
    
    Recordcounter = EntityFields.RecordCount
    
    'Problem is here:
    Set PropTbl = WordDoc.Tables.Add(RangeCT, RoundUp(Recordcounter / 2, 0), 4)

    'after this formatting and filling the just created table with the EntityFields dataset

    Set RangeCT = Nothing
    Set PropTbl = Nothing

End Sub

第一次运行它应该正常工作。第二次运行时,除了格式化和填充表格外,一切正常。据我所知,它看起来Set PropTbl不起作用。创建表工作。但下一行失败/跳过。当我用这个 VBA 关闭 Excel 并再次打开它时,它在第一次运行时再次正常工作。第二次运行再次失败。

我尝试的是制作桌子,然后Set PropTbl

WordDoc.Tables.Add RangeCT, RoundUp(Recordcounter / 2, 0), 4
Set PropTbl = WordDoc.Tables(1)

这也给出了相同的行为。在保持运行excel的同时,它几乎看起来像是在内存中保留了一些东西。但是我不知道如何在所有 VBA 例程都执行和完成时调试内存。

有谁知道如何修复或调试这个?

标签: excelvbams-word

解决方案


真的很奇怪,但错误不在发布的代码中

这确实适用于第一次运行,但第二次失败:

    With PropTbl.Borders
        .LineStyle = Options.DefaultBorderLineStyle
        .LineWidth = Options.DefaultBorderLineWidth
        .Color = Options.DefaultBorderColor
    End With
    With PropTbl.Borders(wdBorderLeft)
        .LineStyle = Options.DefaultBorderLineStyle
        .LineWidth = Options.DefaultBorderLineWidth
        .Color = Options.DefaultBorderColor
    End With
    With PropTbl.Borders(wdBorderBottom)
        .LineStyle = Options.DefaultBorderLineStyle
        .LineWidth = Options.DefaultBorderLineWidth
        .Color = Options.DefaultBorderColor
    End With
    With PropTbl.Borders(wdBorderRight)
        .LineStyle = Options.DefaultBorderLineStyle
        .LineWidth = Options.DefaultBorderLineWidth
        .Color = Options.DefaultBorderColor
    End With
    With PropTbl.Borders(wdBorderVertical)
        .LineStyle = Options.DefaultBorderLineStyle
        .LineWidth = Options.DefaultBorderLineWidth
        .Color = Options.DefaultBorderColor
    End With
    With PropTbl.Borders(wdBorderHorizontal)
        .LineStyle = Options.DefaultBorderLineStyle
        .LineWidth = Options.DefaultBorderLineWidth
        .Color = Options.DefaultBorderColor
    End With

修复很简单:

PropTbl.Borders.Enable = True

给出相同的结果。奇怪的是它只是失败而没有错误。我得到的唯一提示是悬停提示,Options.DefaultBorderLineStyle提示服务器计算机无法访问......?

总之,解决了。


推荐阅读