2010 Access vba How to set displaygridlines in Excel to false? - ms-access

I'm trying to set displaygridlines = false in Access VBA. I started with Excel to record the macro on turning this off, which give the displaygridlines=false. But this does not work in Access VBA. The only reference I could find on the site was How can I turn off gridlines in excel using VBA, without using ActiveWindow but this is for Excel.
The "Method or data member not found" is the error when compiling.
Does anyone know how to turn off gridlines in Excel from Access VBA?

If I understand your question correctly, you want to open a (new?) workbook in Excel from an Access VBA script and then hide the Excel gridlines. This can be done with the following code:
Private Function xlWithoutGrids()
Dim xlApp As New Excel.Application
With xlApp
.Visible = True
.Workbooks.Add
.ActiveWindow.DisplayGridlines = False
End With
End Function
Make sure you have a reference to the Microsoft Excel Object library from your Access database (Tools > References).

Related

I want to export from Access to Excel a Table I created in Access without Access asking me if I want to overwrite the existing file

Is it possible without VBA? I can send the file the first time with no problem. I can manually go in and delete the file then run Access - no problems. I want Access to run and then overwrite the existing Excel file without asking me if I want it overwritten.
I do not think is possible without VBA.
Delete the file before exporting.
Dim strFile As String
strFile = "your filepath here"
If Dir(strFile) <> "" Then Kill(strFile)
'export code here

Problem Importing Excel File into Access via VBA

I've created a multi-user Access Form that will essentially be open for most of the day on several peoples computers. On this form, I've created a button that will import an excel spreadsheet via VBA. The code is:
Option Compare Database
Private Sub Command0_Click()
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE ExcelTable.* FROM ExcelTable;"
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, "ExcelTable", "C:\Users\JohnDoe\Documents\MyTable.xlsx", True, "Data!A6:DB" & numberofrows
DoCmd.SetWarnings True
End Sub
This code imports the data fine. However, users will frequently need to update the spreadsheet throughout the day. There are 2 problems that I am frequently running into:
If a user attempts to overwrite the original spreadsheet with an updated version, they receive an error message "Cannot access read-only document".
If the user attempts to open the spreadsheet, they receive the error message that the excel file is locked for editing.
I was under the impression that by running the above VBA code, the user is importing a static table (not a linked table), meaning that once the table is imported into Access, it's no longer referencing the original Excel file.
How do I get around these error messages?
The users will need to update the excel file several times throughout the day and right now, everyone has to completely exit Access in order to access the original excel file.

Access 2007 - Display PDF content on a form

On Access 2007, is there a way to display the content of a PDF, even if it is just the first page, on a form? This PDF saved in a table as attachment.
Disclaimer: This answer will only work for PDF files stored outside of your database as separate file. They can be located over a network connection, but I do not know how to access them directly from your database table. This site gives a thorough guide to using the attachments, but doesn't show how to actually display them automatically. It is likely functionality not provided by Access.
You can display anything Internet Explorer can display with a Microsoft Web Browser Control.
Once you've added the control, you can navigate to whatever you want to display during the load or open event of the form.
For example, if the control is called WebBrowser0 then the following would work:
Private Sub Form_Load()
Me.WebBrowser0.Navigate2 "C:\example.pdf" 'Substitute the actual address here.
End Sub
This is an extremely versatile method for displaying other content within Access. You can find more information here.
The only two methods I know of for previewing a PDF (WebBrowswer as suggested by Daniel and the Adobe Active X control) require a file path to be passed to the control.
I recommend extracting the file from the attachment field and saving it to a temporary location such as C:\Documents and Settings\username\AppData. This can be found by using the vba Environ command.
Extracting the file is done with the SaveToFile method in the embedded DAO recordset (which is how attachments are stored in memory).
Example Code
Assume each record has a field called AttachedFile and each record has only one attached PDF. The form is using a WebBrowser control named PreviewBrowser to view the PDF
Private Sub Form_Current()
On Error GoTo ExitSub
Dim FormRS As DAO.Recordset
Set FormRS = Me.Recordset
Dim RecAtt As DAO.Recordset
If (Me.AttachedFile.AttachmentCount > 0) Then
Set RecAtt = FormRS.Fields("AttachedFile").Value
RecAtt.OpenRecordset
Dim Path As String
FilePath = Environ("APPDATA") & "\Preview.pdf"
If (Dir(FilePath) <> "") Then Kill FilePath
RecAtt.Fields("FileData").SaveToFile FilePath
Me.PreviewBrowser.Navigate2 FilePath
End If
ExitSub:
RecAtt.Close
End Sub
Of course the code needs to be a bit more complicated if there are multiple attachments to a given record, but that would be done by manipulating the RecAtt recordset.

