Wrox Programmer Forums
|
Word VBA Discuss using VBA to program Word.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Word VBA 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 May 11th, 2009, 07:34 AM
Registered User
Points: 5, Level: 1
Points: 5, Level: 1 Points: 5, Level: 1 Points: 5, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Excel to Word VBA

Hi guys,

I was hoping you could help me with something quickly. I have the following script:

Code:
Sub rejection()

        ' word document object creation
        Dim WordApp As Object

        Dim i As Integer, j As Integer, numRows As Integer


        ' show the actions that the macro is performing for now
                Application.ScreenUpdating = False
       ' the submissions worksheet must be selected
        Set Data = ActiveSheet.Range("A1")
        numRows = ActiveSheet.UsedRange.Rows.Count

        For i = 1 To numRows

            ' pull column for each row into dynamic strings
            rejectType = Data.Offset(i - 1, 4).Value
            authorString = Data.Offset(i - 1, 1).Value
            insightsString = Data.Offset(i - 1, 6).Value
            alternateJournalString = Data.Offset(i - 1, 5).Value

            ' if no value for field
            If rejectType <> "quality" And rejectType <> "Subject" Then
            GoTo Nextif
            End If
            


            If rejectType = "quality" And qualityRejectionTemplateFile = "" Then
                MsgBox ("Please locate Quality-based rejection letter template file")
                qualityRejectionTemplateFile = Application.GetOpenFilename
            ElseIf rejectType = "Subject" And subjectRejectionTemplateFile = "" Then
                MsgBox ("Please locate Subject-based rejection letter template file")
                subjectRejectionTemplateFile = Application.GetOpenFilename
            End If


            ' create rejection letter
            Set WordApp = CreateObject("Word.Application")

            With WordApp
                Application.StatusBar = "Creating " & rejectType & "rejection letter from template"
                .Visible = True

                ' should be .dot, a template file
                ' should be placed in the same directory as the excel file that this is being called from

                If rejectType = "quality" Then
                    .Documents.Open qualityRejectionTemplateFile
                ElseIf rejectType = "Subject" Then
                    .Documents.Open subjectRejectionTemplateFile
                End If

                ' clear out find and replace fields
                .Selection.Find.ClearFormatting
                .Selection.Find.Replacement.ClearFormatting

                ' find and replace the name in the document
                With .Selection.Find
                    .Text = "<Name>"
                    .Replacement.Text = authorString
                    .Wrap = wdFindStop
                    .Execute Replace:=wdReplaceAll
                End With

                ' clear out find and replace fields
                .Selection.Find.ClearFormatting
                .Selection.Find.Replacement.ClearFormatting

                ' find and replace the Insights in the document
                With .Selection.Find
                    .Text = "<insights>"
                    .Replacement.Text = insightsString
                    .Wrap = wdFindStop
                End With

                .Selection.Find.Execute Replace:=wdReplaceAll

                ' clear out find and replace fields
                .Selection.Find.ClearFormatting
                .Selection.Find.Replacement.ClearFormatting

                ' find and replace the Alternate Journal name in the document
                With .Selection.Find
                    .Text = "<Alternate Journal>"
                    .Replacement.Text = alternateJournalString
                End With

                .Selection.Find.Execute Replace:=wdReplaceAll



                ' save document with appropriate name and close
                .ActiveDocument.SaveAs Filename:=authorString & "_reject_" & rejectType & ".doc"
                .ActiveWindow.Close
                ' Kill the object
                .Quit

End With

Nextif:
Next i

End Sub
For the life of me, I cannot figure out why the Replacement.Text function doesn't work. When the macro is running, you can see it select <Name>, but it doesn't make any changes before it closes it and opens a new instance (It doesn't move on to <insights> or <alternate journal>)

Anyone have any ideas??

Thank you so much. I've learned a lot from these boards.
 
Old May 11th, 2009, 08:25 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

Try changing the Wrap to wdFindContinue

With .Selection.Find
.Text = "<Name>"
.Replacement.Text = authorString
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With


Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips &amp; Tricks (http://www.vbadud.blogspot.com)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Code works in Excel VBA but not Access VBA fossx Access VBA 2 May 21st, 2007 08:00 AM
Converting excel data to Access using excel VBA ShaileshShinde VB Databases Basics 1 April 26th, 2006 07:57 AM
Excel VBA to SQL & back to VBA edesousa Excel VBA 1 June 1st, 2004 02:39 AM
Word VBA sdowen Excel VBA 4 December 3rd, 2003 04:32 PM
VBA Word gcookie79 Excel VBA 1 November 12th, 2003 01:04 PM





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