我编写了一个代码只是为了打开一个 Excel 电子表格并截取所有屏幕的屏幕截图,将其粘贴到 Excel 工作表中并将其保存到另一个位置。
这适用于 5 台计算机,但在一台计算机上我收到一条错误消息“hresult 0x800a03ec”。我不知道问题是什么。请帮忙!
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