Sub TexteCellule() Dim xlApp As Excel.Application Dim xlWb As Excel.Workbook Dim xlSh As Excel.Worksheet Dim sttemp As String Set xlApp = New Excel.Application Set xlWb = xlApp.Workbooks.Open("c:\temp\a.xlsx") Set xlSh = xlWb.Sheets(1) sttemp = xlSh.Cells(1, 1) 'Trf du contenu de la cellule vers le signet Call BookmarkNewValue("S1",sttemp) Debug.Print sttemp xlWb.Close xlApp.Quit Set xlWb = Nothing Set xlApp = Nothing End Sub Sub BookmarkNewValue(ByVal NomSignet As String, ByVal NouvelleValeurSignet As String) If ActiveDocument.Bookmarks.Exists(NomSignet) Then ActiveDocument.Bookmarks(NomSignet).Select Selection = NouvelleValeurSignet 'le bug est ici... le remplacement de la valeur supprime le signet Selection.Bookmarks.Add Name:=NomSignet, Range:=Selection.Range Selection.MoveRight Unit:=wdCharacter, Count:=1 End If End Sub
En effet, j'aimerais que quand j'exécute la macro, le texte contenu dans les cellules ne s'ajoute pas au texte apparu lors d'une exécution précédente de la macro mais remplace le texte existant par le nouveau (est ce clair ?).
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionActiveDocument.Bookmarks.Add "S1", xlSh.Cells(1, 1)
Sub BookmarkNewValue(ByVal NomSignet As String, ByVal NouvelleValeurSignet As String) If ActiveDocument.Bookmarks.Exists(NomSignet) Then ActiveDocument.Bookmarks(NomSignet).Select Selection = NouvelleValeurSignet 'le bug est ici... le remplacement de la valeur supprime le signet Selection.Bookmarks.Add Name:=NomSignet, Range:=Selection.Range Selection.MoveRight Unit:=wdCharacter, Count:=1 End If End Sub
Sub MaMacro() '... Call BookmarkNewValue("S1",xlSh.Cells(1,1)) '... End Sub
Sub TexteCelluleS1() Dim xlApp As Excel.Application Dim xlWb As Excel.Workbook Dim xlSh As Excel.Worksheet Dim sttemp As String Set xlApp = New Excel.Application Set xlWb = xlApp.Workbooks.Open("c:\temp\a.xlsx") Set xlSh = xlWb.Sheets(1) sttemp = xlSh.Cells(1, 1) 'Trf du contenu de la cellule vers le signet Call BookmarkNewValue("S1",sttemp) Debug.Print sttemp xlWb.Close xlApp.Quit Set xlWb = Nothing Set xlApp = Nothing End Sub Sub BookmarkNewValue(ByVal S1 As String, ByVal NouvelleValeurSignet As String) If ActiveDocument.Bookmarks.Exists(S1) Then ActiveDocument.Bookmarks(S1).Select Selection = NouvelleValeurSignet 'le bug est ici... le remplacement de la valeur supprime le signet Selection.Bookmarks.Add Name:=S1, Range:=Selection.Range Selection.MoveRight Unit:=wdCharacter, Count:=1 End If End Sub ________________________________________________________________________ Sub TexteCelluleS2() Dim xlApp As Excel.Application Dim xlWb As Excel.Workbook Dim xlSh As Excel.Worksheet Dim sttemp As String Set xlApp = New Excel.Application Set xlWb = xlApp.Workbooks.Open("c:\temp\a.xlsx") Set xlSh = xlWb.Sheets(1) sttemp = xlSh.Cells(2, 1) 'Trf du contenu de la cellule vers le signet Call BookmarkNewValue("S2",sttemp) Debug.Print sttemp xlWb.Close xlApp.Quit Set xlWb = Nothing Set xlApp = Nothing End Sub _______________________________________________________________________ Sub BookmarkNewValue(ByVal S2 As String, ByVal NouvelleValeurSignet As String) If ActiveDocument.Bookmarks.Exists(S2) Then ActiveDocument.Bookmarks(S2).Select Selection = NouvelleValeurSignet 'le bug est ici... le remplacement de la valeur supprime le signet Selection.Bookmarks.Add Name:=S2, Range:=Selection.Range Selection.MoveRight Unit:=wdCharacter, Count:=1 End If End Sub
Set xlWb = xlApp.Workbooks.Open("c:\temp\a.xlsx")
Sub MaMacro()'Sub appelante Dim Ligne as Integer For Ligne=1 to 2 'pour les 2 premieres lignes par exemple Call TexteCellule(Ligne) Next End Sub Sub TexteCellule(Byval Ligne As Integer) Dim xlApp As Excel.Application Dim xlWb As Excel.Workbook Dim xlSh As Excel.Worksheet Dim sttemp As String Set xlApp = New Excel.Application Set xlWb = xlApp.Workbooks.Open("c:\temp\a.xlsx") Set xlSh = xlWb.Sheets(1) sttemp = xlSh.Cells(Ligne, 1) 'Trf du contenu de la cellule vers le signet Call BookmarkNewValue("S" & Ligne,sttemp) 'évidemment si tes signets s'appellent S1, S2, S3 ect Debug.Print sttemp xlWb.Close xlApp.Quit Set xlWb = Nothing Set xlApp = Nothing End Sub Sub BookmarkNewValue(ByVal NomSignet As String, ByVal NouvelleValeurSignet As String) If ActiveDocument.Bookmarks.Exists(NomSignet) Then ActiveDocument.Bookmarks(NomSignet).Select Selection = NouvelleValeurSignet 'le bug est ici... le remplacement de la valeur supprime le signet Selection.Bookmarks.Add Name:=NomSignet, Range:=Selection.Range Selection.MoveRight Unit:=wdCharacter, Count:=1 End If End Sub
Sub TexteCellule() Dim xlApp As Excel.Application Dim xlWb As Excel.Workbook Dim xlSh As Excel.Worksheet Dim sttemp As String Dim Ligne As Integer Set xlApp = New Excel.Application Set xlWb = xlApp.Workbooks.Open("c:\temp\a.xlsx") Set xlSh = xlWb.Sheets(1) For Ligne = 1 To 3 'pour les 3 premières lignes pour l'autre exemple sttemp = xlSh.Cells(Ligne, 1) 'Trf du contenu de la cellule vers le signet Call BookmarkNewValue("S" & Ligne, sttemp) 'évidemment si tes signets s'appellent S1, S2, S3 ect 'Debug.Print sttemp Next xlWb.Close xlApp.Quit Set xlWb = Nothing Set xlApp = Nothing End Sub Sub BookmarkNewValue(ByVal NomSignet As String, ByVal NouvelleValeurSignet As String) If ActiveDocument.Bookmarks.Exists(NomSignet) Then ActiveDocument.Bookmarks(NomSignet).Select Selection = NouvelleValeurSignet 'le bug est ici... le remplacement de la valeur supprime le signet Selection.Bookmarks.Add Name:=NomSignet, Range:=Selection.Range Selection.MoveRight Unit:=wdCharacter, Count:=1 End If End Sub
Est-il possible de ne pas recopier cette ligne de code en l'intégrant une bonne fois pour toute ?
Sub BookmarkNewValue(ByVal NomSignet As String, ByVal NouvelleValeurSignet As String) If ActiveDocument.Bookmarks.Exists(NomSignet) Then ActiveDocument.Bookmarks(NomSignet).Select Selection = NouvelleValeurSignet 'le bug est ici... le remplacement de la valeur supprime le signet Selection.Bookmarks.Add Name:=NomSignet, Range:=Selection.Range Selection.MoveRight Unit:=wdCharacter, Count:=1 End If End Sub
Call BookmarkNewValue(numéro,sttemp)
Call BookmarkNewValue(marché,sttemp)
pffff ! désolé de le dire mais c'est parfois découragent, mais bon...