Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
My Word/VBA problem
Message
 
À
05/01/2001 10:50:41
Information générale
Forum:
Visual FoxPro
Catégorie:
Fonctions Windows API
Divers
Thread ID:
00459701
Message ID:
00459874
Vues:
27
>Hi Della, tnx for caring to help me with Word/VBA - it's one of my bad dreams to work with..

It can be frustrating, I agree! < S >

>I have solved most of my problem by the good old hard work, try and try again,
>but a slight problem remains. What I am doing:
>
>A mail merge creates my main document. Then, I want to insert a number of lines immediately following the present text. I now realize that I actually open the document, seems I must have saved if first. Guess that doesn't matter.
>This is the relevant code:
>
># Define wdLine 5
>with oWord
>.Documents.Open(myDoc)
> .Visible = .t.
> .Application.Activate
> .Selection.MoveDown(wdLine,5)
> .selection.TypeText(subs(mystring,1, 90))
>endwith
>
>Now, what remains is to perform the same operation, but without making the document window visible.
>
>Remove the .Visible = .t. statement, and I get error: Member SELECTION does not evaluate to an object
>
>Remove the .Activate statement too, and the program works – but the text is inserted at the top instead of at bottom of the document.


The Selection object contains the cursor and any selected text. If there's no display (.Visible = .F.), there's no cursor/selected text -- at least in Word's mind. So you need to use the Range object, instead.

The Range object doesn't work in lines, so you can't "arrow down" five lines. Instead, you can move by sentences, paragraphs, characters, or other items. So you could say:
oRange = oWord.ActiveDocument.Paragraphs[3].Range
This gets you the entire third paragraph (the whole thing). You can also access the Sentences collection instead of the Paragraphs collection. Note that these start counting from the first Paragraph/Sentence in the document.

Note that you have selected the whole paragraph (or Sentence, depending on the collection used). To get your cursor to the end of the paragraph, you collapse the range, like this:
#DEFINE wdCollapseEnd 0
oRange.Collapse(wdCollapseEnd)
Now you can set the text of the range by issuing:
oRange.Text = subs(mystring,1, 90)
Play around with these, and see what you get. Feel free to ask more questions!

- della
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform