使用VBA创建单词标签表,获取额外的不需要文档



我正在尝试使用访问中提供的数据在Word中创建标签表单。此VBA代码有效:

Dim appwd As Object
Dim oDoc As Object
Set appwd = CreateObject("Word.Application")
With appwd
    .Documents.Add
    Set oDoc = .MailingLabel.CreateNewDocumentByID(LabelID:="1359804671")
    .Visible = True
    .Activate
End With
oDoc.Activate
'Remaining code that creates labels

但是,它创建并打开空白文档1以及我想要的labels2文档。我如何防止其创建不需要的文档1,或者至少在不保存的情况下再次关闭该文档?

如果我评论了.Documents.Add,那么我得到

运行时错误'4605':

由于文档窗口不活动,因此无法使用此方法或属性。

不幸的是, .MailingLabel对象需要一个文档打开,因此最好的选择是在创建所需的一个文档后立即关闭该文档:

Dim appwd As Object
Dim oDoc As Object
Set appwd = CreateObject("Word.Application")
With appwd
    .Documents.Add
    Set oDoc = .MailingLabel.CreateNewDocumentByID(LabelID:="1359804671")
    .Documents(1).Close SaveChanges:=0 'wdDoNotSaveChanges, close the first document
    .Visible = True
    .Activate
End With
oDoc.Activate

最新更新