タイトル : Re^4: エクセルへのテキストボックス描写方法 投稿日 : 2015/05/02(Sat) 01:32 投稿者 : もとき
本当にいろいろ、 教えて頂き、ありがとうございます。 アドバイスを参考に変更しました。(合っているでしょうか?) ただ、複数個描写しようと思うと やはり、エラーとなってしまいます。 申し訳ございませんが、教えて頂けないでしょうか? Option Strict On Imports Microsoft.Office.Interop Imports Microsoft.Office.Core Imports System.Runtime.InteropServices Public Class Form1 Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click Dim FilePath As String FilePath = "D:\VB\excel1.xlsx" Dim xlApp As New Excel.Application Dim xlBooks As Excel.Workbooks Dim xlBook As Excel.Workbook Dim xlSheets As Excel.Sheets Dim xlSheet As Excel.Worksheet Dim xlShapes As Excel.Shapes Dim xlShape As Excel.Shape Dim xlLine As Excel.LineFormat Dim xlColor As Excel.ColorFormat Dim xlFill As Excel.FillFormat Dim xlTextEffect As Excel.TextEffectFormat Dim TextBoxSu As Integer = 0 Dim TextBoxColor As Integer = 0 xlBooks = xlApp.Workbooks xlBook = xlBooks.Open(FilePath) xlSheets = xlBook.Worksheets xlSheet = DirectCast(xlSheets.Item("sheet1"), Excel.Worksheet) xlShapes = xlSheet.Shapes 'Excelを表示する 'xlApp.Visible = True 'Excelを表示しない xlApp.Visible = False 'Excelの警告メッセージを表示しない xlApp.DisplayAlerts = False For i As Integer = 0 To 20 xlShape = xlShapes.AddTextbox(MsoTextOrientation.msoTextOrientationHorizontal, _ 200, 100 + 100 * i, 100, 100) xlShape.Name = "textBoxName" & TextBoxSu '〇テキストボックスへテキスト追加 xlTextEffect = xlShape.TextEffect xlTextEffect.Text = Str(i) xlTextEffect.FontSize = 12 'フォントサイズ TextBoxSu += 1 MRComObject(xlTextEffect) ' 〇テキストボックス(ライン色変更) xlLine = xlShape.Line xlColor = xlLine.ForeColor xlLine.Weight = 3 '太さ xlColor.RGB = RGB(0, 0, 255) 'ラインを青へ MRComObject(xlLine) '〇テキストボックス背景色変更 xlFill = xlShape.Fill xlColor.SchemeColor = 1 MRComObject(xlColor) MRComObject(xlFill) MRComObject(xlShape) Next i '▼終了処理 MRComObject(xlShapes) MRComObject(xlSheet) 'xlSheet の解放 MRComObject(xlSheets) 'xlSheets の解放 xlBook.Close(False) 'xlBook を閉じる MRComObject(xlBook) 'xlBook の解放 MRComObject(xlBooks) 'xlBooks の解放 xlApp.Quit() 'Excelを閉じる MRComObject(xlApp) 'xlApp を解放 MessageBox.Show("保存しました", "メッセージ", MessageBoxButtons.OK, MessageBoxIcon.Information) End Sub '●エクセルの開放 Public Shared Sub MRComObject(Of T As Class)(ByRef objCom As T, Optional ByVal force As Boolean = False) If objCom Is Nothing Then Return End If Try If System.Runtime.InteropServices.Marshal.IsComObject(objCom) Then If force Then System.Runtime.InteropServices.Marshal.FinalReleaseComObject(objCom) Else Dim count As Integer = System.Runtime.InteropServices.Marshal.ReleaseComObject(objCom) End If End If Finally objCom = Nothing End Try End Sub End Class |