トップ回答者
Excelへの画像挿入処理が実行ファイルだとうまくいかない。

質問
-
VB.NET 2012(.NetFramework4.0)
Windows 7(64bit)
Excel2010(32bit)
上記環境で開発を行っており、
あらかじめ用意されたExcelファイルに画像を挿入しようとしています。
VisualStudioからデバッグで実行すると、
正常に画像が挿入されるのですが、
コンパイルし、exeファイルで実行すると、画像が挿入されません。
(文字列等の画像以外は正常に書き込まれています)
エラーも出ておらず、原因が分かりません。
また、以下の環境に移して試しましたが、
全く同じ症状が現れます。
VB.NET 2010(.NetFramework4.0)
Windows XP(32bit)
Excel2003(32bit)
もし原因等御存知でしたら教えてください。
よろしくお願いします。
(コードは次のレスで書き込みます)- 編集済み Camui 2013年7月1日 5:00
回答
-
VB.NET 2012(.NetFramework4.0)
Windows 7(64bit)
Excel2010(64bit)の手元の環境で動作するように、掲載されたコードを多少修正して試してみましたが、VS2012からの実行でも、EXEからの実行でもちゃんとイメージが挿入されましたよ。
AddPictureの引数を単純にして試してみるなど、コードを単純化してみて原因を切り分けられると良いと思います。★良い回答には回答済みマークを付けよう! わんくま同盟 MVP - Visual C# http://d.hatena.ne.jp/trapemiya/
すべての返信
-
Private Function MakeExcelList(ByVal mstpath As String, ByVal outpath As String) As Boolean Dim xlsApp As Object = Nothing Dim xlsBooks As Object = Nothing Dim xlsBook As Object = Nothing Dim xlsSheets As Object = Nothing Dim xlsSheet As Object = Nothing Dim xlsRange As Object = Nothing Dim xlPictures As Object = Nothing Dim xlPicture As Object = Nothing Dim picFolder As String = Path.Combine(Application.StartupPath, "gif") Try ' Excelのクラスのタイプとインスタンスを取得する xlsApp = CreateObject("Excel.Application") Try 'ワークブックコレクションオブジェクト xlsBooks = xlsApp.[GetType]().InvokeMember("Workbooks", BindingFlags.GetProperty, Nothing, xlsApp, Nothing) Try 'Excelファイルのオープン xlsBook = xlsBooks.[GetType]().InvokeMember("Open", BindingFlags.InvokeMethod, Nothing, xlsBooks, New Object() _ {mstpath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, _ Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, _ Type.Missing}) Try xlsSheets = xlsBook.[GetType]().InvokeMember("WorkSheets", BindingFlags.GetProperty, Nothing, xlsBook, Nothing) Try xlsSheet = xlsSheets.[GetType]().InvokeMember("Item", BindingFlags.GetProperty, Nothing, xlsSheets, New Object() {1}) Try For j As Integer = 0 To sortRow.Count - 1 With sortRow(j) '画像の挿入 Dim picname As String = "abc.gif" Dim picpath As String = Path.Combine(picFolder, picname) '画像の存在確認 If System.IO.File.Exists(picpath) = True Then '画像をインポートするセルのレンジ作成 xlsRange = xlsSheet.[GetType]().InvokeMember _ ("Range", BindingFlags.GetProperty, Nothing, xlsSheet, New Object() {"AE" & exlRow * 2 + 27}) Try xlPictures = xlsSheet.[GetType]().InvokeMember _ ("Shapes", BindingFlags.GetProperty, Nothing, xlsSheet, Nothing) Try xlPicture = xlPictures.AddPicture(picpath, False, True, _ CDbl(xlsRange.left) + 10, CDbl(xlsRange.top) + 3, _ (xlsRange.height * 2) - 6, (xlsRange.height * 2) - 6) Finally If xlPictures IsNot Nothing Then MarshalReleaseComObject(xlPicture) End If End Try Finally If xlPictures IsNot Nothing Then MarshalReleaseComObject(xlPictures) End If End Try Else Dim appFolder As String = Application.StartupPath & "\Log" LogExportClass.ExportAppErrorLog(appFolder, "Info_", Now, _ "画像データが見つかりません:" & picpath, False) End If '画像出力先レンジの解放 If xlsRange IsNot Nothing Then MarshalReleaseComObject(xlsRange) End With exlRow = exlRow + 1 Next Finally If xlsRange IsNot Nothing Then MarshalReleaseComObject(xlsRange) End If End Try Finally If xlsSheet IsNot Nothing Then MarshalReleaseComObject(xlsSheet) End If End Try Finally If xlsSheets IsNot Nothing Then MarshalReleaseComObject(xlsSheets) End If End Try Finally If xlsBook IsNot Nothing Then xlsApp.DisplayAlerts = False xlsBook.saveas(outpath) xlsApp.DisplayAlerts = True End If If xlsBook IsNot Nothing Then MarshalReleaseComObject(xlsBook) End If End Try Finally If xlsBooks IsNot Nothing Then MarshalReleaseComObject(xlsBooks) End If End Try Catch ex As Exception Return False Finally If xlsApp IsNot Nothing Then xlsApp.[GetType]().InvokeMember("Quit", BindingFlags.InvokeMethod, Nothing, xlsApp, Nothing) End If If xlsApp IsNot Nothing Then MarshalReleaseComObject(xlsApp) End If End Try Return True End Function
- 編集済み Camui 2013年7月1日 8:36 不要コード削除
-
VB.NET 2012(.NetFramework4.0)
Windows 7(64bit)
Excel2010(64bit)の手元の環境で動作するように、掲載されたコードを多少修正して試してみましたが、VS2012からの実行でも、EXEからの実行でもちゃんとイメージが挿入されましたよ。
AddPictureの引数を単純にして試してみるなど、コードを単純化してみて原因を切り分けられると良いと思います。★良い回答には回答済みマークを付けよう! わんくま同盟 MVP - Visual C# http://d.hatena.ne.jp/trapemiya/
-