Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

tester si un fichier existe dans le repertoire

9 réponses
Avatar
Sunburn
Bonjour,
j'ai une macro qui me crée un document word, avec un nom précis.
l'emplacement est celui où se situe le fichier excel.
je ne veux pas qu'il crée plusieurs documents avec le même nom.
Donc j'aimerais un test qui cherche si le document existe déjà.
si non, la macro continu et le cré,
si oui, demande à l'utilisateur s'il veut ecraser le précédent fichier, si
oui, il continue la macro, si non, il quitte la macro..
voilà ma macro actuelle.
je n'arrive pas à réaliser le test, la msgbox, ça c'est bon ,mais le test
avec le chemin, j'arrive pas.
Merci de votre aide.
YANN
-----
Sub MacroWordExp()
'Avant de lancer cette macro, Dans l'éditeur VBA: Faire Menu / Outils /
'Reference / Cocher "Microsoft Word xx.xx Library"
Dim NomDoc As String
Set wordobj = CreateObject("Word.Application")
'Set wordobj = GetObject(, "Word.Application")
NomDoc = "Note de revue de l'expert Comptable"
wordobj.documents.Add Template:= _
"G:\COLL\ypa\Dossiers de travail\enteteYPA.dot", NewTemplate:=False, _
DocumentType:=0
'wordobj.Visible = True
wordobj.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
With wordobj.Selection
.MoveRight wdCell
.TypeText Text:=[DGA!B1].Value
.MoveRight wdCell, 3
.TypeText Text:=[DGA!G3].Value
.MoveRight wdCell, 2
.TypeText Text:=CStr([DGA!B2].Value)

.MoveRight wdCell
.TypeText Text:=[DGA!T1].Value

.MoveRight wdCell, 2
.TypeText Text:=[DGA!G4].Value
.MoveRight wdCell, 2
.TypeText Text:=CStr([DGA!B3].Value)
.MoveRight wdCell, 3
.TypeText Text:=[DGA!G5].Value
End With
wordobj.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
If NomDoc <> "" Then
wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "\" & NomDoc
wordobj.Visible = True
'wordobj.Quit
End If
Set wordobj = Nothing
End Sub
-----

9 réponses

Avatar
Fredo P.
L'ouvrir alors qu'il n'existe pas provoque une erreur

Public Sub op()
Err.clear
On Error Resume Next
Workbooks.Open ("Machin.xls")
If Err > 0 Then
MsgBox "Dans " & CurDir("C") & " Machin n'existe pas"
End If
End Sub

"Sunburn" a écrit dans le message de
news:
Bonjour,
j'ai une macro qui me crée un document word, avec un nom précis.
l'emplacement est celui où se situe le fichier excel.
je ne veux pas qu'il crée plusieurs documents avec le même nom.
Donc j'aimerais un test qui cherche si le document existe déjà.
si non, la macro continu et le cré,
si oui, demande à l'utilisateur s'il veut ecraser le précédent fichier, si
oui, il continue la macro, si non, il quitte la macro..
voilà ma macro actuelle.
je n'arrive pas à réaliser le test, la msgbox, ça c'est bon ,mais le test
avec le chemin, j'arrive pas.
Merci de votre aide.
YANN
-----
Sub MacroWordExp()
'Avant de lancer cette macro, Dans l'éditeur VBA: Faire Menu / Outils /
'Reference / Cocher "Microsoft Word xx.xx Library"
Dim NomDoc As String
Set wordobj = CreateObject("Word.Application")
'Set wordobj = GetObject(, "Word.Application")
NomDoc = "Note de revue de l'expert Comptable"
wordobj.documents.Add Template:= _
"G:COLLypaDossiers de travailenteteYPA.dot",


NewTemplate:úlse, _
DocumentType:=0
'wordobj.Visible = True
wordobj.ActiveWindow.ActivePane.View.SeekView wdSeekCurrentPageHeader
With wordobj.Selection
.MoveRight wdCell
.TypeText Text:=[DGA!B1].Value
.MoveRight wdCell, 3
.TypeText Text:=[DGA!G3].Value
.MoveRight wdCell, 2
.TypeText Text:=CStr([DGA!B2].Value)

.MoveRight wdCell
.TypeText Text:=[DGA!T1].Value