How to reference an embedded PowerPoint or Excel file in a form in Access?

So if I was to take a an Access form, and embed either an Excel spreadsheet into it or a PowerPoint deck, how would I reference it in VBA code?
I know I have to set the libraries, name the frame of the OLE object, and use applicable syntax to whatever I want to do, with whatever I stick in the form, however the only things I have ever done with Excel and/or PowerPoint is automate the opening of a seperate window/application from Access, not within the Access form. So I am not sure how to proceed.
If I said its a new Excel.Application, then set xls = to (the ss in the file, and not some file path of another Excel file somewhere)?
Does that make sense?
Let's say you added an Excel Workbook Object in your Form, named it xLObject and added a Reference to the Excel Object Library in VBA
Here is how you Access a Sheet of this Object and change a Range
Dim sheet As Excel.Worksheet
Set sheet = xlObject.Object.Sheets(1)
sheet.Range("A1") = "Hello World"
The same logic applies to Powerpoint.

How to export to excel from a winform vb.net 2008 without office installed?

I am building a windows form application using visual basic (visual studio 2008).
The idea is to query a MySQL DB and export the results to an excel document.
I managed to do this using this code (I will just show the export to excel part):
Imports Excel = Microsoft.Office.Interop.Excel
Imports System.IO
Imports System.Data
Imports MySql.Data.MySqlClient
Imports System.Configuration
Imports System.Runtime.InteropServices
Private Sub btn_getReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_getReport.Click
Dim wb As Excel.Workbook
Dim ex As New Excel.Application
wb = ex.Workbooks.Add(System.Reflection.Missing.Value)
Dim sheet As Excel.Worksheet = CType(wb.Worksheets.Add, Excel.Worksheet)
sheet.Name = "algo"
Dim i As Integer = 1
Try
While reader.Read
sheet.Cells(i, 1) = CStr(reader.Item(0))
sheet.Cells(i, 2) = CStr(reader.Item(1))
sheet.Cells(i, 3) = CStr(reader.Item(2))
i += 1
End While
Catch MyEx As MySqlException
RaiseEvent MySqlError(Err, MyEx, "read")
Catch exc As Exception
RaiseEvent MySqlError(Err, exc, "read")
End Try
Dim dialog As New SaveFileDialog
Dim result As DialogResult = dialog.ShowDialog
Try
wb.SaveAs(dialog.FileName)
Catch exerr As Exception
End Try
'Show the spreadsheet.
'ex.Visible = True
'wb.Activate()
End Sub
And it works fine on my laptop (which has office 2003 installed), but when I create the setup package and install it on the server where I am going to use it (which does not have office installed), I get this error:
"Retrieving the COM class factory for
component with CLSID
{00024500-0000-0000-C000-000000000046}
failed due to the following error:
80040154."
For what I have read, this is a problem while trying to use excel when its not present on the computer, I can understand that, what really confuses me is that I have used apps that export information to excel even running on computers without having office installed on them, how can they do that?
And for the record, I need the excel file, not a CSV.
Thanks a lot.
You've absolutely diagnosed the problem properly. That's the good news. The bad news is that you cannot use Excel Interop without the Excel DLLs installed on the machine.
Your options:
Install Excel on the machine where
this is going to run.
Pull the DLLs (libraries) and deploy
them with your application. In
doing so, you're probably violating
license agreements with Microsoft.
Do so at your own risk.
Create Excel spreadsheets by hand in the Open XML format used by Office 2007.
To do option three, take a look at the format for Excel 2007 in this article: http://msdn.microsoft.com/en-us/library/aa338205.aspx
In a nutshell, you'll read your data into a dataset. Then you'll export it to an XML file. Cram that file along with any supporting info into a zip file and change the extension to .xlsx. It's a bit more complex than that, but not much.
If you really want to avaid the easy way of exporting to a comma or tabdelimited file which excel can handle perfectly...
...then export to the Excel-XML format (spreadsheetml) by generating xml/using an xsl. You can find an example here. Should work from Excel 2002+.
SpreadsheetGear for .NET can export Excel files, works with any .NET solution including WinForms and does not require Excel to be installed.
You can see live samples here and download the free trial here.
Disclaimer: I own SpreadsheetGear LLC