Avatar billede taae Juniormester
01. juni 2016 - 14:44 Der er 9 kommentarer og
2 løsninger

Lukning af enkelt Excel vindue vha. vba

Hej Eksperter

Jeg bøvler pt. med en template, hvori man kan indtaste diverse oplysninger. Den indeholder en Send knap, når man trykker på den sendes template som vedhæftet fil til en anden afdeling. Samtidig med at man trykker på Send knappen lukkes template auto. ned. Så længe jeg har et enkelt Excel vindue kørende, så er alt fint, men lige så snart jeg har flere vinduer kørende, lukkes alle vinduer ned. Det er ikke hensigten, da jeg kun ønsker template skal lukkes. Jeg mistænker brug af Application.Quit til at være skurken !

Håber I kan hjælpe ASAP, har kun til fredag at løse dette problem.
Avatar billede supertekst Ekspert
01. juni 2016 - 15:30 #1
Du er velkommen til at sende den til pb<snabela>supertekst-it.dk eller Uploade filen
Avatar billede taae Juniormester
01. juni 2016 - 15:55 #2
Sub Mail_ActiveSheet()
'Working in Excel 2000-2016
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim Sourcewb As Workbook
    Dim Destwb As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim OutApp As Object
    Dim OutMail As Object
   

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Set Sourcewb = ActiveWorkbook

    'Copy the ActiveSheet to a new workbook
    ActiveSheet.Copy
    Set Destwb = ActiveWorkbook

    'Determine the Excel version and file extension/format
    With Destwb
        If Val(Application.Version) < 12 Then
            'You use Excel 97-2003
            FileExtStr = ".xls": FileFormatNum = -4143
        Else
            'You use Excel 2007-2016
            Select Case Sourcewb.FileFormat
            Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
            Case 52:
                If .HasVBProject Then
                    FileExtStr = ".xlsm": FileFormatNum = 52
                Else
                    FileExtStr = ".xlsx": FileFormatNum = 51
                End If
            Case 56: FileExtStr = ".xls": FileFormatNum = 56
            Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
            End Select
        End If
    End With

    '    'Change all cells in the worksheet to values if you want
    '    With Destwb.Sheets(1).UsedRange
    '        .Cells.Copy
    '        .Cells.PasteSpecial xlPasteValues
    '        .Cells(1).Select
    '    End With
    '    Application.CutCopyMode = False

    'Save the new workbook/Mail it/Delete it
    TempFilePath = Environ$("temp") & "\"
    TempFileName = "Part of " & Sourcewb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    With Destwb
        .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
        On Error Resume Next
        With OutMail
            .to = "touseef@torshavn.fo"
            .CC = ""
            .BCC = ""
            .Subject = "Heimildarskjal, " & Range("D13")
            .Body = "Hi there"
            .Attachments.Add Destwb.FullName
            If MsgBox("Send Heimildarskjal?", vbOKCancel, "Confirm") = vbOK Then
            .Send
            Application.DisplayAlerts = False
            ThisWorkbook.Save
            Application.DisplayAlerts = True
            Application.Quit
              ElseIf msgValue = vbCancel Then
              MsgBox "Ret og send igen"
            End If
            'You can add other files also like this
            '.Attachments.Add ("C:\test.txt")
            '.Send  'or use .Display
        End With
        On Error GoTo 0
        .Close SaveChanges:=False
    End With

    'Delete the file you have send
    Kill TempFilePath & TempFileName & FileExtStr

    Set OutMail = Nothing
    Set OutApp = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        '.DisplayAlerts = False
        '.Quit
    End With
End Sub
Avatar billede taae Juniormester
01. juni 2016 - 15:56 #3
ovenstående kode er til Send knappen
Avatar billede supertekst Ekspert
01. juni 2016 - 16:05 #4
Ok - vil se på det..
Avatar billede supertekst Ekspert
01. juni 2016 - 16:43 #5
If MsgBox("Send Heimildarskjal?", vbOKCancel, "Confirm") = vbOK Then
            .Send
            Application.DisplayAlerts = False
            ThisWorkbook.Save
            Application.DisplayAlerts = True
'          Application.Quit                  '<<<<<<<<<<<<<<<<<< prøv at slette denne
              ElseIf msgValue = vbCancel Then
              MsgBox "Ret og send igen"
            End If
            'You can add other files also like this
            '.Attachments.Add ("C:\test.txt")
            '.Send  'or use .Display
        End With
        On Error GoTo 0
        .Close SaveChanges:=False
    End With
Avatar billede Dan Elgaard Ekspert
01. juni 2016 - 19:13 #6
If Workbooks.Count < 2 Then
      Application.Quit
Else
    ThisWorkbook.Close SaveChanges:=False
End If
Avatar billede taae Juniormester
02. juni 2016 - 09:55 #7
Hej

Supertekst - jeg har fjernet linjen du bad om.
Pistolprinsen - jeg har modificeret din kode en lille smule så den nu ser sådan ud:

With Application
        .ScreenUpdating = True
        .EnableEvents = True
        '.DisplayAlerts = False
        If Workbooks.Count < 2 Then
        .Quit
          Else
            ThisWorkbook.Close SaveChanges:=False
        End If
    End With

Nu er problemet at de første 2 gange er det den rigtige Excel der lukker ned og de andre kører stadig, men TREDJE gang, laver den fuldstændig ged i den og lukker alt ned. ?!?!?!?!?!
Avatar billede taae Juniormester
01. juli 2016 - 15:15 #8
løst vha. hjælp fra konsulent
Avatar billede Dan Elgaard Ekspert
01. juli 2016 - 18:58 #9
Hva' var løsningen?
Avatar billede taae Juniormester
01. juli 2016 - 19:02 #10
Han lavede mange ændringer i koden. Jeg er ikke så skrap til VBA, så det ved jeg ikke. Men det virker efter hensigt nu.
Avatar billede Dan Elgaard Ekspert
01. juli 2016 - 19:32 #11
Ja, jeg have nok på fornemmelsen, at det var noget af alt det andet kode, der måtte drille :-)

For min løsning burde virke efter hensigten - har aldrig selv oplevet problemer med den metiode.

Men, godt, du fandt en løsning :-)
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester