I'm writing the following bits of code for a SaveFile Dialog box in Visual Basic 2010.NET. The code works well as the XLSX file is created and opens in Microsoft Excel. However, the problem that I am having is that if the program overwrites a file, the user is getting two prompts. One prompt is from the program's own SaveFile dialog confirming the choice. The other is from Microsoft Excel asking you the same. It's as if the program is running in the background.
Does anyone know how I can write this code to save the file as an Excel file?
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Frm1
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim rowcounter As Integer = 1
xlApp = New Excel.Application
xlApp.Visible = False
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("Sheet1")
xlWorkSheet.Cells(rowcounter, rowcounter) = "Something goes here."
xlWorkSheet.SaveAs(SaveXLS.FileName)
xlWorkBook.Close()
xlApp.Quit()
End Class
I think you want DisplayAlerts=false. Take a look at the answer to this question.
Related
I have a Windows Form application that my company uses to access all it's reports. Most of the reports are given to the user in an Excel sheet that is created at run-time either from scratch or an Excel Template. This has been working fine for everything up until now. The problem I am running into now is that I need to load an ImageBox on the Excel Template with an image saved on the drive. I have the filepath of the image (this will change each time this run). The only way I have found to be able to set the picture property of the ImageBox is like this...
Dim FileStr As String = "C:\Folder\ImageFile.jpg"
xlWorksheet.ImageName.Picture = LoadPicture(FileStr)
The problem is I can't figure out how to call the LoadPicture() function from within the windows form. I know I could create an Excel Module at run-time that call the LoadPicture() then delete it, but i just figured there had to be a better way? Hoping someone out there has suggestions. Thanks.
Edit:- Here is an example of the code I am Using to Open The Excel Sheet
Imports ExcelVB = Microsoft.Office.Interop.Excel
Imports ad = GartnerInterface.AdminClass.AdminTools
Imports xl = GartnerInterface.AdminClass.XlHelp
Public Class TestClass
Public Shared Sub NewSub()
Dim xlApp As ExcelVB.Application
Dim xlWorkbook As ExcelVB.Workbook
Dim xlWorksheet As ExcelVB.Worksheet
Dim TestSht As String
TestSht = "H:\Josh\ExcelTest.xlsm"
xlApp = CreateObject("Excel.Application")
xlWorkbook = xlApp.Workbooks.Add(TestSht)
xlApp.DisplayAlerts = False
xlApp.Visible = True
xlWorksheet = xlApp.Sheets("Sheet1")
Dim FileStr As String = "H:\12117\12117_Original.png"
'xlWorksheet.RFQImg.Picture = LoadPicture(FileStr)
End Sub
End Class
Now that I'm looking at the code, you would likely need the .AddPicture method
At the bottom of this code, you would need something like the following:
Dim FileStr As String = "H:\12117\12117_Original.png"
xlWorksheet.Shapes.AddPicture(FileStr, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 50, 50, 300, 45)
Taken from,
https://msdn.microsoft.com/en-us/library/office/ff198302.aspx
OR, if you have a template where the image is already named "test"
Dim FileStr As String = "H:\12117\12117_Original.png"
Dim imgName as String = "test"
For Each myShape In xlWorksheet.Shapes
If myShape.Name = imgName then
cTop = myShape.ShapeRange.Top 'we must save the values here
cLeft = myShape.ShapeRange.Left
cHeight = myShape.ShapeRange.Height
cWidth = myShape.ShapeRange.Width
myShape.delete
Exit For
end if
next
xlWorksheet.Shapes.AddPicture(FileStr, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, cLeft, cTop, cWidth, cHeight)
An example, with user interaction, I borrowed from
So my company is transition from 2007 to 2013 - and we have some reports writting in VB6 applications where do extract data from database and write it to excel. It seems that there is some changes that were made and they're this a very difficult process...
This is how I create the declarations for some of my extracts...This code is in my VB6 application!
dim xlsheet1 as Excel.Worksheet ' i have OPTION EXPLICIT and do this at the top
In my function to actually write this report I do this....
Set xlApp = New Excel.Application
set xlBook = xlApp.Workbooks.Add
Set xlSheet1 = xlBook.Worksheets.Add
This worked in Office 2007 - but now it seems to hang up on Set xlBook = xlApp.Workbooks.Add
Any idea?
I was facing the same issue when we migrated from Windows XP to Windows 7. The application was not able to create the excel files.
I changed the declaration from "ExcelSheet" to Object
Dim xlApp as Object
Dim xlBook as Object
Then at the time of using it I set it to excel objects
Set xlApp = New Excel.Application
Set xlBook = New Excel.Workbooks.Add
Hope this will work for you !!!
In my web site project I need to create an excel file in Server site. Thus I uese the following.
Try
Dim xlApp As Excel.Application = New Microsoft.Office.Interop.Excel.Application()
If xlApp Is Nothing Then
srvHandler.ErrorAnswer = "Excel is not properly installed!!\r\n CreateExcel xlApp"
ServerHandler._InnerError += srvHandler.ErrorAnswer
_Default.errorCall = True
Return
End If
'Dim xlWorkBooks As Excel.Workbooks = xlApp.Workbooks
Dim xlWorkBook As Excel.Workbook = xlApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet)
Dim xlWorkSheet As Excel.Sheets = xlWorkBook.Worksheets
Dim misValue As Object = System.Reflection.Missing.Value
xlWorkSheet.Item(1).Name = "Page1"
If xlWorkSheet Is Nothing Then
srvHandler.ErrorAnswer = "ERROR: xlWorkSheet == null!!\r\n CreateExcel xlWorkSheet"
ServerHandler._InnerError += srvHandler.ErrorAnswer
_Default.errorCall = True
End If
The all code it runs excellent in my PC but when I send it to my ISP it produces the problem
At the point of
Dim xlApp As Excel.Application = New Microsoft.Office.Interop.Excel.Application()
It throws me the following error
Create Excel Retrieving the COM class factory for component with CLSID
{00024500-0000-C000-000000000046} failed due to the following error:
80040154 Class not registered Exception
I try few ways to overcome but was worthless
Is there someone to assist me on this?
I don't think they have Excel installed.
Or if they have during install you have to enable .NET support (at least you had to do it in Office 2003 if I remember correctly).
I have a Windows Forms application. In this application I save a Excel file to folder. This is the function that save the file:
Public Sub ExportToXls(ByRef dt As DataTable, ByRef path As String)
On Error Resume Next
'I use Microsoft.Office.Interop.Excel version = 12
Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
xlApp = New Microsoft.Office.Interop.Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets(1)
For i1 As Integer = 0 To dt.Columns.Count - 1
xlWorkSheet.Cells(1, i1 + 1) = dt.Columns(i1).Caption
Next
For i1 As Integer = 0 To dt.FieldCount - 1
xlWorkSheet.Cells(2, i1 + 1) = dt.Fields(i1)
Next
Dim fi As New FileInfo(path)
If fi.Exists Then
fi.Delete()
End If
' Default path is X:\WMisch.xls
' here on X: I have all rights.
xlWorkSheet.SaveAs(Filename:=path, FileFormat:=Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel8)
xlWorkBook.Close()
xlApp.Quit()
xlApp = Nothing
xlWorkBook = Nothing
xlWorkSheet = Nothing
On Error GoTo 0
End Sub
All works fine on more that 1 PC/servers but on one Server, with Windows Server 2008 R2 Standard, DOESN'T WORK (doesn't save the file to path X:\WMisch.xls). Here, on this server is installed Office 2003 but I installed also MS Office Interop version 12.
Can anybody say me where is the problem?
Thanks!
I see you're using On Error Resume Next. Rather use a Try..Catch statement. This might help you find you error.
I'm trying to open the Excel file using VBA in Powerpoint 2010 with the help of following code.
Private Sub CommandButton1_Click()
Dim xlApp As Excel.Application
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
xlApp.Workbooks.Open "C:\lol\Book1.xlsx", True, False
Set xlApp = Nothing
Range("A8").Value = "Hello"
End
But I'm getting the following error.
Compile Error
User Defined type not defined.
Am I missing something. Can anyone share the sample piece of code to open an excel file, change a cell value and close Excel file in Powerpoint 2007 and 2010 using VBA.
I have searched a lot and tried different pieces of code, but getting the same error everytime. :(
Thanks in advance. :)
Have you added a reference to the Excel Object Model? That would save you having to use the late bound objects (and you get the benefit of having the Intellisense help when you are coding).
You need to go to Tools -> References and check the "Microsoft Excel v.x Object Library" (I think that number changes depending on the version of office you are using.
Your code should work if you do that, you should also remove the
CreateObject("Excel.Application")
line and replace it with
Set xlApp = new Excel.Application
And move the
Set xlApp = nothing
line to the end of your subroutine.
The rest of your code looks fine to me.
Late binding code would be this
Private Sub test()
Dim xlApp As Object
Dim xlWorkBook As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlWorkbook = xlApp.Workbooks.Open("C:\lol\Book1.xlsx", True, False)
xlWorkbook.sheets(1).Range("A8").Value = "Hello"
Set xlApp = Nothing
Set xlWorkbook = Nothing
End Sub
It's better to use early binding though.