Exporting worksheet to Word

Skyfoil

New Member
Joined
Mar 4, 2019
Messages
1
Hello,

I have a HUGE spreadsheet full of information on a product range that essentially lists all of the properties of the products by part number.
A front page allows the user to select from a number of product options and the page returns the part number and all of the technical data associated with it.
A second, hidden worksheet contains the template for a datasheet that is automatically generated from the selected options at the push of a button to a *.pdf document using the following code:-

VBA Code:
Public Sub DatasheetPDF()

Worksheets("Technical Datasheet").Visible = True
Sheets("Technical Datasheet").Select

ActiveSheet.ExportAsFixedFormat OpenAfterPublish:=True, Type:=xlTypePDF

Sheets("ATS Configurator").Select
Worksheets("Technical Datasheet").Visible = False
End Sub

So far, so good....

I've now been asked for another button to export the same datasheet to *.doc format. I started with this :-

VBA Code:
Public Sub DatasheetDOC()

Worksheets("Technical Datasheet").Visible = True
Sheets("Technical Datasheet").Select

Set obj = CreateObject("Word.Application")
    obj.Visible = True
    Set newObj = obj.Documents.Add
    ActiveSheet.UsedRange.Copy
    newObj.Range.Paste
    Application.CutCopyMode = False
    obj.Activate
    newObj.SaveAs Filename:=Environ$("USERPROFILE") & "\Documents\" & "\" & ActiveSheet.Name

Sheets("ATS Configurator").Select
Worksheets("Technical Datasheet").Visible = False
Worksheets("Legend").Visible = False
Worksheets("Database").Visible = False
Worksheets("Images").Visible = False
End Sub

Problem is, the margins on the word document are too large and the table stretches over two pages.

Attempt 2:-

VBA Code:
Public Sub DatasheetDOC2()

Worksheets("Technical Datasheet").Visible = True
Sheets("Technical Datasheet").Select

Dim ws As Worksheet
Set ws = ActiveSheet
Dim objWd As Object
Set objWd = CreateObject("word.application")
objWd.Visible = True
Dim objDoc As Object
Set objDoc = objWd.Documents.Add
objDoc.PageSetup.Orientation = 0
objDoc.PageSetup.TopMargin = CentimetersToPoints(1)
objDoc.PageSetup.BottomMargin = CentimetersToPoints(1)
objDoc.PageSetup.LeftMargin = CentimetersToPoints(1)
objDoc.PageSetup.RightMargin = CentimetersToPoints(1)
Application.ScreenUpdating = False
ws.UsedRange.Copy
objDoc.Content.Paste
Application.CutCopyMode = False
Application.DisplayAlerts = False
objDoc.SaveAs Filename:=Environ$("USERPROFILE") & "\Documents\" & "\" & ActiveSheet.Name
Application.DisplayAlerts = True
Application.ScreenUpdating = True

Sheets("ATS Configurator").Select
Worksheets("Technical Datasheet").Visible = False
Worksheets("Legend").Visible = False
Worksheets("Database").Visible = False
Worksheets("Images").Visible = False
End Sub

This has fixed the margins, but the table is no wider than it was before and still stretches over two pages.
The "Technical Datasheet" page is formatted to A4 portrait. All excess cells are hidden.

It's worth mentioning that i'm very new to VBA so I have been strongly relying on previously published papers online for this so far, but i'm struggling with the final hurdle on this one.

Any help would be greatly appreciated.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Forum statistics

Threads
1,215,590
Messages
6,125,707
Members
449,251
Latest member
Herushi

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