2017-01-27 44 views
0

我一直在從Visual Basic應用程序中設置的Excel電子表格的行高和列寬不成功。Visual Basic中使用Excel自動化

我有一個Visual Basic應用程序,其中我在剪貼板中有數據。我將該代碼複製到一個excel實例,然後用excel保存生成的電子表格,然後excel關閉。我試圖在保存電子表格之前以編程方式設置行高和單元格寬度,但一直未能這樣做。這是我執行的代碼:

If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then 
    Dim oXL As Excel.Application 
    Dim oWB As Excel.Workbook 
    Dim oSheet As Excel.Worksheet 
    oXL = CreateObject("Excel.Application") 
    oXL.Visible = True 
    oWB = oXL.Workbooks.Add 
    oSheet = oWB.ActiveSheet 

    oSheet.Paste() 
    oSheet.Cells.Select() 
    oSheet.Selection.RowHeight = 11.4 
    oSheet.Cells.EntireColumn.AutoFit() 
    oSheet = Nothing 
    oWB.Close(True, SaveFileDialog1.FileName) 
    oWB = Nothing 
    oXL.Quit() 
    oXL = Nothing 
    MsgBox("Finished!") 
End If 

該應用程序運行,而不oSheet.Cells.Select(),oSheet.Selection.RowHeight = 11.4,和oSheet.Cells.EntireColumn.AutoFit() 線。有了這些行,我得到這個錯誤對話框消息:

公共部件類型的「工作表」「選擇」未找到。

當我在Visual Studio中跟蹤程序時,執行oSheet.Paste()命令並執行oSheet.Cells.Select()命令。當我嘗試執行oSheet.Selection.RowHeight = 11.4命令時會生成異常。

任何援助將不勝感激。

喬納森

+1

試試這個'oSheet.Rows( 「1:1」)的rowHeight = 11.4'並刪除'oSheet.Cells.Select()' – Codexer

+0

如果你要使用'.Select',它是。 [強烈推薦避免做(https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros),它會只是'.Select',不'。選擇()'。 – BruceWayne

+0

@ Zaggler - 我試過你的建議。我不再收到錯誤消息,但生成的電子表格沒有任何高度爲11.4的行,並且列也不是自動填充的。還有其他建議嗎? –

回答

0

下面是不是後期綁定,這是早期綁定,但模式應該有輕微的代碼更改工作。

Option Strict On 
Imports Excel = Microsoft.Office.Interop.Excel 
Imports System.Runtime.InteropServices 
Imports System.IO 

Public Class Operations 
    Public HasError As Boolean 
    Public ErrorMessage As String 
    ''' <summary> 
    ''' 
    ''' </summary> 
    ''' <param name="FileName">Path and file name for Excel file</param> 
    ''' <param name="SheetName">Sheet name to work on</param> 
    ''' <param name="RowHeight">Used to set row height in SheetName</param> 
    ''' <returns></returns> 
    Public Function SetWidthHeight(
     ByVal FileName As String, 
     ByVal SheetName As String, 
     ByVal RowHeight As Integer) As Boolean 

     If File.Exists(FileName) Then 

      Dim Proceed As Boolean = False 

      Dim xlApp As Excel.Application = Nothing 
      Dim xlWorkBooks As Excel.Workbooks = Nothing 
      Dim xlWorkBook As Excel.Workbook = Nothing 
      Dim xlWorkSheet As Excel.Worksheet = Nothing 
      Dim xlWorkSheets As Excel.Sheets = Nothing 
      Dim xlCells As Excel.Range = Nothing 

      xlApp = New Excel.Application 
      xlApp.DisplayAlerts = False 


      xlWorkBooks = xlApp.Workbooks 
      xlWorkBook = xlWorkBooks.Open(FileName) 

      xlApp.Visible = False 

      xlWorkSheets = xlWorkBook.Sheets 

      For x As Integer = 1 To xlWorkSheets.Count 
       xlWorkSheet = CType(xlWorkSheets(x), Excel.Worksheet) 

       If xlWorkSheet.Name = SheetName Then 
        Proceed = True 
        Exit For 
       End If 

       Marshal.FinalReleaseComObject(xlWorkSheet) 
       xlWorkSheet = Nothing 

      Next 
      If Proceed Then 

       xlCells = xlWorkSheet.Cells 
       xlCells.PasteSpecial() 

       Dim EntireRow As Excel.Range = xlCells.EntireRow 
       ' 
       ' Set row height 
       ' 
       EntireRow.RowHeight = RowHeight 
       Dim ColRange = xlCells.EntireColumn 
       ' 
       ' Auto fit all columns 
       ' 
       ColRange.AutoFit() 
       ReleaseComObject(ColRange) 

       ReleaseComObject(xlCells) 
       ReleaseComObject(EntireRow) 

      Else 
       HasError = True 
       ErrorMessage = SheetName & " not found." 
      End If 

      xlWorkSheet.SaveAs(FileName) 

      xlWorkBook.Close() 
      xlApp.UserControl = True 
      xlApp.Quit() 

      ReleaseComObject(xlWorkSheets) 
      ReleaseComObject(xlWorkSheet) 
      ReleaseComObject(xlWorkBook) 
      ReleaseComObject(xlWorkBooks) 
      ReleaseComObject(xlApp) 

     Else 
      HasError = True 
      ErrorMessage = "'" & FileName & 
       "' not located. Try one of the write examples first." 
     End If 

     Return HasError 
    End Function 
    Private Sub ReleaseComObject(ByVal excelObject As Object) 
     Try 
      If excelObject IsNot Nothing Then 
       Marshal.ReleaseComObject(excelObject) 
       excelObject = Nothing 
      End If 
     Catch ex As Exception 
      excelObject = Nothing 
     End Try 
    End Sub 
End Class