Visual Basic in VS 2013 and Excel 2010: cannot find xlQualityStandard (is not declared) - vb.net

I am trying to code some Excel automation stuff using VB in VS2013. Basically, I can perform a lot of excel operation from my code (even applying the data analysis tool, and creating histograms).
However, in my case, Visual Studio is complaining 'xlQualityStandard' is not declared.
Here is a sample of my code
Imports Microsoft.Office
Imports Excel = Microsoft.Office.Interop.Excel
' do something
xlTempSheet.ExportAsFixedFormat(Type:=Excel.XlFixedFormatType.xlTypePDF, Filename:="c:\plots test\test.pdf",
IgnorePrintAreas:=True, OpenAfterPublish:=True, Quality:=xlQualityStandard)
If I remove
Quality:=xlQualityStandard
The code will just work.

In my case,
Excel.XlFixedFormatQuality.xlQualityStandard
will work. Thanks to user2930100

The values can be found here
https://msdn.microsoft.com/en-us/library/bb241292(v=office.12).aspx
xlQualityMinimum 1 Minimum quality
xlQualityStandard 0 Standard quality
Interestingly Google now seems to provide API help

Related

Import source from txt or bas file

I want to store source code of VBA macro in .bas or .txt file and run it when user runs macro. I have macro that is used by multiple people, and I would like to store file on server to prevent them to use older versions of same macro. I found following line in stackoverflow and placed it in module code that should import code
'Library should be turned on Microsoft Visual Basic for Applications Extensibility 5.3
Option Explicit
Sub main()
Dim VBPrj As VBIDE.VBProject
Dim VBCom As VBIDE.VBComponent
Set VBPrj = Application.VBE.ActiveVBProject
Set VBCom = VBPrj.VBComponents("Module1")
VBCom.CodeModule.AddFromFile ("C:\Users\lietu\OneDrive\Documents\tests\Module1.txt")
End Sub
then I created txt file with following code in right location
Attribute VB_Name = "Module1"
Sub main()
MsgBox "Hello World"
End Sub
What I'm doing wrong?
It is not possible to execute code in a text file as a VBA "macro". The programming language/environment/interface simply does not work that way - no ifs, ands or buts - no workarounds.
The content of a text or bas file must be imported into a VBA project, using code similar to what's in the question.
In order to be able to use the VB Extensibility libraries that this code depends on, a specific security setting in the host Office application must be disabled, making this kind of approach unreliable, at best. The setting cannot be disabled using code, for understandable reasons.
Indeed, the approach proposed in the question would be a massive security risk...

save datagrid as excel 2003 or excel 2007 using vb.net

i can save a datagrid using the vb.net code:
saveFileDialog.Filter = "Excel2007 (*.xlsx)|*.xlsx|All files (*.*)|*.*"
i wish to give the user the option to save in either Excel2003 or Excel2007 format. What changes do i need to make in the code and what about references and imports? thanks...
Additionally to the adequate extension, you have to provide the Excel version when calling SaveAs():
curBook.SaveAs("full path with adequate extension", Excel.XlFileFormat.xlWorkbookDefault)
You can find the list of values for XlFileFormat here. Although the names in this list are not too descriptive. Here you have a translation:
2003 -> Excel.XlFileFormat.xlExcel8 (as explained here)
2007 -> Excel.XlFileFormat.xlOpenXMLWorkbook (as explained here)
NOTE: bear in mind that the code above relies on Office Interop.

Datasource or Linq Result to excel with VB.Net

