【问题标题】:Open newly added document打开新添加的文档
【发布时间】:2014-05-05 15:34:53
【问题描述】:

使用 Windows XP、Office 2007 和 Microsoft 自定义 UI 编辑器构建插件。

我正在创建一个加载项,它只是打开一些预定义的单词 .dotx 模板。但是我遇到了一个奇怪的问题。

新的word文档根本不会激活。我认为这是因为每个用户要么使用两台或三台显示器,但我不确定如何在 VBA 中处理这样的设置。

另外,只是有时新文档不会激活。每当代码中出现用户表单时,它似乎都会出错。

激活意味着用户看到的是“旧”文档,而新打开的文档没有获得焦点。

无论如何,这是无法让 i 工作的代码:

Private Sub btnOK_Click()

If Me.chbxFigures = False Then
    If Me.obtnDanish Then
        Set newDoc = Documents.Add(Template:="path", NewTemplate:=False, DocumentType:=0, Visible:=True)
    ElseIf Me.obtnEnglish Then
        Set newDoc = Documents.Add(Template:="path", NewTemplate:=False, DocumentType:=0, Visible:=True)
    End If
ElseIf Me.chbxFigures Then
    If Me.obtnDanish Then
        Set newDoc = Documents.Add(Template:="path", NewTemplate:=False, DocumentType:=0, Visible:=True)
    ElseIf Me.obtnEnglish Then
        Set newDoc = Documents.Add(Template:="path", NewTemplate:=False, DocumentType:=0, Visible:=True)
    End If
End If

'Documents.Add.ActiveWindow.Panes(2).Activate
newDoc.ActiveWindow.Visible = True
newDoc.ActiveWindow.Activate
newDoc.Activate
Set newDoc = Nothing

Unload Me

End Sub

而且这段代码似乎工作正常:

Sub LoadSC(control As IRibbonControl)
    Set newDoc = Documents.Add(Template:="path", NewTemplate:=False, DocumentType:=0, Visible:=True)
    newDoc.Activate
    newDoc.ActiveWindow.Visible = True
    newDoc.ActiveWindow.Activate
    newDoc.ActiveWindow.ActivePane.Activate
    Set newDoc = Nothing
End Sub

Google 已经两天没有给出答案了,所以我希望你们中的一些人可以引导我朝着有用的方向前进。

【问题讨论】:

  • 但是你的问题是什么?我知道您有问题,但您也提供了解决方案,所以...使用看起来不错的代码。
  • 您的表单是ShowModal = True 吗?您可以将Unload Me 移动到newDoc.ActiveWindow.Visible = True 上方。 unload Me 后的代码仍然会被执行。
  • @PatricK 在 newDoc.ActiveWindow.Visible = True 成功之前移动卸载我!谢谢 - 不知道我能否将您的评论标记为答案?

标签: vba ms-word


【解决方案1】:

根据评论和确认,将Unload Me 移到newDoc.ActiveWindow.Visible = True 行上方将解决此问题 - 可能表单正在停止启动新文档。

在 VBA 中,代码仍会执行到 End Sub/FunctionExit Sub/Function

【讨论】:

    猜你喜欢
    • 2015-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    相关资源
    最近更新 更多