Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > Beginning VB 6
|
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 3rd, 2003, 03:49 PM
Registered User
 
Join Date: Sep 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Selecting multiple words in Word 2000

I currently have some VBA code in Word that is piece-mealed together from some Microsoft articles and suggestions from other people. What the code does is take an .rtf document that has been created from an Access 2000 report, make seperate documents out of each report, and save it with the first word of the document. However, I would like now to include more than 1 word in the file name. I would like to have up to 6 words, seperated by an _ . It would look something like:

Word1_word2_word3_word4_word5_word6.doc

The code I have is:

Dim rngTitle As Range
Dim strTitle As String


' Find "xyz" and replace with Section Break Next Page, remove Page Break
    Do
        Selection.Find.ClearFormatting
        With Selection.Find
            .Text = "xyz"
            .Forward = True
            .Wrap = wdFindContinue
        End With
        If Selection.Find.Execute = False Then
    Exit Do
        Else
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.MoveDown Unit:=wdLine, Count:=1
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.InsertBreak Type:=wdSectionBreakNextPage
        End If
    Loop

    Selection.HomeKey Unit:=wdStory

' Used to set criteria for moving through the document by section.
Application.Browser.Target = wdBrowseSection

'A mailmerge documents ends with a section break next page.
'Subtracting one from the section count stop error message.
For i = 1 To ((ActiveDocument.Sections.Count) - 1)

     'Select and copy the section text to the clipboard
     ActiveDocument.Bookmarks("\Section").Range.Cop y

     'Create a new document to paste text from clipboard.
     Documents.Add Template:="P:\650-00_ABC Company_2003\Reporting\Individual Reports.dot"
     Selection.Paste

' Removes the break that is copied at the end of the section, if any.
     Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
     Selection.Delete Unit:=wdCharacter, Count:=1
     Selection.Delete Unit:=wdCharacter, Count:=1
ChangeFileOpenDirectory "P:\650-00_ABC Company_2003\Reporting\Individual Reports"
        
     Selection.HomeKey Unit:=wdStory
     Selection.EndKey Unit:=wdLine, Extend:=wdExtend
     'Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
     Selection.Font.Color = wdColorWhite
     Selection.HomeKey Unit:=wdStory
    
     Set rngTitle = ActiveDocument.Range.Words(1)
     strTitle = rngTitle.Text
     ActiveDocument.SaveAs FileName:=strTitle & ".doc"
     ActiveDocument.Close
     ' Move the selection to the next section in the document
     Application.Browser.Next
     Next i
     ActiveDocument.Close savechanges:=wdDoNotSaveChanges


End Sub


I have limited coding knowledge, but I believe it is something in the Set rngTitle = ActiveDocument.Range.Words(1) that needs to be changed. I tried using
Set rngTitle = ActiveDocument.Range.Paragraphs(1), but I get a Runtime error that says "Type Mismatch". Any help would be GREATLY appreciated!!! Thanks.

Jeff





Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Selecting a picture in MS Word VBA zainhansrod VB How-To 1 January 2nd, 2007 03:39 AM
Selecting from SQL Server 2000 MAKO C# 6 March 1st, 2006 06:48 AM
ASP - Selecting Multiple Records Ei0nN Classic ASP Databases 2 May 28th, 2004 03:38 PM
search for multiple words keph Beginning PHP 5 April 6th, 2004 01:23 PM
macro for finding multiple words Lakshmi KS Access VBA 2 January 30th, 2004 02:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.