IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VBA Discussion :

Passer de Visual basic a Visual Studio


Sujet :

VBA

  1. #1
    Membre habitué Avatar de ac/dc
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Août 2006
    Messages
    369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Industrie

    Informations forums :
    Inscription : Août 2006
    Messages : 369
    Points : 197
    Points
    197
    Par défaut Passer de Visual basic a Visual Studio
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
     
    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. #2
    Rédacteur
    Avatar de DarkVader
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2002
    Messages
    2 130
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2002
    Messages : 2 130
    Points : 3 118
    Points
    3 118
    Par défaut
    Citation Envoyé par ac/dc Voir le message
    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.
    .../...
    J'imagine que tu veux dire :
    « 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.

  3. #3
    Membre habitué Avatar de ac/dc
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Août 2006
    Messages
    369
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Industrie

    Informations forums :
    Inscription : Août 2006
    Messages : 369
    Points : 197
    Points
    197
    Par défaut
    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. #4
    Invité
    Invité(e)
    Par défaut
    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

Discussions similaires

  1. visual basic ou visual basic express
    Par s4mk1ng dans le forum VB 6 et antérieur
    Réponses: 2
    Dernier message: 04/07/2008, 08h15
  2. visual basic ou visual basic office
    Par hono dans le forum Microsoft Office
    Réponses: 11
    Dernier message: 27/02/2008, 15h00
  3. Réponses: 6
    Dernier message: 22/01/2008, 14h57

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo