Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setting text in word 3

Status
Not open for further replies.

ccaathl

MIS
Mar 7, 2001
35
US
Hi,
I am struggling to get the following task to work:
writing a .vbs file to sit on a user's desktop.
When clicked it opens a particular Word File, selects all the text and makes it all bold.
I am told it is possible but for the life of me can find no info on the net nor in my Wrox W Script Host book.
If anyone can lend a few pointers I'd be most appreciative.
Thanks in advance,
Tom
 
You would have much been better off asking this in the MS Office Forum but since I've been automating Word from VB6 for awhile I'll give you a hint. Go to and enter "word vbscript" in the search box on the very first page. You'll get a meager 100 references of which I see 30 as being right on point. Don't restrict yourself to reading about VBScript sice you can convert much VB code to VBScript.

How do you find out how to Highlight etc. from code? CHEAT. Make Word show how it is done. Get into Word at the point where you want something done by code and RECORD a Macro. Stre it with the document. Now use the VB editor in Word to look at the genertated code. There you have you "example". VBScript does not support Keywprd parameters but use the Office Doc on objects, methods and function in MSDN to translate.
 
Here is the macro.
Code:
Sub Test()
'
' Test Macro
' Macro recorded 04/23/01 by John S. Yingling
'
    Selection.WholeStory
    Selection.Font.Bold = wdToggle
End Sub
Problem. Selection can not be addressed outside a macro in this manner so while in the macro editor I put APPLICATION. in front, got a list of objects, tried ActiveDocument but it does not have a Selection object so I tried ActiveWindow and it does. By stepping into the Macro with debug and putting the cursor over wdToggle I see that its value is 9999998. Here is the result.

Code:
Sub Test()
Const wdToggle = 9999998

    With Application.ActiveWindow
        .Selection.WholeStory
        .Selection.Font.Bold = wdToggle
    End With
End Sub

 
I've never done it in VBScript :) so I had to prove I knew of which I spoke.
This script works on a Win '95 / Word '97 machine.
Code:
Dim Document
Dim Application
Set Application = CreateObject("Word.Application")
Application.Visible = TRUE
Application.Documents.Open("D:\VBForums\Hello World.doc")

Const wdToggle = 9999998

    With Application.ActiveWindow
        .Selection.WholeStory
        .Selection.Font.Bold = wdToggle
    End With
' WORD Stays open unless you QUIT.
'''''Application.ActiveWindow.Close false ' Close, Do not save changes
'''''Application.Quit false ' do not save changes
CreateObject always creates a new instance.
 
You beat me to it!! Mine was a little more simple...

Dim myWord
Set myWord = CreateObject("Word.Application")

With myWord
.Documents.Open "d:\myworddoc.doc", , False
.Selection.WholeStory
.Selection.Font.Bold = True
.Visible = True
.Documents(1).Save
.Quit 0
End With If my post was helpful, please Mark it below. Thanks! - Al
 
cheers guys! that was very helpful of you both.
Thanks again, Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top