VBA Code to Export a Worksheet as a Word Document

UncleBajubjubs

Board Regular
Joined
Jul 11, 2017
Messages
111
Office Version
  1. 2010
Hello, I have a workbook with one worksheet which is intended to gather data from the other sheets, and then be exported as a word document. I don't have much experience with this, so i copied some code I found online:
Code:
Sub export_excel_to_word()
    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:=Application.ActiveWorkbook.Path & "" & ActiveSheet.Name
End Sub

But this gives me "Unexpected End Sub" on the first line. Any ideas, or just alternate code which will do what I need? Thanks!
 
Last edited by a moderator:

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
have not tested your code, but in preLast row I think should be written that:
Rich (BB code):
 ... Application.ActiveWorkbook.Path &
Rich (BB code):
"\" & ActiveSheet.Name 
 
Last edited:
Upvote 0
Thanks,
Strangely though it is there in my code, I think maybe the text formatting of the forum made it not show on here.
 
Upvote 0
Strangely though it is there in my code, I think maybe the text formatting of the forum made it not show on here.
Yes, I have seen that happen.

There is nothing wrong with the structure of that code, as far as I can see (nothing that would return that particular error message).
Is there any other VBA code in the module?
That is the kind of message I would expect to see if you have too many "End Sub" statements.
 
Upvote 0
Yes, I have seen that happen.

There is nothing wrong with the structure of that code, as far as I can see (nothing that would return that particular error message).
Is there any other VBA code in the module?
That is the kind of message I would expect to see if you have too many "End Sub" statements.

Yup, I had been moving things around, and accidentally had a Sub started without an End Sub before this newest sub.

Now it exports, but there are a few issues. The page it copies scales larger than intended, so what should be 2 pages in word is now 5. Any ideas on how to fix? Thanks!
 
Upvote 0
No, I don't do anything with Word VBA. "Word" is a four letter word in my book, and I avoid it whenever possible.

I will move this to the "General Excel Discussion & Other Questions" forum. We have a resident Word expert who answers a lot of Word question in that forum (so that is typically the best place to post questions regarding Word).
 
Upvote 0
No, I don't do anything with Word VBA. "Word" is a four letter word in my book, and I avoid it whenever possible.

I will move this to the "General Excel Discussion & Other Questions" forum. We have a resident Word expert who answers a lot of Word question in that forum (so that is typically the best place to post questions regarding Word).

Alrighty, thank you!
 
Upvote 0
I've done more research and think if I remove the margins, it will be fixed, but am having difficulty in doing so. I found some code:
Code:
With WordApp.ActiveDocument.PageSetup
  .Orientation = wdOrientLandscape
  .TopMargin = WordApp.InchesToPoints(0.6)
  .BottomMargin = WordApp.InchesToPoints(0.6)
  .LeftMargin = WordApp.InchesToPoints(0.6)
  .RightMargin = WordApp.InchesToPoints(0.6)
End With

But am not able to get this to work, when added as

Code:
Sub Export_Excel_To_Word()
    Set obj = CreateObject("Word.Application")
    obj.Visible = True
    Set newObj = obj.Documents.Add
    ActiveSheet.UsedRange.Copy
    newObj.Range.Paste
    Application.CutCopyMode = False
    obj.Activate
    
With WordApp.ActiveDocument.PageSetup
.Orientation = wdOrientLandscape
.TopMargin = WordApp.InchesToPoints(0.6)
.BottomMargin = WordApp.InchesToPoints(0.6)
.LeftMargin = WordApp.InchesToPoints(0.6)
.RightMargin = WordApp.InchesToPoints(0.6)
End With


    newObj.SaveAs filename:=Application.ActiveWorkbook.Path & "" & ActiveSheet.Name
End Sub

Any thoughts?

Edit: Am I not doing the code tags right?
 
Last edited by a moderator:
Upvote 0
Try:
Code:
Sub Export_Excel_To_Word()
Dim wdApp As Object, wdDoc As Object, bStart As Boolean
bStart = False
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
On Error GoTo 0
If wdApp Is Nothing Then
  Set wdApp = CreateObject(Class:="Word.Application")
  bStart = True
End If

ActiveSheet.UsedRange.Copy

With wdApp
  .Visible = True
  'Create new Document
  Set wdDoc = .Documents.Add
  With wdDoc
    With .PageSetup
      .Orientation = 1 'wdOrientLandscape
      .TopMargin = wdApp.InchesToPoints(0.6)
      .BottomMargin = wdApp.InchesToPoints(0.6)
      .LeftMargin = wdApp.InchesToPoints(0.6)
      .RightMargin = wdApp.InchesToPoints(0.6)
    End With
    'Paste Data
    .Range.Paste
    .SaveAs2 Application.ActiveWorkbook.Path & "/" & ActiveSheet.Name & ".docx", 12, , , False ' 12 = wdFormatXMLDocument
    '.Close False
  End With
  'If bStart = True Then .Quit
End With
Application.CutCopyMode = False
'Clean up the Object when Finished
Set wdDoc = Nothing: Set wdApp = Nothing
End Sub
Note: I've added two lines of commented-out code for saving the document and, if your code starts the Word session, exiting Word. You can enable that code if you want.

As for the code tags, you create them by clicking on the # symbol on the reply menu. You then insert your code between them.
 
Last edited:
Upvote 0
Thank you, and for some reason it was showing me the code tags instead of just performing the formatting. I put in your solution, which works other than I commented out the line
Code:
  .SaveAs2 Application.ActiveWorkbook.Path & "/" & ActiveSheet.Name & ".docx", 12, , False

As it was giving me type mismatch, but otherwise it all works. I was also trying to set the line and paragraph spacing, it seems to automatically put large spacing in. I found a helpful example, which was

Code:
With Selection.ParagraphFormat
  .LineSpacingRule = wdLineSpaceAtLeast 
 .LineSpacing = 24
EndWith


on another site, but am not able to figure out how best to implement it in my code. I want to be able to set the paragraphs to not have big gaps between them.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,791
Messages
6,174,603
Members
452,574
Latest member
hang_and_bang

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