【问题标题】:Microsoft access vba-shift focus from access to word微软访问vba-将焦点从访问word转移
【发布时间】:2014-08-18 10:49:24
【问题描述】:

我正在通过 access 2007 vba 创建和打开一个 word 文档。文档已创建,但焦点未转移到 word。相反,重点仍然放在创建文档的访问表单上。以下是我的代码:

Dim obj As Word.Application
Dim wor As Word.Document
Dim str As String

str = "C:\hello\folder1\vin.dot"

Set obj = CreateObject("Word.Application")
Set wor = obj.Documents.Add

With wor

 .SaveAs str

 .Close
End With


obj.Visible = True
obj.Documents.Open str

obj.WindowState = wdWindowStateMaximize

请大家多多指教。

【问题讨论】:

    标签: ms-access vba


    【解决方案1】:

    您可以使用 AppActivate 移动焦点;

    AppActivate "Microsoft Word"
    

    【讨论】:

    【解决方案2】:

    VBA Word.Application 对象可能有多个与之关联的窗口。您可以使用 Caption 属性指定要显示的窗口。

    由于您创建了一个新的 Word.Application 并且只创建了一个 Document,您可能会假设第一个元素(基于 1 的数组)是您想要在代码中显示的内容。

    AppActivate (obj.Windows(1).Caption)
    

    【讨论】:

      【解决方案3】:

      即使打开其他 word 文档,我也能够调用特定窗口:

      Private Sub but_Click()
          'must add tools > references Microsoft Word XX.X Object Library (xx.x is max update)
          Dim WordApp As Word.Application
          Dim WordDoc As Word.Document
          strProjPath = Application.CurrentProject.Path 'get current path
          strVerWord = "test" 'i use this to identify paths that can change based on network configs.
          posOp = InStr(1, strProjPath, strVerWord) ' find the verification word in the path
          strVerPath = "strVerWord filepath\FP\FP\document.docx" 'create string location for document
          strPathPlusDBName = Left(strProjPath, posOp - 1) & strVerPath
          Set WordApp = CreateObject("Word.Application")
          WordApp.Visible = True
          Set WordDoc = WordApp.Documents.Open(FileName:=strPathPlusDBName, ReadOnly:=True)  'open word file in read only
          AppActivate WordDoc
      End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多