.MoveRight wdCell, 2
.TypeText Text:=[DGA!G4].Value
.MoveRight wdCell, 2
.TypeText Text:=CStr([DGA!B3].Value)
.MoveRight wdCell, 3
.TypeText Text:=[DGA!G5].Value
End With
wordobj.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
If NomDoc <> "" Then
wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" & NomDoc
wordobj.Visible = True
'wordobj.Quit
End If
Set wordobj = Nothing
End Sub
-----


Avatar
Daniel.C
Essaie comme ça :

If NomDoc <> "" Then
If Dir(ActiveWorkbook.Path & "" & NomDoc) <> "" Then
rep = MsgBox("Le fichier existe déjà; Voulez-vous l'écraser
?", vbYesNo)
If rep = vbYes Then
Kill ActiveWorkbook.Path & "" & NomDoc
wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" &
NomDoc
End If
Else
wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" & NomDoc
End If
End If

Daniel

Bonjour,
j'ai une macro qui me crée un document word, avec un nom précis.
l'emplacement est celui où se situe le fichier excel.
je ne veux pas qu'il crée plusieurs documents avec le même nom.
Donc j'aimerais un test qui cherche si le document existe déjà.
si non, la macro continu et le cré,
si oui, demande à l'utilisateur s'il veut ecraser le précédent fichier, si
oui, il continue la macro, si non, il quitte la macro..
voilà ma macro actuelle.
je n'arrive pas à réaliser le test, la msgbox, ça c'est bon ,mais le test
avec le chemin, j'arrive pas.
Merci de votre aide.
YANN
-----
Sub MacroWordExp()
'Avant de lancer cette macro, Dans l'éditeur VBA: Faire Menu / Outils /
'Reference / Cocher "Microsoft Word xx.xx Library"
Dim NomDoc As String
Set wordobj = CreateObject("Word.Application")
'Set wordobj = GetObject(, "Word.Application")
NomDoc = "Note de revue de l'expert Comptable"
wordobj.documents.Add Template:= _
"G:COLLypaDossiers de travailenteteYPA.dot", NewTemplate:úlse,
_ DocumentType:=0
'wordobj.Visible = True
wordobj.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
With wordobj.Selection
.MoveRight wdCell
.TypeText Text:=[DGA!B1].Value
.MoveRight wdCell, 3
.TypeText Text:=[DGA!G3].Value
.MoveRight wdCell, 2
.TypeText Text:=CStr([DGA!B2].Value)

.MoveRight wdCell
.TypeText Text:=[DGA!T1].Value

.MoveRight wdCell, 2
.TypeText Text:=[DGA!G4].Value
.MoveRight wdCell, 2
.TypeText Text:=CStr([DGA!B3].Value)
.MoveRight wdCell, 3
.TypeText Text:=[DGA!G5].Value
End With
wordobj.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
If NomDoc <> "" Then
wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" & NomDoc
wordobj.Visible = True
'wordobj.Quit
End If
Set wordobj = Nothing
End Sub
-----


Avatar
Sunburn
Salut,
ben en fait, il ne me cré bien le fichier, mais le message lorsqu'il existe
deja le fichier n'apparait pas, et donc il ecrase l'autre.
Pour faire plus simple, si le fichier existe deja, le message "le fichier
existe déjà" s'affiche, sinon il cré le fichier.
et si le fichier existe deja, alors il l'affiche.
ma macri, avec un test pour ce que je viens de dire :
-----
Sub MacroWordExp()
'Avant de lancer cette macro, Dans l'éditeur VBA: Faire Menu / Outils /
'Reference / Cocher "Microsoft Word xx.xx Library"
Dim NomDoc As String
Set wordobj = CreateObject("Word.Application")
'Set wordobj = GetObject(, "Word.Application")
NomDoc = "Note de revue de l'expert Comptable"
wordobj.documents.Add Template:= _
"G:COLLypaDossiers de travailenteteYPA.dot", NewTemplate:úlse, _
DocumentType:=0
'wordobj.Visible = True
wordobj.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
With wordobj.Selection
.MoveRight wdCell
.TypeText Text:=[DGA!B1].Value
.MoveRight wdCell, 3
.TypeText Text:=[DGA!G3].Value
.MoveRight wdCell, 2
.TypeText Text:=CStr([DGA!B2].Value)
.MoveRight wdCell
.TypeText Text:=[DGA!T1].Value
.MoveRight wdCell, 2
.TypeText Text:=[DGA!G4].Value
.MoveRight wdCell, 2
.TypeText Text:=CStr([DGA!B3].Value)
.MoveRight wdCell, 3
.TypeText Text:=[DGA!G5].Value
End With
wordobj.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
If NomDoc <> "" Then
If Dir(ActiveWorkbook.Path & "" & NomDoc) <> "" Then
rep = MsgBox("Le fichier existe déjà. Ouverture de ce fichier",
vbOKOnly)
wordobj.Visible = True
Else
wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" & NomDoc
End If
End If
'Set wordobj = Nothing
wordobj.Visible = True
End Sub
-----
"Daniel.C" a écrit :

Essaie comme ça :

If NomDoc <> "" Then
If Dir(ActiveWorkbook.Path & "" & NomDoc) <> "" Then
rep = MsgBox("Le fichier existe déjà; Voulez-vous l'écraser
?", vbYesNo)
If rep = vbYes Then
Kill ActiveWorkbook.Path & "" & NomDoc
wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" &
NomDoc
End If
Else
wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" & NomDoc
End If
End If

Daniel


Avatar
Daniel.C
OK. c'est parce que la sauvegarde ajoute l'extension .doc au nom du
fichier.
Remplace donc le code que je t'ai fourni par :

If NomDoc <> "" Then
If Dir(ActiveWorkbook.Path & "" & NomDoc) & ".docx" <> "" Then
rep = MsgBox("Le fichier existe déjà; Voulez-vous l'écraser ?",
vbYesNo)
If rep = vbYes Then
Kill ActiveWorkbook.Path & "" & NomDoc
wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" & NomDoc
End If
Else
wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" & NomDoc
End If
End If

Daniel

Salut,
ben en fait, il ne me cré bien le fichier, mais le message lorsqu'il existe
deja le fichier n'apparait pas, et donc il ecrase l'autre.
Pour faire plus simple, si le fichier existe deja, le message "le fichier
existe déjà" s'affiche, sinon il cré le fichier.
et si le fichier existe deja, alors il l'affiche.
ma macri, avec un test pour ce que je viens de dire :
-----
Sub MacroWordExp()
'Avant de lancer cette macro, Dans l'éditeur VBA: Faire Menu / Outils /
'Reference / Cocher "Microsoft Word xx.xx Library"
Dim NomDoc As String
Set wordobj = CreateObject("Word.Application")
'Set wordobj = GetObject(, "Word.Application")
NomDoc = "Note de revue de l'expert Comptable"
wordobj.documents.Add Template:= _
"G:COLLypaDossiers de travailenteteYPA.dot", NewTemplate:úlse,
_ DocumentType:=0
'wordobj.Visible = True
wordobj.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
With wordobj.Selection
.MoveRight wdCell
.TypeText Text:=[DGA!B1].Value
.MoveRight wdCell, 3
.TypeText Text:=[DGA!G3].Value
.MoveRight wdCell, 2
.TypeText Text:=CStr([DGA!B2].Value)
.MoveRight wdCell
.TypeText Text:=[DGA!T1].Value
.MoveRight wdCell, 2
.TypeText Text:=[DGA!G4].Value
.MoveRight wdCell, 2
.TypeText Text:=CStr([DGA!B3].Value)
.MoveRight wdCell, 3
.TypeText Text:=[DGA!G5].Value
End With
wordobj.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
If NomDoc <> "" Then
If Dir(ActiveWorkbook.Path & "" & NomDoc) <> "" Then
rep = MsgBox("Le fichier existe déjà. Ouverture de ce fichier",
vbOKOnly)
wordobj.Visible = True
Else
wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" & NomDoc
End If
End If
'Set wordobj = Nothing
wordobj.Visible = True
End Sub
-----
"Daniel.C" a écrit :

Essaie comme ça :

If NomDoc <> "" Then
If Dir(ActiveWorkbook.Path & "" & NomDoc) <> "" Then
rep = MsgBox("Le fichier existe déjà; Voulez-vous l'écraser
?", vbYesNo)
If rep = vbYes Then
Kill ActiveWorkbook.Path & "" & NomDoc
wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" &
NomDoc
End If
Else
wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" & NomDoc
End If
End If

Daniel




Avatar
Sunburn
Salut daniel,
en fait, ça ne fonctionne pas comme je veux, car si j'ai un fichier word
alpha existe dans mon répertoire, il pense que c'est un double de celui qu'il
tente d'ouvrir.
Donc est-il possible de lui dire que si il trouve dans ce répertoire un
fichier "note.doc", alors il l'ouvre, sinon, il cré un doc sur le modèle.
j'ai testé un truc, mais ça marche pas.
Merci.
YANN
----
If NomDoc <> "" Then
If Dir(ActiveWorkbook.Path & "" & NomDoc) <> "" & ".doc" Then
rep = MsgBox("Le fichier existe déjà. Voulez-vous ouvrir l'existant
?", vbYesNo)
If rep = vbYes Then
dos = ActiveWorkbook.Path
'Kill ActiveWorkbook.Path & "" & NomDoc
'wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" & NomDoc
'wordobj.Visible = True
wordobj.Open Filename = dos & "" & NomDoc
End If
Else
wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" & NomDoc
End If
'wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" & NomDoc'
enlever pr test
'wordobj.Quit
End If
----

Merci.
YANN

"Daniel.C" a écrit :

OK. c'est parce que la sauvegarde ajoute l'extension .doc au nom du
fichier.
Remplace donc le code que je t'ai fourni par :

If NomDoc <> "" Then
If Dir(ActiveWorkbook.Path & "" & NomDoc) & ".docx" <> "" Then
rep = MsgBox("Le fichier existe déjà; Voulez-vous l'écraser ?",
vbYesNo)
If rep = vbYes Then
Kill ActiveWorkbook.Path & "" & NomDoc
wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" & NomDoc
End If
Else
wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" & NomDoc
End If
End If

Daniel

> Salut,
> ben en fait, il ne me cré bien le fichier, mais le message lorsqu'il existe
> deja le fichier n'apparait pas, et donc il ecrase l'autre.
> Pour faire plus simple, si le fichier existe deja, le message "le fichier
> existe déjà" s'affiche, sinon il cré le fichier.
> et si le fichier existe deja, alors il l'affiche.
> ma macri, avec un test pour ce que je viens de dire :
> -----
> Sub MacroWordExp()
> 'Avant de lancer cette macro, Dans l'éditeur VBA: Faire Menu / Outils /
> 'Reference / Cocher "Microsoft Word xx.xx Library"
> Dim NomDoc As String
> Set wordobj = CreateObject("Word.Application")
> 'Set wordobj = GetObject(, "Word.Application")
> NomDoc = "Note de revue de l'expert Comptable"
> wordobj.documents.Add Template:= _
> "G:COLLypaDossiers de travailenteteYPA.dot", NewTemplate:úlse,
> _ DocumentType:=0
> 'wordobj.Visible = True
> wordobj.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
> With wordobj.Selection
> .MoveRight wdCell
> .TypeText Text:=[DGA!B1].Value
> .MoveRight wdCell, 3
> .TypeText Text:=[DGA!G3].Value
> .MoveRight wdCell, 2
> .TypeText Text:=CStr([DGA!B2].Value)
> .MoveRight wdCell
> .TypeText Text:=[DGA!T1].Value
> .MoveRight wdCell, 2
> .TypeText Text:=[DGA!G4].Value
> .MoveRight wdCell, 2
> .TypeText Text:=CStr([DGA!B3].Value)
> .MoveRight wdCell, 3
> .TypeText Text:=[DGA!G5].Value
> End With
> wordobj.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
> If NomDoc <> "" Then
> If Dir(ActiveWorkbook.Path & "" & NomDoc) <> "" Then
> rep = MsgBox("Le fichier existe déjà. Ouverture de ce fichier",
> vbOKOnly)
> wordobj.Visible = True
> Else
> wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" & NomDoc
> End If
> End If
> 'Set wordobj = Nothing
> wordobj.Visible = True
> End Sub
> -----
> "Daniel.C" a écrit :
>
>> Essaie comme ça :
>>
>> If NomDoc <> "" Then
>> If Dir(ActiveWorkbook.Path & "" & NomDoc) <> "" Then
>> rep = MsgBox("Le fichier existe déjà; Voulez-vous l'écraser
>> ?", vbYesNo)
>> If rep = vbYes Then
>> Kill ActiveWorkbook.Path & "" & NomDoc
>> wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" &
>> NomDoc
>> End If
>> Else
>> wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" & NomDoc
>> End If
>> End If
>>
>> Daniel





Avatar
Daniel.C
Je ne comprends pas. Pourquoi parles-tu d'ouvrir un fichier. On est au
niveau de la sauvegarde ?
Daniel

Salut daniel,
en fait, ça ne fonctionne pas comme je veux, car si j'ai un fichier word
alpha existe dans mon répertoire, il pense que c'est un double de celui qu'il
tente d'ouvrir.
Donc est-il possible de lui dire que si il trouve dans ce répertoire un
fichier "note.doc", alors il l'ouvre, sinon, il cré un doc sur le modèle.
j'ai testé un truc, mais ça marche pas.
Merci.
YANN
----
If NomDoc <> "" Then
If Dir(ActiveWorkbook.Path & "" & NomDoc) <> "" & ".doc" Then
rep = MsgBox("Le fichier existe déjà. Voulez-vous ouvrir l'existant
?", vbYesNo)
If rep = vbYes Then
dos = ActiveWorkbook.Path
'Kill ActiveWorkbook.Path & "" & NomDoc
'wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" & NomDoc
'wordobj.Visible = True
wordobj.Open Filename = dos & "" & NomDoc
End If
Else
wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" & NomDoc
End If
'wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" & NomDoc'
enlever pr test
'wordobj.Quit
End If
----

Merci.
YANN

"Daniel.C" a écrit :

OK. c'est parce que la sauvegarde ajoute l'extension .doc au nom du
fichier.
Remplace donc le code que je t'ai fourni par :

If NomDoc <> "" Then
If Dir(ActiveWorkbook.Path & "" & NomDoc) & ".docx" <> "" Then
rep = MsgBox("Le fichier existe déjà; Voulez-vous l'écraser ?",
vbYesNo)
If rep = vbYes Then
Kill ActiveWorkbook.Path & "" & NomDoc
wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" & NomDoc
End If
Else
wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" & NomDoc
End If
End If

Daniel

Salut,
ben en fait, il ne me cré bien le fichier, mais le message lorsqu'il existe
deja le fichier n'apparait pas, et donc il ecrase l'autre.
Pour faire plus simple, si le fichier existe deja, le message "le fichier
existe déjà" s'affiche, sinon il cré le fichier.
et si le fichier existe deja, alors il l'affiche.
ma macri, avec un test pour ce que je viens de dire :
-----
Sub MacroWordExp()
'Avant de lancer cette macro, Dans l'éditeur VBA: Faire Menu / Outils /
'Reference / Cocher "Microsoft Word xx.xx Library"
Dim NomDoc As String
Set wordobj = CreateObject("Word.Application")
'Set wordobj = GetObject(, "Word.Application")
NomDoc = "Note de revue de l'expert Comptable"
wordobj.documents.Add Template:= _
"G:COLLypaDossiers de travailenteteYPA.dot",
NewTemplate:úlse, _ DocumentType:=0
'wordobj.Visible = True
wordobj.ActiveWindow.ActivePane.View.SeekView =
wdSeekCurrentPageHeader With wordobj.Selection
.MoveRight wdCell
.TypeText Text:=[DGA!B1].Value
.MoveRight wdCell, 3
.TypeText Text:=[DGA!G3].Value
.MoveRight wdCell, 2
.TypeText Text:=CStr([DGA!B2].Value)
.MoveRight wdCell
.TypeText Text:=[DGA!T1].Value
.MoveRight wdCell, 2
.TypeText Text:=[DGA!G4].Value
.MoveRight wdCell, 2
.TypeText Text:=CStr([DGA!B3].Value)
.MoveRight wdCell, 3
.TypeText Text:=[DGA!G5].Value
End With
wordobj.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
If NomDoc <> "" Then
If Dir(ActiveWorkbook.Path & "" & NomDoc) <> "" Then
rep = MsgBox("Le fichier existe déjà. Ouverture de ce fichier",
vbOKOnly)
wordobj.Visible = True
Else
wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" & NomDoc
End If
End If
'Set wordobj = Nothing
wordobj.Visible = True
End Sub
-----
"Daniel.C" a écrit :

Essaie comme ça :

If NomDoc <> "" Then
If Dir(ActiveWorkbook.Path & "" & NomDoc) <> "" Then
rep = MsgBox("Le fichier existe déjà; Voulez-vous l'écraser
?", vbYesNo)
If rep = vbYes Then
Kill ActiveWorkbook.Path & "" & NomDoc
wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" &
NomDoc
End If
Else
wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" & NomDoc
End If
End If

Daniel











Avatar
Sunburn
Salut,
euh je crois que je vais mettre noir sur blanc ce que j'ai compris et ce que
je voudrais avoir.
la premiere macro word que tu m'avais faite, impecable pour créer des
documents aux noms divers et variés.
J'ai essayé de l'adapter, pour un document word que je souhaite unique dans
mon "dossier", disons "machin.doc"
Donc, je veux un et un seul "machin.doc" dans le dossier où est sauvegardé
mon fichier excel actif.
Donc s'il n'existe pas de fichier "machin.doc", alors j'execute ma macro
pour le créer. S'il existe, alors je l'ouvre.
---
j'avais mal interprété la macro, donc il faut que je mette ma condition pour
savoir si le fichier "machin.doc" existe ou pas au tout debut de la macro.
je te redonne la macro actuelle, avec en commentaire '*** ce que je pense.
Merci. YANN
-----
Sub MacroWordExp()
'Avant de lancer cette macro, Dans l'éditeur VBA: Faire Menu / Outils /
'Reference / Cocher "Microsoft Word xx.xx Library"
'*** if Dir(ActiveWorkbook.Path & "" & "Machin.doc)
'*** then open "machin.doc"
'*** else
Dim NomDoc As String
Set wordobj = CreateObject("Word.Application")
'Set wordobj = GetObject(, "Word.Application")
NomDoc = "Note de revue de l'expert Comptable"
wordobj.documents.Add Template:= _
"G:COLLypaDossiers de travailenteteEC.dot", NewTemplate:úlse, _
DocumentType:=0
'wordobj.Visible = True
wordobj.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
With wordobj.Selection
.MoveRight wdCell
.TypeText Text:=[DGA!B1].Value
.MoveRight wdCell, 3
.TypeText Text:=[DGA!G3].Value
.MoveRight wdCell, 2
.TypeText Text:=CStr([DGA!B2].Value)
.MoveRight wdCell
.TypeText Text:=[DGA!T1].Value
.MoveRight wdCell, 2
.TypeText Text:=[DGA!G4].Value
.MoveRight wdCell, 2
.TypeText Text:=CStr([DGA!B3].Value)
.MoveRight wdCell, 3
.TypeText Text:=[DGA!G5].Value
End With
wordobj.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
'If NomDoc <> "" Then
'If Dir(ActiveWorkbook.Path & "" & NomDoc) <> "" & ".doc" Then
'rep = MsgBox("Le fichier existe déjà. Voulez-vous ouvrir
l'existant ?", vbYesNo)
'If rep = vbYes Then
'dos = ActiveWorkbook.Path
'Kill ActiveWorkbook.Path & "" & NomDoc
'wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" & NomDoc
'wordobj.Visible = True
'End If
'Else
'wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" & NomDoc
'End If
'wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" & NomDoc'
enlever pr test
'wordobj.Quit
End If
'Set wordobj = Nothing
wordobj.Visible = True
End Sub
-----
Avatar
Daniel.C
Essaie :

Sub MacroWordExp()
'Avant de lancer cette macro, Dans l'éditeur VBA: Faire Menu / Outils /
'Reference / Cocher "Microsoft Word xx.xx Library"
'*** if Dir(ActiveWorkbook.Path & "" & "Machin.doc)
'*** then open "machin.doc"
'*** else
Dim NomDoc As String
Set wordobj = CreateObject("Word.Application")
'Set wordobj = GetObject(, "Word.Application")
If Dir(ActiveWorkbook.Path & "" & "machin.doc") <> "" Then
wordobj.documents.Open ActiveWorkbook.Path & "" & "machin.doc"
wordobj.Visible = True
Else
wordobj.documents.Add Template:= _
"G:COLLypaDossiers de travailenteteEC.dot",
NewTemplate:úlse, _
DocumentType:=0
wordobj.Visible = True
wordobj.ActiveWindow.ActivePane.View.SeekView =
wdSeekCurrentPageHeader
With wordobj.Selection
.MoveRight wdCell
.TypeText Text:=[DGA!B1].Value
.MoveRight wdCell, 3
.TypeText Text:=[DGA!G3].Value
.MoveRight wdCell, 2
.TypeText Text:=CStr([DGA!B2].Value)
.MoveRight wdCell
.TypeText Text:=[DGA!T1].Value
.MoveRight wdCell, 2
.TypeText Text:=[DGA!G4].Value
.MoveRight wdCell, 2
.TypeText Text:=CStr([DGA!B3].Value)
.MoveRight wdCell, 3
.TypeText Text:=[DGA!G5].Value
End With
wordobj.ActiveWindow.ActivePane.View.SeekView =
wdSeekMainDocument
wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" &
"machin"
End If
'wordobj.Quit
'Set wordobj = Nothing
End Sub


Salut,
euh je crois que je vais mettre noir sur blanc ce que j'ai compris et ce que
je voudrais avoir.
la premiere macro word que tu m'avais faite, impecable pour créer des
documents aux noms divers et variés.
J'ai essayé de l'adapter, pour un document word que je souhaite unique dans
mon "dossier", disons "machin.doc"
Donc, je veux un et un seul "machin.doc" dans le dossier où est sauvegardé
mon fichier excel actif.
Donc s'il n'existe pas de fichier "machin.doc", alors j'execute ma macro
pour le créer. S'il existe, alors je l'ouvre.
---
j'avais mal interprété la macro, donc il faut que je mette ma condition pour
savoir si le fichier "machin.doc" existe ou pas au tout debut de la macro.
je te redonne la macro actuelle, avec en commentaire '*** ce que je pense.
Merci. YANN
-----
Sub MacroWordExp()
'Avant de lancer cette macro, Dans l'éditeur VBA: Faire Menu / Outils /
'Reference / Cocher "Microsoft Word xx.xx Library"
'*** if Dir(ActiveWorkbook.Path & "" & "Machin.doc)
'*** then open "machin.doc"
'*** else
Dim NomDoc As String
Set wordobj = CreateObject("Word.Application")
'Set wordobj = GetObject(, "Word.Application")
NomDoc = "Note de revue de l'expert Comptable"
wordobj.documents.Add Template:= _
"G:COLLypaDossiers de travailenteteEC.dot", NewTemplate:úlse, _
DocumentType:=0
'wordobj.Visible = True
wordobj.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
With wordobj.Selection
.MoveRight wdCell
.TypeText Text:=[DGA!B1].Value
.MoveRight wdCell, 3
.TypeText Text:=[DGA!G3].Value
.MoveRight wdCell, 2
.TypeText Text:=CStr([DGA!B2].Value)
.MoveRight wdCell
.TypeText Text:=[DGA!T1].Value
.MoveRight wdCell, 2
.TypeText Text:=[DGA!G4].Value
.MoveRight wdCell, 2
.TypeText Text:=CStr([DGA!B3].Value)
.MoveRight wdCell, 3
.TypeText Text:=[DGA!G5].Value
End With
wordobj.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
'If NomDoc <> "" Then
'If Dir(ActiveWorkbook.Path & "" & NomDoc) <> "" & ".doc" Then
'rep = MsgBox("Le fichier existe déjà. Voulez-vous ouvrir
l'existant ?", vbYesNo)
'If rep = vbYes Then
'dos = ActiveWorkbook.Path
'Kill ActiveWorkbook.Path & "" & NomDoc
'wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" & NomDoc
'wordobj.Visible = True
'End If
'Else
'wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" & NomDoc
'End If
'wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" & NomDoc'
enlever pr test
'wordobj.Quit
End If
'Set wordobj = Nothing
wordobj.Visible = True
End Sub
-----


Avatar
Sunburn
Ok, merci c'est impecable, c'est juste ce que je voulais.
J'ai juste rajouté une petite msgbox, pour informer l'utilisateur qu'il y a
deja une note de créée, et donc qu'il va l'ouvrir.
rep = MsgBox("Le fichier existe déjà, Excel va l'ouvrir", vbOKOnly)

Voilou.
Merci beaucoup encore, Daniel.

YANN


"Daniel.C" a écrit :

Essaie :

Sub MacroWordExp()
'Avant de lancer cette macro, Dans l'éditeur VBA: Faire Menu / Outils /
'Reference / Cocher "Microsoft Word xx.xx Library"
'*** if Dir(ActiveWorkbook.Path & "" & "Machin.doc)
'*** then open "machin.doc"
'*** else
Dim NomDoc As String
Set wordobj = CreateObject("Word.Application")
'Set wordobj = GetObject(, "Word.Application")
If Dir(ActiveWorkbook.Path & "" & "machin.doc") <> "" Then
wordobj.documents.Open ActiveWorkbook.Path & "" & "machin.doc"
wordobj.Visible = True
Else
wordobj.documents.Add Template:= _
"G:COLLypaDossiers de travailenteteEC.dot",
NewTemplate:úlse, _
DocumentType:=0
wordobj.Visible = True
wordobj.ActiveWindow.ActivePane.View.SeekView =
wdSeekCurrentPageHeader
With wordobj.Selection
.MoveRight wdCell
.TypeText Text:=[DGA!B1].Value
.MoveRight wdCell, 3
.TypeText Text:=[DGA!G3].Value
.MoveRight wdCell, 2
.TypeText Text:=CStr([DGA!B2].Value)
.MoveRight wdCell
.TypeText Text:=[DGA!T1].Value
.MoveRight wdCell, 2
.TypeText Text:=[DGA!G4].Value
.MoveRight wdCell, 2
.TypeText Text:=CStr([DGA!B3].Value)
.MoveRight wdCell, 3
.TypeText Text:=[DGA!G5].Value
End With
wordobj.ActiveWindow.ActivePane.View.SeekView =
wdSeekMainDocument
wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" &
"machin"
End If
'wordobj.Quit
'Set wordobj = Nothing
End Sub


> Salut,
> euh je crois que je vais mettre noir sur blanc ce que j'ai compris et ce que
> je voudrais avoir.
> la premiere macro word que tu m'avais faite, impecable pour créer des
> documents aux noms divers et variés.
> J'ai essayé de l'adapter, pour un document word que je souhaite unique dans
> mon "dossier", disons "machin.doc"
> Donc, je veux un et un seul "machin.doc" dans le dossier où est sauvegardé
> mon fichier excel actif.
> Donc s'il n'existe pas de fichier "machin.doc", alors j'execute ma macro
> pour le créer. S'il existe, alors je l'ouvre.
> ---
> j'avais mal interprété la macro, donc il faut que je mette ma condition pour
> savoir si le fichier "machin.doc" existe ou pas au tout debut de la macro.
> je te redonne la macro actuelle, avec en commentaire '*** ce que je pense.
> Merci. YANN
> -----
> Sub MacroWordExp()
> 'Avant de lancer cette macro, Dans l'éditeur VBA: Faire Menu / Outils /
> 'Reference / Cocher "Microsoft Word xx.xx Library"
> '*** if Dir(ActiveWorkbook.Path & "" & "Machin.doc)
> '*** then open "machin.doc"
> '*** else
> Dim NomDoc As String
> Set wordobj = CreateObject("Word.Application")
> 'Set wordobj = GetObject(, "Word.Application")
> NomDoc = "Note de revue de l'expert Comptable"
> wordobj.documents.Add Template:= _
> "G:COLLypaDossiers de travailenteteEC.dot", NewTemplate:úlse, _
> DocumentType:=0
> 'wordobj.Visible = True
> wordobj.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
> With wordobj.Selection
> .MoveRight wdCell
> .TypeText Text:=[DGA!B1].Value
> .MoveRight wdCell, 3
> .TypeText Text:=[DGA!G3].Value
> .MoveRight wdCell, 2
> .TypeText Text:=CStr([DGA!B2].Value)
> .MoveRight wdCell
> .TypeText Text:=[DGA!T1].Value
> .MoveRight wdCell, 2
> .TypeText Text:=[DGA!G4].Value
> .MoveRight wdCell, 2
> .TypeText Text:=CStr([DGA!B3].Value)
> .MoveRight wdCell, 3
> .TypeText Text:=[DGA!G5].Value
> End With
> wordobj.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
> 'If NomDoc <> "" Then
> 'If Dir(ActiveWorkbook.Path & "" & NomDoc) <> "" & ".doc" Then
> 'rep = MsgBox("Le fichier existe déjà. Voulez-vous ouvrir
> l'existant ?", vbYesNo)
> 'If rep = vbYes Then
> 'dos = ActiveWorkbook.Path
> 'Kill ActiveWorkbook.Path & "" & NomDoc
> 'wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" & NomDoc
> 'wordobj.Visible = True
> 'End If
> 'Else
> 'wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" & NomDoc
> 'End If
> 'wordobj.ActiveDocument.SaveAs ThisWorkbook.Path & "" & NomDoc'
> enlever pr test
> 'wordobj.Quit
> End If
> 'Set wordobj = Nothing
> wordobj.Visible = True
> End Sub
> -----