vba Workbooks.OpenText 不采用“fieldinfo”参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9559579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 15:26:58  来源:igfitidea点击:

Workbooks.OpenText doesn't take 'fieldinfo' parameter

vbams-officeparameter-passingexcel-2003

提问by nixda

The following code demonstrates the problem which is stated in questions title.

以下代码演示了问题标题中所述的问题。

Copy & paste it in a new Microsoft Excel 2003 workbook.

将其复制并粘贴到新的 Microsoft Excel 2003 工作簿中。

Sub mytest()
    mypath = Application.GetSaveAsFilename()

    Workbooks.OpenText Filename:=mypath, DataType:=xlDelimited, _ 
        TextQualifier:=xlTextQualifierDoubleQuote, _ 
        semicolon:=True, _ 
        fieldinfo:=Array(Array(1, 2), Array(2, 2), Array(3, 2), Array(4, 2)), _ 
        Local:=True
End Sub

Execute the code. It will ask for an input file where you should use this semicolon-seperated test.csv. It creates a new workbook and imports all the data from test.csv to sheet1.

执行代码。它将要求输入文件,您应该在其中使用此分号分隔的test.csv。它创建一个新工作簿并将所有数据从 test.csv 导入到 sheet1。

The picture below shows the result
enter image description here

下图显示了结果
在此处输入图片说明

But It should have shown a result like this
enter image description here

但它应该显示这样的结果
在此处输入图片说明



fieldinfo:=Array(Array(1, 2), Array(2, 2), Array(3, 2), Array(4, 2))
states that Excel should treat all imported data as text. Unfortunately it doesn't.

fieldinfo:=Array(Array(1, 2), Array(2, 2), Array(3, 2), Array(4, 2))
指出 Excel 应将所有导入的数据视为文本。不幸的是它没有。

Can someone show me how to use opentext and fieldinfo in the correct way?

有人可以告诉我如何以正确的方式使用 opentext 和 fieldinfo 吗?

I already know the work-around with QueryTables.Add(Connection[...].
But thats not a solution for my case.

我已经知道 QueryTables.Add(Connection[...]
的解决方法。但这不是我的情况的解决方案。

采纳答案by nixda

If you rename the extension from the input file from .csv to .txt the fieldinfoparameter works as designed.

如果您将输入文件中的扩展名从 .csv 重命名为 .txt,则fieldinfo参数按设计工作。