2013-09-25 26 views
0

我的第一篇文章在这个伟大的Q &一个网站,我希望有人能帮助我。在VBA GUI程序中包含可执行脚本

以DOS格式(script.exe)运行的exe格式的脚本。这个脚本需要一个输入文件来处理,所以在DOS 我需要做“Script.exe输入文件> ouputfile.txt”。

可以在VBA中用几个按钮创建一个简单的GUI,并在VBA应用程序中插入 script.exe?

在可执行的VBA GUI中可以有script.exe文件吗?

我想什么来实现的是有一个文本,要求输入文件和一个按钮来选择要处理 的文件,并且VBA程序运行script.exe的GUI。

+1

VBA不会创建独立的应用程序。 VBA只能在Office包内运行,并且不会创建任何可执行文件。您需要下载VS2012 Express for Desktop(免费)并构建Windows窗体应用程序以实现您想要的功能。 – 2013-09-26 07:51:40

回答

1

您无法使用VBA创建独立的.exe文件。你可能会想到而不是

您必须在Office应用程序(通常为Excel/Word/Access/Powerpoint/Outlook)中制作VBA程序。

要做到这一点:

  1. 创建一个提交按钮
  2. 名称提交按钮“cbSubmit”
  3. 下面的代码添加到与用户窗体

    Private Sub cbSubmit_Click() 
    
    Dim myCommand As String 
    Dim myInputArg As String 
    Dim myFile As Office.FileDialog 
    Set myFile = Application.FileDialog(msoFileDialogFilePicker) 
    
    With myFile 
    
        .AllowMultiSelect = False 
    
        ' Set the title of the dialog box. 
        .Title = "Please select the file." 
    
        ' Clear out the current filters, and add our own. 
        .Filters.Clear 
        .Filters.Add "All Files", "*.*" 
    
        ' Show the dialog box. If the .Show method returns True, the 
        ' user picked at least one file. If the .Show method returns 
        ' False, the user clicked Cancel. 
        If .Show = True Then 
        myInputArg = .SelectedItems(1) 'replace txtFileName with your textbox 
    
        End If 
    End With 
    
    'don't continue if user doesn't select something 
    If myInputArg = "" Then Exit Sub 
    'construct shell command 
    myCommand = "Script.exe" & myInputArg & " > ouputfile.txt" 
    
    'run shell command 
    Dim returnVal As Double 
    returnVal = Shell(myCommand, vbHide) 
    
    End Sub 
    
    相关的代码一个用户窗体
+0

谢谢你的帮助。我会尝试你分享我的代码。 一旦我能够创建GUI界面并使用代码,在VB程序的编译 的时刻使其可执行,script.exe文件将驻留在创建的exe程序 通过VBA?我的意思是script.exe将成为由VBA生成的最终.exe文件的一部分? 或者我该怎么做,我的目标是提供一个GUI到script.exe程序,但最终的应用程序在 单个.exe。 再次感谢您的帮助。 – Zurix

+0

@Zurix VBA不会生成可执行文件,所以你建议不会工作。我建议研究一下VBA来澄清你所问的问题。 – enderland

+0

你好恩德兰,那么,以Visual Studio为例,我可以将VBA项目转换为可执行文件?分辩? 也许我做了错误的问题。我的意思是,如何在单个exe程序中包含VBA代码和script.exe?再次感谢 – Zurix