I am trying to use a single work book with multiple sheets each sheet contains a different macro to generate a single word.doc template,

anasha81

New Member
Joined
May 16, 2023
Messages
1
Office Version
  1. 365
Platform
  1. Windows
1st macro:

Sub ReplaceText()

Dim WordApp As Object
Dim wdoc As Object
Set wAPP = CreateObject("Word.Application")
wAPP.DisplayAlerts = False
wAPP.Visible = True

Set wdoc = wAPP.Documents.Add(Template:="C:\Users\Desktop\Site Survey VBA TEST 001.docx", NewTemplate:=False, DocumentType:=0)

With wdoc
.Application.Selection.Find.Text = "<<Customer>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("C2")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Customer Site>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("C3")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Customer Site Code>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("C4")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Customer Site Address>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("C5")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Site Contact>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("C7")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Telephone Number>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("C8")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<Email Address>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("C9")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<WF12>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("D12")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<WF13>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("D13")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<WF14>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("D14")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<WF15>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("D15")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<WF16>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("D16")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<WF17>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("D17")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<WF18>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("D18")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<WF19>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("D19")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<WF20>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("D20")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<WF21>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("D21")
.Application.Selection.EndOf

.Application.Selection.Find.Text = "<<WF22>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("D22")
.Application.Selection.EndOf


.SaveAs2 Filename:=("C:\Users\ANash\Desktop\Site Survey CUSTOMER VERSION.dox"), _
FileFormat:=wdFormat.doc, AddtoRecentFiles:=False

End With


End Sub

(This macro works fine and opens up the doc template and populates the document.. the problem im having is when i use the third macro which is a button to run both macros, it opens up multiple replications of the template and does not populate the data from Macro one or Macro 2.

Macro 2:

Sub ReplaceText()

Dim WordApp As Object
Dim wdoc As Object
Set wAPP = CreateObject("Word.Application")
wAPP.DisplayAlerts = False
wAPP.Visible = True


With wdoc
.Application.Selection.Find.Text = "<<RO12>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("B12")
.Application.Selection.EndOf



End With


End Sub

( I really just need to know what commands I need to utilise to run the macros at the same time when Macro 3 button is pressed, to generate on the same word document which opens when Macro 1 is ran, and for macro 2 to run on the same word document and populate the information from sheet2 in excel any help any one can give would be greatly appreciated)

For info Macro 3:

Sub Button1_Click()
Call Module1.ReplaceText 'Macro1
Call Module2.ReplaceText 'Macro2
Call Button1_Click 'Macro3
End Sub


If I can manage to get this to work.. I will be adding additional macros to further sheets within the same workbook in the hopes that i can generate all information onto the single word.doc template

 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

Forum statistics

Threads
1,223,888
Messages
6,175,206
Members
452,618
Latest member
Tam84

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