DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2005
    Posts
    1

    Question Using Word commands through an Excel macro

    Hey all,
    I am working on a project that requires a macro to run in Excel, open a Word document, and copy a specific selection [which is being found through a crude
    search] of that document to another document. The problem I am having is in the copying part. I am trying to use commands that work when run as a Word macro (eg, Selection.Information (wdFirstCharacterLineNumber) and ActiveDocument.Range(0, Selection.Paragraphs(1).Range.End).Paragraphs.Count ), yet each one gives an error when run through Excel. Anyone have any idea how to get this to work? Here is the code:

    Sub testsearch4()

    Dim intX As Integer, intY As Integer, intZ As Range


    Selection.Find.ClearFormatting

    With Selection.Find

    .Text = "[EA]"

    .Replacement.Text = ""

    .Forward = True

    .Wrap = wdFindContinue

    .Format = False

    .MatchCase = False

    .MatchWholeWord = False

    .MatchWildcards = False

    .MatchSoundsLike = False

    .MatchAllWordForms = False

    End With

    Selection.Find.Execute


    X = ActiveDocument.Range(0, Selection.Paragraphs(1).Range.End).Paragraphs.Count


    Selection.Find.ClearFormatting

    With Selection.Find

    .Text = "[/EA]"

    .Replacement.Text = ""

    .Forward = True

    .Wrap = wdFindContinue

    .Format = False

    .MatchCase = False

    .MatchWholeWord = False

    .MatchWildcards = False

    .MatchSoundsLike = False

    .MatchAllWordForms = False

    End With

    Selection.Find.Execute


    Y = ActiveDocument.Range(0, Selection.Paragraphs(1).Range.End).Paragraphs.Count


    Set myRange = ActiveDocument.Range( _

    Start:=ActiveDocument.Paragraphs(X + 1).Range.Start, _

    End:=ActiveDocument.Paragraphs(Y - 1).Range.End)

    myRange.Select


    Selection.Copy


    ActiveWindow.Close

    Windows("MyTest.doc").Activate

    Selection.EndKey Unit:=wdLine

    Selection.TypeParagraph

    Selection.Paste

    ActiveDocument.Save

    End Sub

    This works in Office 2000. Thank you for your help

  2. #2
    Join Date
    Apr 2004
    Location
    New York City
    Posts
    538
    Sounds like you need to add a reference to the Word object model in your Excel project: TOOLS menu, REFERENCES, select "Microsoft Word version number Object Library" and click OK.

    -Andrew

Similar Threads

  1. word macro problem : No reply
    By namella in forum Mobile
    Replies: 0
    Last Post: 08-25-2005, 07:27 AM
  2. Word Macro Problem
    By namella in forum VB Classic
    Replies: 0
    Last Post: 08-20-2005, 01:46 AM
  3. Matching data macro in VBA Excel
    By Werner in forum VB Classic
    Replies: 2
    Last Post: 06-15-2005, 02:13 PM
  4. VB, Excel, word
    By VAX in forum VB Classic
    Replies: 4
    Last Post: 10-12-2000, 09:46 AM
  5. VB, Excel, word
    By VAX in forum VB Classic
    Replies: 0
    Last Post: 10-10-2000, 11:52 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links