Excel中的Word.GetAddress / "Check Names"对话框显示在后台



我在用Word。GetAddress函数在Excel文档中检索第一个&如果他在GAL中,请告诉他的姓。

据我所知,拥有内置"检查名称"对话框的唯一方法是使用Word。GetAddress函数。

当输入的名称匹配多个条目时,"检查名称"显示,但在中显示背景。我必须按Alt+Tab键才能看到

我曾尝试使用"激活"功能或"WindowsState"属性将其前置,但我被卡住了…

Function getFirstAndLastNames(pName As String) As String
    Dim oWord As Word.Application
    Dim strAddress  As String
    On Error GoTo getFirstAndLastNames_Error
    'If the search doesn't work, returns the argument
    getFirstAndLastNames = pName
    'Create the Word object to use GetAddress
    Set oWord = CreateObject("Word.Application")
    'Search
    strAddress = oWord.GetAddress(Name:=pName, CheckNamesDialog:=True, AddressProperties:="<PR_GIVEN_NAME> <PR_SURNAME>")
    'If there is a result, the function returns it
    If strAddress <> "" Then getFirstAndLastNames = strAddress
    'Quit Word
    oWord.Quit
    Set oWord = Nothing
    On Error GoTo 0
    Exit Function
getFirstAndLastNames_Error:
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure getFirstAndLastNames of Module Test"
    If Not (oWord Is Nothing) Then oWord.Quit
End Function

我看过这篇文章,那里有一个类似的问题解决了,但它没有说如何…

提前感谢您的帮助。

对另一篇文章的编辑没有说明他们找到了解决框不出现在前面的方法;只能使用alt-tab键将它放到最前面。

你有一个死锁,你的Excel代码停止了,等待Word,你需要一个动作把Word(或者更确切地说是Outlook)窗口带到前面,这样用户才能找到它。

你可以最小化并恢复Excel窗口,但它是拼凑的,如果屏幕上有其他窗口,那么它将不可靠,因为你需要的对话框也将隐藏在那些窗口后面。

你需要做的是有点难看,但将工作。这是有一个助手脚本或应用程序,您可以使用Application.Run异步启动,这将启动应用程序并继续在VBA中执行。该脚本/应用程序将等待一段时间(给VBA时间运行GetAddress行),然后使用windows API将该对话框带到前面。

大多数脚本或编程语言都足够好,选择哪一种取决于你最熟悉的语言。StackOverflow有一个Powershell的例子,你可以根据自己的需要进行调整。

最后,我找到了一篇关于Microsoft.com支持的文章,解释了如何在Word之外使用CheckSpelling。我使它适合我的使用。

代码将Word窗口置于屏幕之外,但对话框出现在前台。

Function getFirstAndLastNames(pName As String) As String
    Dim oWord As Word.Application
    Dim strAddress  As String
    Dim lOrigTop As Long
    Dim lOrigState As Byte
    'Display the "Check names" dialog (available only with Word.Application ...)
    On Error GoTo getFirstAndLastNames_Error
    'If the search doesn't work, returns the argument
    getFirstAndLastNames = pName
    'Create the Word object to use GetAddress
    Set oWord = CreateObject("Word.Application")
    'Position Word off screen to avoid having document visible
    'http://support.microsoft.com/kb/243844/en-us
    lOrigTop = oWord.Top
    lOrigState = oWord.WindowState
    oWord.Top = -3000
    oWord.Visible = True
    oWord.WindowState = wdWindowStateMinimize
    oWord.Activate

    'Search
    strAddress = oWord.GetAddress(Name:=pName, CheckNamesDialog:=True, AddressProperties:="<PR_GIVEN_NAME> <PR_SURNAME>")
    'If there is a result, the function returns it
    If strAddress <> "" Then getFirstAndLastNames = strAddress
    'Reset the position and state of Word and quit the application
    oWord.Visible = False
    oWord.Top = lOrigTop
    oWord.WindowState = lOrigState
    oWord.Quit
    Set oWord = Nothing
    On Error GoTo 0
    Exit Function
getFirstAndLastNames_Error:
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure getFirstAndLastNames of Module Test"
    'If an error raised, Reset the position and state of Word and quit the application
    If Not (oWord Is Nothing) Then
        oWord.Top = lOrigTop
        oWord.WindowState = lOrigState
        oWord.Quit
    End If
End Function

最新更新