![]() |
| Le forum de référence en programmation et développement. Articles, cours et tutoriels du débutant au chef de projet et DBA confirmé. | |||||||
|
|||||||
| Général VBA Forum général VBA . Pour les logiciels spécifiques (Access, Excel, Word, ...), postez dans les bons sous forums. |
![]() |
|
|
Outils de la discussion |
|
|
#1 (permalink) |
|
Membre Confirmé
![]() Date d'inscription: août 2006
Âge: 21
Messages: 261
|
Bonjour, J'ai une macro dans Visual Basic que je souhaite passer dans un projet Visual Basic de Visual Studio.
Lorsque je créer mon projet et que j'ajoute les mêmes références, j'ai encore des erreurs de recompile. Comment dois-je faire ? Merci ! Ma macro dans Visual Basic à passer dans Visual studio : Code :
Sub GenereMacros() ' ' GenereMacros Macro ' Macro créée le 16/10/2008 par acer ' Dim Fichier As String, Direction As String ' -------------- REPERTOIRE DES FICHIERS WORD ------------------------ Direction = "C:\FichiersWord" ' -------------------------------------------------------------------- Dim Debut As Integer, Lignes As Integer, X As Integer Dim Doc As Document Dim NewModule As VBIDE.VBComponent Application.ScreenUpdating = False ' Fichier .dot Fichier = Dir(Direction & "\*.dot") Do While Fichier <> "" Set Doc = Documents.Open(Direction & "\" & Fichier) Set NewModule = Doc.VBProject.VBComponents _ .Add(vbext_ct_StdModule) NewModule.Name = "MyModule" With Doc.VBProject.VBComponents("MyModule").CodeModule X = .CountOfLines .InsertLines X + 1, "Sub AutoOpen()" .InsertLines X + 2, "' AutoOpen Macro" .InsertLines X + 3, "' Macro created 05/10/2008 by acer" .InsertLines X + 4, "Dim aStory As Range" .InsertLines X + 5, "Dim aField As Field" .InsertLines X + 6, "Dim tableau(1000) As String" .InsertLines X + 7, "Dim i As Integer" .InsertLines X + 8, "Dim majOK As Boolean" .InsertLines X + 9, "Dim cptTableau As Integer" .InsertLines X + 10, "cptTableau = 0" .InsertLines X + 11, "For Each aStory In ActiveDocument.StoryRanges" .InsertLines X + 12, "For Each aField In aStory.Fields" .InsertLines X + 13, "majOK = True" .InsertLines X + 14, "For i = 0 To cptTableau" .InsertLines X + 15, "If UCase(tableau(i)) = UCase(aField.Code.Text) Then" .InsertLines X + 16, "majOK = False" .InsertLines X + 17, "End If" .InsertLines X + 18, "Next i" .InsertLines X + 19, "If majOK = True Then" .InsertLines X + 20, "aField.Update" .InsertLines X + 21, "If InStr(aField.Code.Text, ""ASK"") <> 0 Then" .InsertLines X + 22, "tableau(cptTableau) = aField.Code.Text" .InsertLines X + 23, "cptTableau = cptTableau + 1" .InsertLines X + 24, "End If" .InsertLines X + 25, "End If" .InsertLines X + 26, "Next aField" .InsertLines X + 27, "Next aStory" .InsertLines X + 28, "ActiveWindow.Activate" .InsertLines X + 29, "ActiveWindow.WindowState = wdWindowStateMaximize" .InsertLines X + 30, "If (ActiveWindow.View.ShowFieldCodes) Then" .InsertLines X + 31, "ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes" .InsertLines X + 32, "End If" .InsertLines X + 33, "End Sub" End With DoEvents Doc.Close True Set Doc = Nothing Fichier = Dir Loop ' Fichier .doc Fichier = Dir(Direction & "\*.doc") Do While Fichier <> "" Set Doc = Documents.Open(Direction & "\" & Fichier) Set NewModule = Doc.VBProject.VBComponents _ .Add(vbext_ct_StdModule) NewModule.Name = "MyModule" With Doc.VBProject.VBComponents("MyModule").CodeModule X = .CountOfLines .InsertLines X + 1, "Sub AutoOpen()" .InsertLines X + 2, "' AutoOpen Macro" .InsertLines X + 3, "' Macro created 05/10/2008 by acer" .InsertLines X + 4, "Dim aStory As Range" .InsertLines X + 5, "Dim aField As Field" .InsertLines X + 6, "Dim tableau(1000) As String" .InsertLines X + 7, "Dim i As Integer" .InsertLines X + 8, "Dim majOK As Boolean" .InsertLines X + 9, "Dim cptTableau As Integer" .InsertLines X + 10, "cptTableau = 0" .InsertLines X + 11, "For Each aStory In ActiveDocument.StoryRanges" .InsertLines X + 12, "For Each aField In aStory.Fields" .InsertLines X + 13, "majOK = True" .InsertLines X + 14, "For i = 0 To cptTableau" .InsertLines X + 15, "If UCase(tableau(i)) = UCase(aField.Code.Text) Then" .InsertLines X + 16, "majOK = False" .InsertLines X + 17, "End If" .InsertLines X + 18, "Next i" .InsertLines X + 19, "If majOK = True Then" .InsertLines X + 20, "aField.Update" .InsertLines X + 21, "If InStr(aField.Code.Text, ""ASK"") <> 0 Then" .InsertLines X + 22, "tableau(cptTableau) = aField.Code.Text" .InsertLines X + 23, "cptTableau = cptTableau + 1" .InsertLines X + 24, "End If" .InsertLines X + 25, "End If" .InsertLines X + 26, "Next aField" .InsertLines X + 27, "Next aStory" .InsertLines X + 28, "ActiveWindow.Activate" .InsertLines X + 29, "ActiveWindow.WindowState = wdWindowStateMaximize" .InsertLines X + 30, "If (ActiveWindow.View.ShowFieldCodes) Then" .InsertLines X + 31, "ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes" .InsertLines X + 32, "End If" .InsertLines X + 33, "End Sub" End With DoEvents Doc.Close True Set Doc = Nothing Fichier = Dir Loop Application.ScreenUpdating = True End Sub
__________________
Let there be light, sound, drums, guitar ... Let there be rock ! |
|
|
|
|
|
#2 (permalink) | |
|
Membre expérimenté
![]() Date d'inscription: mai 2002
Localisation: . . . . . . . . . . . . . Consternation du Sans tort
Messages: 941
|
Citation:
« J'ai un code VBA sous Word que je souhaite passer en Visual Basic » Si tel est le cas, au regard du code fourni, il faut passer par un projet «Addin» afin de pouvoir récupérer l'instance VBA active.
__________________
« Heureux soient les fêlés, car ils laisseront passer la lumière. » Pensez aux liens Rechercher - Google - Google Labs - AllApi et avant de poster : « A lire » |
|
|
|
|
|
|
#3 (permalink) |
|
Membre Confirmé
![]() Date d'inscription: août 2006
Âge: 21
Messages: 261
|
Oui, en fait j'ai ma macro dans Visual Basic lorsque je clique sur Macro dans le Menu de Word. Je voudrais créer une application qui permet d'ajouter une macro a plusieurs documents word.
Pour cela, je part donc de ma macro actuelle, et je veux la passer dans un projet de Visual Studio. Il faut pas que je créer un projet Visual Basic ?
__________________
Let there be light, sound, drums, guitar ... Let there be rock ! |
|
|
|
|
|
#4 (permalink) |
![]() |
Bonjour
Quelques petits détails pourraient être important : Quelle est ta version de Visual Studio ? Ensuite en général il faut travailler par Automation pour piloter une application. Le copier coller tel quel d'un code VBA a peu de chances de réussir, il faut rajouter quelques éléments pour que cela fonctionne. De plus je ne pense pas que tu sois dans le bon forum, en fonction de tes réponses, on t'orientera sûrement vers le forum qui est la version de ton Visual Studio. Philippe |
|
|
|
|
![]() |
![]() |
||
Passer de Visual basic a Visual Studio
|
||
| Outils de la discussion | |
|
|