I wrote a code just to open an Excel spreadsheet and take a screenshot of all screens, paste it into the Excel sheet and save it to another location.
This works for 5 computers, but on one computer I receive an error message "hresult 0x800a03ec". I have no idea what the problem is. Please help!
Imports System.IO
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim xlTaslakPath, xlDestinationPath As String
Dim i, j As Int64
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Select Case MsgBox("-----------------------------", vbYesNo + vbQuestion + vbDefaultButton1, "Uyarı Penceresi")
Case MsgBoxResult.Yes
xlTaslakPath = ("PATH")
xlApp = CreateObject("Excel.Application")
xlApp.ScreenUpdating = True
xlApp.Visible = True
xlWorkBook = xlApp.Workbooks.Open(xlTaslakPath)
xlWorkSheet = xlWorkBook.Sheets("AAAAA")
Threading.Thread.Sleep(4000)
'Aylık Klasör Kontrol Ediliyor
If File.Exists("PATH") Then
Else
Directory.CreateDirectory("PATH")
End If
'Aylık Klasör İçinde PNG Klasörü Kontrol Ediliyor
If File.Exists("PATH") Then
Else
Directory.CreateDirectory("PATH")
End If
'Aylık Klasör İçinde Bloomberg Klasörü Kontrol Ediliyor
If File.Exists("PATH") Then
Else
Directory.CreateDirectory("PATH")
End If
'Aylık Klasör İçinde Yield Curve Klasörü Kontrol Ediliyor
If File.Exists("PATH") Then
Else
Directory.CreateDirectory("PATH")
End If
'PNG Path'i Seçiliyor
Dim captureSavePath As String
captureSavePath = ("PATH")
i = 2
While File.Exists(captureSavePath)
captureSavePath = ("PATH")
i = i + 1
End While
' This line is modified for multiple screens, also takes into account different screen size (if any)
Dim bmp As Bitmap = New Bitmap(
Screen.AllScreens.Sum(Function(s As Screen) s.Bounds.Width),
Screen.AllScreens.Max(Function(s As Screen) s.Bounds.Height))
Dim gfx As Graphics = Graphics.FromImage(bmp)
' This line is modified to take everything based on the size of the bitmap
gfx.CopyFromScreen(SystemInformation.VirtualScreen.X,
SystemInformation.VirtualScreen.Y,
0, 0, SystemInformation.VirtualScreen.Size)
' Oh, create the directory if it doesn't exist
Directory.CreateDirectory(Path.GetDirectoryName(captureSavePath))
bmp.Save(captureSavePath)
'replace you picture to xl_pic.JPG
xlWorkSheet.Shapes.AddPicture(captureSavePath, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 1200, 0, 2840, 1080)
'Bloomberg Excel'inin Path'i Seçiliyor
xlDestinationPath = ("PATH")
j = 2
While File.Exists(xlDestinationPath)
xlDestinationPath = ("PATH")
j = j + 1
End While
xlWorkSheet.SaveAs(xlDestinationPath)
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
MsgBox("İşlem Tamamlandı. Teşekkürler :)")
Close()
Case MsgBoxResult.No
MsgBox("-----------------------------------------")
Close()
End Select
End Sub
Private Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
End Class