Word/Excel help required

sharrison

New Member
Joined
Jan 27, 2006
Messages
17
I have a word document which is to be used as a quote document. What I want to be able to do is allow the salesperson to be able to choose, in Excel, which sections of the document they want to include. Depending on their selections I would like, via Excel, to be able to delete the pages which the salesperson doesn't want. Has anybody got any suggestions on how to do it. I tried recording a macro in Word to try and see how to do it but it appears that this macro doesn't translate into Excel (or at least I can't get it too). The recorded macro is as follows:

Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="3"
Selection.GoTo What:=wdGoToBookmark, Name:="\page"
Selection.Find.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Delete Unit:=wdCharacter, Count:=1
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
You can't just drop VBA from Word into Excel. You need to add a reference to the Microsoft Word Object Library (Excel > VBA editor > Tools > References), then in your VBA code you open an instance of MS Word, then open the Word document you want to manipulate, and finally issue those commands to Word so that Word can execute them.

If you Google vba excel word you will find many examples of this sort of thing. Unfortunately I've only done this once or twice - controlling Word from Excel - but I use the same method for controlling Access from Excel and it works fine.

Here's an example of how to create and save a Word document from Excel - which is about as much as I know about the subject:-
Code:
[FONT=Fixedsys]Option Explicit[/FONT]
 
[FONT=Fixedsys]Public Sub MakeWordDoc()[/FONT]
 
[FONT=Fixedsys] Dim wordApp As Word.Application[/FONT]
[FONT=Fixedsys] Set wordApp = CreateObject("Word.Application")[/FONT]
[FONT=Fixedsys] Dim wordDoc As Word.Document[/FONT]
[FONT=Fixedsys] Dim wordRange As Word.Range[/FONT]
 
[FONT=Fixedsys] With wordApp[/FONT]
 
[FONT=Fixedsys]   .WindowState = Word.WdWindowState.wdWindowStateMaximize[/FONT]
[FONT=Fixedsys]   .Documents.Add ("normal.dotm")[/FONT]
[FONT=Fixedsys]   Set wordDoc = .ActiveDocument[/FONT]
[FONT=Fixedsys]   Set wordRange = wordDoc.Range[/FONT]
 
[FONT=Fixedsys]   With wordRange[/FONT]
 
[FONT=Fixedsys]     .InlineShapes.AddPicture "C:\Temp\TEMP.PNG"[/FONT]
[FONT=Fixedsys]     .Move wdStory, 1[/FONT]
[FONT=Fixedsys]     .InlineShapes.AddHorizontalLineStandard[/FONT]
[FONT=Fixedsys]     .Move wdStory, 1[/FONT]
[FONT=Fixedsys]     .InsertAfter vbCrLf[/FONT]
 
[FONT=Fixedsys]     .Font.Bold = False[/FONT]
[FONT=Fixedsys]     .Font.Size = 12[/FONT]
[FONT=Fixedsys]     .ParagraphFormat.Alignment = wdAlignParagraphLeft[/FONT]
 
[FONT=Fixedsys]     .InsertAfter "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod "[/FONT]
[FONT=Fixedsys]     .InsertAfter "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, "[/FONT]
[FONT=Fixedsys]     .InsertAfter "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo "[/FONT]
[FONT=Fixedsys]     .InsertAfter "consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse "[/FONT]
[FONT=Fixedsys]     .InsertAfter "cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat "[/FONT]
[FONT=Fixedsys]     .InsertAfter "non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."[/FONT]
[FONT=Fixedsys]     .InsertAfter vbCrLf[/FONT]
 
[FONT=Fixedsys]     End With[/FONT]
 
[FONT=Fixedsys]   .ActiveDocument.SaveAs "C:\Temp\TEMP.DOC"[/FONT]
[FONT=Fixedsys]   .ActiveDocument.Close[/FONT]
[FONT=Fixedsys]   .Application.Quit[/FONT]
 
[FONT=Fixedsys]   Set wordDoc = Nothing[/FONT]
[FONT=Fixedsys]   Set wordApp = Nothing[/FONT]
 
[FONT=Fixedsys] End With[/FONT]
 
[FONT=Fixedsys]End Sub[/FONT]
 
Upvote 0
Thanks Ruddles. I can do that bit and I can customised the quote document from job to job the only bit I am struggling with is the deleting of pages.
 
Upvote 0
You're way ahead of me in that case!

Sorry I couldn't help.
 
Upvote 0

Forum statistics

Threads
1,224,608
Messages
6,179,872
Members
452,949
Latest member
Dupuhini

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top