0

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
Laurenz Albe
  • 129,316
  • 15
  • 96
  • 132
  • 1
    1) It would help if you told us which line the error happens on. 2) Have you seen [HRESULT: 0x800A03EC on Worksheet.range](https://stackoverflow.com/q/7099770/1115360)? – Andrew Morton Aug 22 '19 at 10:22
  • 1
    This error is a *Nope, can't do that* thing or *Sorry, can't find this*. In other words `Unsupported Operation`. You may receive it when you try to write a file that is already opened, for example. Or you're using strings/dates/other with different locales. I'ld check all those `("PATH")` things and conditions, the Worksheets actual names, etc. You're also not disposing correctly of the Excel process and the objects you create. Especially if you answer `No` when that MessageBox is shown. – Jimi Aug 22 '19 at 10:25
  • Hi thank you for your response. I receive that error at xlWorkBook = xlApp.Workbooks.Open(xlTaslakPath) line. xlTaslakPath doesn't include any variable date or other. I wrote ("PATH") instead of actual pathes. they include "Date.Today.ToString("MM_yyyy")" in the code. But I didn't understand It works for 5 computers just perfect repeatedly but 1 computer I couldn't make it work. I couldn't find any solution. – Anıl Ozturk Aug 22 '19 at 10:52
  • What happens if you try to open that file yourself in Excel? – Andrew Morton Aug 22 '19 at 13:52
  • I can open file manually – Anıl Ozturk Aug 23 '19 at 08:17
  • Maybe the file is open in a Preview Panel somewhere. That can lock it. Make sure you deselect it from all file managers on all PCs, and try again – Ess Kay Aug 23 '19 at 14:45
  • Get rid of all those empty If statemens and just used `If NOT Then`. No Else necessary. – Mary Aug 25 '19 at 17:37
  • `While File.Exists(xlDestinationPath)` Do you mean this loop to continue as long as the file exists? – Mary Aug 25 '19 at 17:40
  • How about `If MsgBox("-----------------------------", vbYesNo, "Uyarı Penceresi") = MsgBoxResult.No Then` `MsgBox("-----------------------------------------")` `Close()` `End If` So we don't have to scroll all over to see what the other Case is? – Mary Aug 25 '19 at 17:55
  • Thank you for your responses. I solved the problem. The code is not running because of Security settings of that specific computer :) – Anıl Ozturk Aug 29 '19 at 14:14

0 Answers0