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:-
So far, so good....
I've now been asked for another button to export the same datasheet to *.doc format. I started with this :-
Problem is, the margins on the word document are too large and the table stretches over two pages.
Attempt 2:-
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.
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.