Is it possible to deploy a VB application made in

2019-09-13 22:22发布

问题:

Basically wondering if there's a way for me to create a VB application in Excel and have it run without a full version of MS Office. The VB application would load an Excel sheet that would import a CSV onload, then print a PDF of the sheet and close.

If you have any questions, let me know.

回答1:

No. Not without converting to a standalone application.

If you had were familiar with VB6 (and had access to it; it's no longer for sale), you could create a VB6 app. that references the excel COM components (still need to be installed on each target PC).

Otherwise, build an app. using VB.NET and use Office VSTO 2010 (need to reference the Office PIAs)

How to: Target Office Applications Through Primary Interop Assemblies



回答2:

Just a little conflict. In office, you code with VBA, which is different than VB. What you would need to do is create a VB app that uses excel libraries or something to do some meaningful work.



回答3:

The short answer is no.

You could write an external visual basic script that calls in to office and opens excel using some excel libraries, if memory serves me correctly however - you'd still require office installed on this machine. (Unfortunately I can't find a link at the moment to back this up)

Your best bet is to parse the CSV data yourself and generate a PDF from that.

There is some information here: http://www.rlmueller.net/ReadCSV.htm on how to Read CSV data using VBS (to get the examples to run, you should simply have to rename the .txt to .vbs and double click it.)

I'll leave you to find out how you'd then generate the PDF.

I don't think however this is the best solution to your problem - a full .NET application or perhaps some Python would likely serve you better.



回答4:

Code will go in several different places, "ThisWorkbook" object and the "UserForm" code.

"ThisWorkbook" contains code that will determine if the UserForm is the only Excel thing (workbook) open, and if it is it will hide the Excel application and hide the workbook itself. and if other workbooks are open it simply hides the workbook. I have it set to hide the application and the workbook in both cases so that a new instance of Excel can be opened after the UserForm is running without pulling up the workbook associated with the UserForm. The code for this is below (goes into the "ThisWorkbook" object):

Private Sub WorkBook_Open()
  Dim wb As Workbook
     Set wb = Workbooks("MyBook.xlsm")
    If Workbooks.Count > 1 Then 

       wb.Windows(1).Visible = False
    Else
       wb.Windows(1).Visible = False
       Application.Visible = False
    End If
   UserForm1.Show vbModeless
   'Put defaults and populations here
End Sub

The UserForm1.Show vbModelessallows for Excel to be used while the UserForm is active. A couple of notes on this section:

  • "UserForm1" is the name of my UserForm, change this to the name of yours
  • Where I Set wb = Workbooks("") change inside the quotes to the name of the workbook the UserForm is in
  • The IfElse statement could be eliminated and moved to the If, if you don't need any other action on the opening with no other workbooks open

The next section of code goes in the UserForm Code. I have a button set up to show the Excel workbook in order to edit it and whatnot, you could have a region you click if you don't want a button to show up. When you want to activate the Excel sheet and the application will need to be activated. I unload (deactivate) the active thing (the UserForm). This bit of code isn't necessary if the user doesn't need access to the spreadsheet:

Private Sub See_Excel_Click()

   Dim wb As Workbook
   Set wb = Workbooks("MyBook.xlsm")
   wb.Windows(1).Visible = True
   Application.Visible = True
   wb.Sheets("Sheet1").Activate

   Unload Me

End Sub

Within the userform there should be a way to handle what happens when the userform is closed, as the excel application and workbook will stay open hidden in the background. I have the action close the workbook and the application. A quick note, if you set the Cancel = True then the red x button won't close the userform. The code I use for this is:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)

    If CloseMode = 0 Then
      Cancel = False
      Dim wb As Workbook
      Set wb = Workbooks("MyBook.xlsm")
      wb.Windows(1).Visible = True
      Application.Visible = True
      ThisWorkbook.Saved = True
      ThisWorkbook.Activate

     If Workbooks.Count > 1 Then
          ActiveWorkbook.Close
       Else
          Application.Quit
     End If

    End If

End Sub

That is it for the code that goes inside the UserForm. And the code that is necessary to have the UserForm in VBA act as it's own application while allowing for Excel to operate normally at the same time as the UserForm.

To summarize what happens:

  1. When the Workbook is launched the workbook is hidden, and if no other workbook is open the the Excel application is hidden.
  2. The UserForm is initiated to allow for Excel to be used at the same time
  3. When the spreadsheet is activated again excel is re-enabled and the application and un-hide the worksheet
  4. When the user form is closed, the workbook is closed, and if there are no other workbooks the excel application is closed
  5. If you set defaults or populate ComboBoxes put them in the "WorkBook" object code.