Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27248

How to save an Excel Workbook as Macro Enabled Workbook

$
0
0
Name:  error msg.JPG
Views: 117
Size:  24.8 KBHello,

I have found a block of code that saves an Excel file when I click on a button on the form. Here is the code:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


Dim xlApp As Excel.Application

Dim xlWorkBook As Excel.Workbook

Dim xlWorkSheet As Excel.Worksheet

Dim misValue As Object = System.Reflection.Missing.Value

Dim i As Integer

Dim j As Integer

xlApp = New Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = CType(xlWorkBook.Sheets("sheet1"), Excel.Worksheet)
For i = 0 To dgvBookInfo.RowCount - 2
For j = 0 To dgvBookInfo.ColumnCount - 1
xlWorkSheet.Cells(i + 1, j + 1) = dgvBookInfo(j, i).Value.ToString()
Next
Next
xlWorkSheet.SaveAs("C:\output.xls")
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)

If I need to change the the type of the Excel Workbook (Output) to be an MAcro Enabled Workbook what needs to be changed in the above code?
If I only change the extension of the file Output to XLSM I got an error message:
Attached Images
 

Viewing all articles
Browse latest Browse all 27248

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>