Hoi allemaal Ik wil graag een Excel-bestand (bestaand) komen schrijven vanuit SW (met een vba-macro) om de naam van mijn onderdeel, de naam van de klant te schrijven... Maar ik kom een probleem tegen als ik deze methode gebruik:
Dim xlApp As Excel.Application
Set xlApp = Excel.Application
Dim wbk As Excel.Workbook
Dim sht As Excel.Worksheet
With xlApp
.Visible = True
.Workbooks.Open ("mondoc.xlsm")
End With
Het probleem is dat deze code een nieuw exemplaar van Excel voor mij creëert En dit levert me een probleem op, vooral als het mondoc-bestand in eerste instantie al open is, het bestand is alleen-lezen als het bestand niet wordt geopend, werkt het, ik kan schrijven, maar mijn mondoc-bestand zelf bevat macro's die niet meer kunnen werken met de andere geopende bestanden (omdat ze niet allemaal in dezelfde instantie van Excel staan).
Mijn vraag is het mogelijk om via SW het mondoc-bestand te openen in dezelfde instantie als het bestand dat al bezig is op mijn werkstation Zo ja, hoe doe je dat? Ik heb verschillende dingen getest, maar mijn VBA-waarde is niet heel hoog Bedankt voor de tijd.
Hallo; Ga als volgt te werk om te controleren of een Excel-bestand al is geopend: Dan gebruiken we dit bestand. Anders openen we dit bestand:
Sub CheckAndOpenExcelFile()
Dim xlApp As Object
Dim xlWorkbook As Object
Dim filePath As String
Dim isFileOpen As Boolean
' Chemin du fichier Excel
filePath = "C:\Chemin\Vers\Votre\Fichier.xlsx"
' Vérifiez si Excel est déjà ouvert
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If xlApp Is Nothing Then
' Si Excel n'est pas ouvert, ouvrez une nouvelle instance
Set xlApp = CreateObject("Excel.Application")
End If
On Error GoTo 0
' Vérifiez si le fichier est déjà ouvert
isFileOpen = False
For Each xlWorkbook In xlApp.Workbooks
If xlWorkbook.FullName = filePath Then
isFileOpen = True
Set xlWorkbook = xlApp.Workbooks.Open(filePath)
Exit For
End If
Next xlWorkbook
' Si le fichier n'est pas ouvert, ouvrez-le
If Not isFileOpen Then
Set xlWorkbook = xlApp.Workbooks.Open(filePath)
End If
' Affichez Excel (facultatif)
xlApp.Visible = True
' Faites quelque chose avec le fichier Excel
' ...
' Nettoyez
Set xlWorkbook = Nothing
Set xlApp = Nothing
End Sub
Door het pad aangegeven in " filepath " te vervangen door de locatie van uw Excel-bestand.
Let op: vergeet niet om de </> tags te gebruiken bij het plaatsen van een macro
… anders kan het automatisch in Franse tekst worden vertaald. wat het niet gemakkelijk maakt om te lezen
Hallo Mijn code is bijna klaar, maar ik heb nog een klein probleempje dat ik maar niet kan oplossen wanneer het Excel-bestand wordt geopend, is het OK (het wordt weergegeven en ik kan mijn rij selecteren) aan de andere kant, als het Excel-bestand gesloten is, opent het bestand goed, maar het blijft op de achtergrond, ik ben genoodzaakt het via de werkbalk te selecteren Is er de mogelijkheid om de weergave op de voorgrond van het Excel-bestand te forceren??? Zo ja, hoe doe je dat? Bedankt Ik was het bijna vergeten, het Excel-bestand staat online (office365) en het bevat ook macro's wanneer het wordt geopend.
Sub EXPDDC()
Set swApp = GetObject(, "SldWorks.Application") 'CreateObject("SldWorks.Application")
Set swmodel = swApp.ActiveDoc
If swmodel Is Nothing Then
Exit Sub
End If
If swmodel.GetType = 3 Then
Set swDraw = swmodel
Set swView = swDraw.GetFirstView
Set swView = swView.GetNextView
Set swParentModel = swView.ReferencedDocument
prop1 = swParentModel.GetTitle & "-" & swParentModel.CustomInfo2("", "REVISION")
If swParentModel.CustomInfo2("", "type") = "FAB" Then
prop2 = swParentModel.CustomInfo2("", "client")
ElseIf prop4 = "QUINCAI" Then
prop2 = ""
Else: MsgBox "MANQUE CODE FAMILLE"
End If
If swmodel.CustomInfo2("", "ETAT") = "VALIDE" And swParentModel.CustomInfo2("", "ETAT") = "VALIDE" Then prop3 = "etat ok"
Else
prop1 = swmodel.GetTitle & "-" & swmodel.CustomInfo2("", "REVISION")
If swmodel.CustomInfo2("", "type") = "FAB" Then
prop2 = swmodel.CustomInfo2("", "client")
ElseIf prop4 = "QUINCAI" Then
prop2 = ""
Else: MsgBox "MANQUE CODE FAMILLE"
End If
If swmodel.CustomInfo2("", "ETAT") = "VALIDE" Then prop3 = "etat ok"
End If
If prop3 <> "etat ok" Then
réponse = MsgBox("LES FICHIERS NE SONT PAS A L'ETAT " & Chr(34) & " VALIDÉ " & Chr(34) & vbCrLf & "SOUHAITEZ VOUS TOUS DE MÊME EXPORTER VERS" & vbCrLf & Chr(34) & " DEMANDE DE CHIFFRAGE " & Chr(34) & " ?", vbExclamation + vbYesNo + vbDefaultButton2, "ETAT FICHIER")
End If
If réponse = vbYes Or prop3 = "etat ok" Then
'traitement
Dim xlApp As Object
Dim xlWorkbook As Object
Dim filePath As String
Dim Selec As Range
' Chemin du fichier Excel
filePath = "chemin/monfichier.xlsm"
'Vérifiez si Excel est déjà ouvert
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If xlApp Is Nothing Then
' Si Excel n'est pas ouvert, ouvrez une nouvelle instance
Set xlApp = CreateObject("Excel.Application")
End If
On Error GoTo 0
Set xlWorkbook = xlApp.Workbooks.Open(filePath)
' Affichez Excel (facultatif)
xlApp.Visible = True
xlWorkbook.Sheets("feuil1").Activate
' Faites quelque chose avec le fichier Excel
On Error Resume Next 'au cas où on clique sur Annuler
Set Selec = xlApp.InputBox("CLIQUER SUR LA LIGNE DE DESTINATION", Type:=8)
On Error GoTo 0
If Not Selec Is Nothing Then
Ligne = Selec.Row
xlWorkbook.Sheets("feuil1").Cells(Ligne, 2) = prop1
xlWorkbook.Sheets("feuil1").Cells(Ligne, 4) = prop2
xlWorkbook.Save
Else
swApp.Visible = True
swApp.SendMsgToUser "EXPORT ANNULÉ!"
End If
' Nettoyez
Set xlWorkbook = Nothing
Set xlApp = Nothing
Else
MsgBox "VEUILLEZ MODIFIER L'ETAT DES FICHIERS" & vbCrLf & "( AUCUNE ACTION EFECTUEE !!! )"
End If
End Sub
saisissez ou collez du code ici
… Ik ben perplex, de hele Macro zou niet moeten werken met Excel in de online versie (je zou een webbrowser moeten openen, inloggen, de ID valideren), bovendien staat Solidworks niet bekend als supercompatibel met Office 365... en ik denk niet dat het mogelijk is om het in VBA te beheren...
In theorie, om het Excel-venster op de voorgrond weer te geven (voor een offline Excel), de opdracht:
Voor de leesbaarheid van uw code, vergeet niet om uw variabele declaraties de " Dim..." " op de top... net onder " Sub EXPDDC() " … Verplaats de lijnen:
' Nettoyez
Set xlWorkbook = Nothing
Set xlApp = Nothing
Aan het einde van je macro, net boven het einde sub
probeer de stap-voor-stap modus (F8-toets) en vertel me of Excel zichtbaar is op de voorgrond, wanneer je bij de opdracht komt "xlWorkbook.Sheets("feuil1").Activate" (regel 68) .
Dus ik heb een einde gemaakt aan de vorige regel en in de stapsgewijze modus als ik over de streep kom
xlWorkbook.Sheets("feuil1").Activate
Er gebeurt niets
(Ik kan niet alle code in de stapmodus doen, want bij het openen van het Excel-bestand dat zelf macro's bevat, ben ik genoodzaakt het bestand op de voorgrond te plaatsen om ze in de stapsgewijze modus uit te voeren)
De lijn werkt echter Ik heb getest door tabbladen te wijzigen Het komt dus niet tevoorschijn, maar als ik het handmatig op de voorgrond zet, volg ik natuurlijk het gekozen tabblad