i have found alot of sh*** right now. But not the things that im looking for.
I need to export a LINQ query (or the Datasource of my DataGridview)
to an existing Excel Spreadsheet. Lets Say after Cell A25
I have a Windows Form application using LINQ for the Databindings.
Any Suggestions or good Examples?
Thanks in Advance
I would recommend to use EPPlus because it's simple, powerful and works without having office/excel being installed with Excel 2007 spreadsheets(xlsx-files). It's license model is GPL.
Dim excel As New ExcelPackage
excel.File = New IO.FileInfo("C:\Temp\AnExcelFile.xlsx")
If excel.File.Exists Then excel.Load(excel.File.Open(FileMode.Open))
Dim ws As ExcelWorksheet = excel.Workbook.Worksheets.Add("Worksheet-Name")'must be unique and less than 31 characters long'
ws.Cells(26, 1).LoadFromDataTable(dt, True) 'loading from DataTable, the 2.Parameter is PrintHeaders'
ws.Cells(26, 1).LoadFromCollection(query, True)'loading by LINQ-Query'
excel.Save()
I don't think that you can use it for xls-files without problems. Have a look what Jan said: http://epplus.codeplex.com/discussions/253371
No, only XLSX, but you can open it in Excel 2003 if you have the
compatibility pack installed. If you need XLS, have a look at the
NPOI project here on Codeplex.

How to add/Delete lines of code to a "ThisWorkbook" of excel file using vb.net?

As per my application I want to write some Lines code in "ThisWorkbook" of Excel file using vb.net,before that we need to check the file for existance of code.
Please let me know any code or links for reference..
thank you...
It's possible but it's also very likely that the user's macro security settings will prevent this from working initially.
To adjust the security settings (all examples for Excel 2003):
(from a workbook): Tools > Macro > Security > Trusted Publishers
You now need to check the box which says "Trust access to Visual Basic project"
To read the code:
(from the VBA editor): Tools > References and add "Microsoft Visual Basic For Applications Extensibility 5.3" (the actual file is VBE6EXT.OLB)
To work out which VBProject is which, use the FileName property:
For Each vbpItem In Application.VBE.VBProjects
If (vbpItem.FileName = "C:\foo.xls") Then
Set vbpProject = vbpItem
End If
Next vbpItem
Once you have the project, you can refer to the module by name:
vbpProject.VBComponents("ThisWorkbook")
and you can check how many lines there are like this:
If (vbpProject.VBComponents("ThisWorkbook").CodeModule.CountOfLines <> 147) Then
With the CodeModule object, you can read back specific lines (via the Lines property) and change lines (with the ReplaceLine method)
The only thing I can find that would do this is ThisWorkbook.VBProject.VBComponents.Count which counts the number of Modules in your VB solution, which is Sheets + ThisWorkbook + anything additional. I can't find anything that would let you do a diff of the code.
Are you trying to do a security check of some sort? If random code was being inserted into your workbooks, wouldn't a black hat delete this coded in check? Why not just use signatures and digitally sign it?

Need skeleton code to call Excel VBA from PythonWin

I need to invoke a VBA macro within an Excel workbook from a python script. Someone else has provided the Excel workbook with the macro. The macro grabs updated values from an external database, and performs some fairly complex massaging of the data. I need the results from this massaging, and I don't really want to duplicate this in my Python script, if I can avoid it. So, it would be great if I could just invoke the macro from my script, and grab the massaged results.
Everything I know about COM I learned from "Python Programming on Win32". Good book, but not enough for my task at hand. I searched, but haven't found any good examples on how to do this. Does anyone have any good examples, or perhaps some skeleton code of how to address/invoke the VBA macro? A general reference (book, web link, etc) on the Excel COM interfaces would also help here. Thanks.
OK, I got it! Thanks for the help on the Application.Run method. This info, plus the "Microsoft Excel Visual Basic Reference": http://msdn.microsoft.com/en-us/library/aa209782(office.10).aspx--as recommended by Hammond & Robinson in "Python Programming on Win32"--was what was needed.
Here's the skeleton code:
import win32com.client
xl=win32com.client.Dispatch("Excel.Application")
xl.Workbooks.Open(Filename="<your Excel File>",ReadOnly=1)
xl.Application.Run("<your macro name>")
#...access spreadsheet data...
xl.Workbooks(1).Close(SaveChanges=0)
xl.Application.Quit()
xl=0
I am sorry, I dont know python enough.
However, the following should help.
Excel's Application object has a Run method - which takes the name of the macro alongwith arguments to it.
Lets assume that the workbook has a macro named test.
Sub test(ByVal i As Integer)
MsgBox "hello world " & i
End Sub
You can call this using Application.Run "test", 1234
This will call the macro and show the messagebox with "hello world 1234".