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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Word & VBA 1

Status
Not open for further replies.

vnk

IS-IT--Management
Nov 14, 2001
33
US
Hi,

I need to read a word document and delete few words,
blocks of lines and create a new file. Please help me with a sample script of vba. I have tried with vbscript but it
creates junk in the resulting file.
Any help is appreciated.
Thanks,
VNK
 
Here's some code to give you an idea of how things work in Word VBA.
Code:
Sub TestWordVBA()
Dim MyDoc As Object
Dim done As Boolean
Dim MyStr As String
    
    done = False              'Control value for loop
       
         'Setting search string
    MyStr = InputBox("Enter search string value: ", _
                     "Find String")

'Loop will execute as long as another search string exists
    While Not (done)
        Selection.Find.ClearFormatting
        With Selection.Find
            .Text = MyStr
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With

        'If another word is found, then continue
        If Not (Selection.Find.Execute) Then
            done = True
        Else
        'Deleting search string from document
            Selection.Delete Unit:=wdCharacter, Count:=1
        End If
    Wend
    
    'Creating new/blank document
    Set MyDoc = Documents.Add
    Selection.TypeText Text:="This is my new document."
End Sub
About 90% of this code was generated via the macro recorder (Tools>Macro>Record New Macro...). All you do is run the recorder and do manually whatever you'd like your macro to do automatically. When you're done, just hit the stop button. Then, look at the code (see here if you don't know how to: faq68-1271) and see if you can make out how things work. ----------------------------------------
If you are reading this, then you have read too far... :p

lightwarrior@hotmail.com
 
Thanks Logius for the input,
I have replaced the required fields, but now I wanna
format it to right.alignment and I have tried using
"Selection.ParagraphFormat.Alignment = wdAlignParagraphRight" but it doesnt seems working.

Any input????
 
I tried the same code, and it worked for me. All I did was enter it right after the last line of code. If you'd like, go ahead and email me the document and I'll take a look at it. ----------------------------------------
If you are reading this, then you have read too far... :p

lightwarrior@hotmail.com
 
Selection.ParagraphFormat.Alignment = wdAlignParagraphRight will only right align the Selection. I would not recommend using Selection as it is difficult to be certain of what it contains (especially in tables).

Use:

<document reference>.Paragraphs.Alignment=wdAlignParagraphRight

to right justify the whole document.

or replace <document reference> with any other range type object.

M :)
 
Hey, thanks, Mossoft. I've only been working with Word VBA for a short time now (Excel is really my thing) and didn't know about that. ----------------------------------------
If you are reading this, then you have read too far... :p

lightwarrior@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top