访问VBA以获取文件输入和输出 [英] Access VBA for File Input and Output

查看:154
本文介绍了访问VBA以获取文件输入和输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

位于不同表单上的是上传按钮。 上传按钮会打开文件对话框。 问题是获取文件对话框以允许选择一个文件并将其保存在另一个目录中。 


理想情况下,保存目录是从文本文件中读取的,可以根据用户要求进行更改。 这就是为什么输出目录没有编码到VBA中。


问题是如何选择一个文件并将其输出到另一个目录,同时从文本文件中读取该目录。


提前致谢。



解决方案

这很可能涉及使用以下元素:


    Application.FileDialog属性

    FileCopy声明

    FreeFile功能

   公开声明

    关闭声明

   输入#语句或行输入语句


在联机帮助中没有很好地描述VBA I / O,您应该在其中查找所有其他元素。 用于读取文本文件的特定VBA I / O语句将取决于文件中目录路径的格式。 如果路径在文件中占用
一行,并且这是文件中的第一行或唯一一行,那么代码可能如下所示:

 Dim intFileNo As Integer 
Dim strCopyToDirectory As String

intFileNo = FreeFile()
打开" C:\Your path \To \YourTextFile。 TXT"输入为#intFileNo
行输入#intFileNo,strCopyToDirectory
关闭#intFileNo


Located on different forms is an upload button.  The upload button opens a file dialog.  The problem is getting the file dialog to allow selection of one file and saving it in another directory. 

Ideally the save directory is read from a text file, which may be changed depending on user requirements.  This is why the output directory is not coded into the VBA.

The question is how to select a file and output it to a different directory meanwhile reading that directory from a text file.

Thanks in advance.

解决方案

This would most likely involve the use of the following elements:

    Application.FileDialog property
    FileCopy statement
    FreeFile function
    Open statement
    Close statement
    Input # statement or Line Input statement

VBA I/O isn't awfully well described in the online help, where you should look for all the other elements.  The specific VBA I/O statement to use to read the text file would depend on the format of the directory path in the file.  If the path occupies one single line in the file, and that is the first or only line in the file, then the code could look something like this:

Dim intFileNo           As Integer
Dim strCopyToDirectory  As String

intFileNo = FreeFile()
Open "C:\Your path\To\YourTextFile.txt" For Input As #intFileNo
Line Input #intFileNo, strCopyToDirectory
Close #intFileNo


这篇关于访问VBA以获取文件输入和输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