Help! Exporting Excel data to Word

crvazquez954

New Member
Joined
Jul 9, 2019
Messages
23
I have the following code but can't figure out why when I export the information to word and generate a "proposal" it opens the actual template. When the user saves the report it's saving it over the original document. Is there any way we can fix it so that word uses the template to generate a new document while leaving the original template intact?

Sub Export_To_Word() 'FINAl TEST OF CELL DATA AND TABLE TRANSFER
'Name of the existing Word doc.
Const stWordReport As String = "Proposal.docm"


'Word objects.
Dim pappWord As Object
Dim docWord As Object
Dim wdbmRange As Word.Range


'Excel objects.
Dim wb As Excel.Workbook
Dim wsSheet As Worksheet
Dim rnReport As Range
Dim r As Range
Dim xlName As Excel.Name
Dim TodayDate As String


'Initialize the Excel objects.
Set wb = ActiveWorkbook
Set wsSheet = wb.Worksheets("Summary")
Set rnReport = wsSheet.Range("BidTable")


'Turn off screen updating.
Application.ScreenUpdating = False


'Remove formatting
Range("B28:D47").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
.PatternTintAndShade = 0
End With


For Each r In rnReport.Rows
If WorksheetFunction.Count(r) = 0 Then r.Hidden = True
Next


'Initialize the Word objects.
'Set pappWord = New Word.Application
Set pappWord = CreateObject("Word.Application")
Set docWord = pappWord.Documents.Open(wb.Path & "" & stWordReport)
Set wdbmRange = docWord.Bookmarks("BidTable").Range
pappWord.Visible = True


'If the macro has been run before, clean up any artifacts before trying to paste the table in again.
On Error Resume Next
With docWord.InlineShapes(1)
.Select
.Delete
End With


'Loop through names in the activeworkbook
For Each xlName In wb.Names
'if xlName's name is existing in document then put the value in place of the bookmark
If docWord.Bookmarks.Exists(xlName.Name) Then
docWord.Bookmarks(xlName.Name).Range.Text = Range(xlName.Value)
End If
Next xlName


On Error GoTo 0


rnReport.Copy 'Copy the report to the clipboard.


'Select the range defined by the "Report" bookmark and paste in the report from clipboard.
With wdbmRange
.Select
.PasteSpecial Link:=False, DataType:=wdPasteMetafilePicture, _
Placement:=wdInLine, DisplayAsIcon:=False
End With


'Activate word and display document
With pappWord
.Visible = True
.ActiveWindow.WindowState = 0
.Activate
End With

'Unhide hidden rows
Rows("26:48").Select
Selection.EntireRow.Hidden = False


'Restore formatting
Range("B28:D47").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent1
.TintAndShade = 0.599993896298105
.PatternTintAndShade = 0
End With


Application.ScreenUpdating = True
End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Looks like all I had to do was change the following line:
Set docWord = pappWord.Documents.Open (wb.Path & "" & stWordReport)

to:

Set docWord = pappWord.Documents.Add(wb.Path & "" & stWordReport)
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,264
Members
452,627
Latest member
KitkatToby

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