【问题标题】:New window not in focus with VBScript新窗口未与 VBScript 成为焦点
【发布时间】:2014-05-03 01:47:48
【问题描述】:

我有一段简单的代码,它要求用户导航到 .csv 文件,它会计算列数(一个更大的项目的一部分),虽然它运行良好,但我真的有一个烦人的问题...当窗口打开提示用户导航到文件时,它不在焦点上,而是位于先前处于焦点的窗口后面。谁能指出哪里出了问题?

我正在使用运行 IE 9 版本 9.0.8112.16421 的 Windows 7 64 位

'Get location of .csv file

'set the type of dialog box you want to use: 1 = Open 2 = SaveAs 3 = File Picker 4 = Folder Picker
Const msoFileDialogOpen = 1

Set fso = CreateObject("Scripting.FileSystemObject")
Set objWord = CreateObject("Word.Application")
Set WshShell = CreateObject("WScript.Shell")
'Launch at default path
strInitialPath = WshShell.ExpandEnvironmentStrings("C:\")
objWord.ChangeFileOpenDirectory(strInitialPath)

With objWord.FileDialog(msoFileDialogOpen)
   .Title = "Select the file to process"
   .AllowMultiSelect = False
   .Filters.Clear
    .Filters.Add "CSVFiles", "*.csv"
   .Filters.Add "All Files", "*.*"               
   If .Show = -1 Then 
      For Each File in .SelectedItems 
        Set objFile = fso.GetFile(File) 
      Next 
   End If
End With
'Close Word
objWord.Quit

'Counts columns in csv file if delimiter is comma

Dim oFso, oReg, sData, lCount
Const ForReading = 1
Set oReg = New RegExp
Set oFso = CreateObject("Scripting.FileSystemObject")
sData = oFso.OpenTextFile(objFile.Path, ForReading).ReadLine
With oReg
.Global = True
'Check Pattern (delimiter, has qualifier?)
'.Pattern = ","
'.Pattern = "|"
'.Pattern = "^"
.Pattern = ""","""
'.Pattern = """|"""
'.Pattern = """^"""
lCount = .Execute(sData).Count + 1
End With
WScript.Echo lCount
Set oFso = Nothing
Set oReg = Nothing

谢谢大家!

【问题讨论】:

  • 我真的不太了解 Office VBA,但在我看来,您需要为您的 FileDialog 设置所有者窗口。

标签: vbscript


【解决方案1】:

你试过AppActivate吗?

CreateObject("WScript.Shell").AppActivate "Your window title here..."

【讨论】:

    猜你喜欢
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    • 2012-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多