excel - 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 例程都执行和完成时调试内存。
有谁知道如何修复或调试这个?
解决方案
真的很奇怪,但错误不在发布的代码中
这确实适用于第一次运行,但第二次失败:
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
提示服务器计算机无法访问......?
总之,解决了。
推荐阅读
- entity-framework-core - EF Core 5.0 - 无法翻译 linq 表达式。要么重写查询
- reactjs - 如何使用 React Spring 创建抛物线动画
- flask - gremlinpython client.submitAsync() 在作为应用服务部署到 Azure 时失败
- erlang - 如何将命令行参数传递给构建的 Mix Release
- vue.js - Vue-Plyr 加载视频很慢
- windows - TensorFlow 在 Windows 上联合
- ios - 在 Swift 中根据 API 调用设置前景色
- rust - 无法分配给“self.input.cached_token”,因为它是借来的
- bash - 使用换行符或不使用换行符打印字符串序列
- node.js - VueJS 3/路由器/使用推送重定向:未捕获(承诺)类型错误:无法读取未定义的属性(读取“推送”)