2010-01-07 17 views
1

Je crée un document Word par programme en utilisant VBA.

Comment faire une chaîne en gras

1) J'ai une chaîne avec la valeur - "Stratégie". Je veux le rendre gras et être affiché dans le document Word.

J'ai essayé ci-dessous, mais le texte ne change jamais:

Dim wrdApp As Word.Application 
Dim wrdDoc As Word.Document 
Set wrdApp = CreateObject("Word.Application") 

Dim strategy As String 
strategy = "STRATEGY" 
Dim objWdRange As Word.Range 


wrdApp.Visible = True 
Set wrdDoc = wrdApp.Documents.Open("C:\Program Files\DailyStrategy.doc") 


With wrdDoc 
    If wrdDoc.Bookmarks.Exists("MarketCommentry") Then 
     wrdDoc.Bookmarks("MarketCommentry").Range.Text = strategy 

    Set objWdRange = wrdDoc.Content 
    With objWdRange.Find 
     .ClearFormatting 
     .Replacement.ClearFormatting 
     .Text = "STRATEGY" 

     'Make found bold and italic 
     With .Replacement.Font 
      .Bold = True 
      .Italic = True 
     End With 
     .Execute Replace:=wdReplaceAll 
    End With 
End With 

End If ce qui a trait

Kojo

Répondre

1

EDIT:: Je aurais dû mieux A fait dans le débogueur VBA en premier, ce que j'ai fait maintenant. Celui-ci devrait fonctionner:

With wrdDoc 
    Set objWdRange = wrdDoc.Content 
    With objWdRange.Find 
     .ClearFormatting 
     .Text = "STRATEGY" 
     .Execute Replace:=wdReplaceNone 
    End With 
End With 

If objWdRange.Find.Found Then 
    'Make found bold and italic 
    With objWdRange.Font 
     .Bold = True 
     .Italic = True 
    End With 
End If 
+0

Voulez-vous dire que je devrais l'appeler juste après cette ligne? Avec objWdRange.Find .Execute Remplacer: = wdReplaceAll – Kojof

+0

* .Execute * devrait trouver et sélectionner votre texte. Ensuite, vous pouvez appliquer vos modifications à la police. Si vous ne sélectionnez rien d'abord, changer la police est inutile. J'ai ajouté les changements pertinents en guise d'exemple à ma réponse. –

+0

Salut, merci de répondre, cela a plus de sens. Cependant, cette ligne ne compile pas wrdDoc.Selection.Font. Je l'ai changé pour wrdApp.Selection.Font, mais il ne change toujours pas le texte quand j'exécute l'application. – Kojof