vba to word help please.

mirinjawbro

New Member
Joined
Sep 28, 2016
Messages
30
Hello

Currently have a macro working that opens word. Pastes a range from excel and makes the margins narrow. It just will not work to have after spacing set to 0.

Can anyone help please?

Let's assume we copy a1 to f10. Open word. Paste. Narrow margins and after and before spacing set to 0

Thanks
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
I can't imaging how you expect anyone to provide specific advice when you've not posted the problem code. As for:
Currently have a macro working that opens word. Pastes a range from excel and makes the margins narrow. It just will not work to have after spacing set to 0.
Precisely what doesn't work?
 
Upvote 0
Sorry I should be more clear

I have data from excel that I want to paste into a new word document. This needs to have 0 spacing and narrow margins

Thats all I have trouble with. I'm just looking for some code to do that

So copy a1 to b6 from excel
Open new word
Paste into word with narrow margins and 0 spacing

Thanks
 
Upvote 0
What you're describing is pretty basic, but vague. I have no idea what you consider 'narrow margins'.

Try something based on:
Code:
Sub SendRangeToDoc()
'Note: This code requires a reference to the Word Object Library,
' set via Tools|References in the VBA Editor.
Dim wdApp As New Word.Application, WdDoc As Word.Document
ActiveWorkbook.Sheets(1).Range("A1:B6").Copy
wdApp.Visible = True
Set WdDoc = wdApp.documents.Add
With WdDoc.Range
  .Paste
  With .ParagraphFormat
    .SpaceBefore = 0
    .SpaceAfter = 0
  End With
  With .PageSetup
    .TopMargin = wdApp.InchesToPoints(0.25)
    .BottomMargin = wdApp.InchesToPoints(0.25)
    .LeftMargin = wdApp.InchesToPoints(0.25)
    .RightMargin = wdApp.InchesToPoints(0.25)
  End With
End With
Set WdDoc = Nothing: Set wdApp = Nothing
End Sub
Most of the above code (almost everything after .Paste) wouldn't be needed if you actually used a Word template with the required layout & formatting specifications...
 
Upvote 0
I did try making my own word template. But when pasting it changed the spacing back to 10. Very annoying

I will try the above code and let you know thIanks again

Margins are for print options
 
Upvote 0
Thanks that worked perfectly.

Next step I have problem with is saving the word document using cell a1 As the name. I need it to open a directory with the save as dialog boxo

Lastly. Any books or courses anyone can recommend? I really wanna learn this myself.

Thanks
 
Upvote 0
It would be helpful if you said up-front what the full scope is of what you want to do. Your piecemeal approach just creates more work. Try:
Code:
Sub SendRangeToDoc()
'Note: This code requires a reference to the Word Object Library,
' set via Tools|References in the VBA Editor.
Dim wdApp As New Word.Application, WdDoc As Word.Document
ActiveWorkbook.Sheets(1).Range("A1:B6").Copy
wdApp.Visible = True
Set WdDoc = wdApp.documents.Add
With WdDoc
  With .Range
    .Paste
    With .ParagraphFormat
      .SpaceBefore = 0
      .SpaceAfter = 0
    End With
    With .PageSetup
      .TopMargin = wdApp.InchesToPoints(0.25)
      .BottomMargin = wdApp.InchesToPoints(0.25)
      .LeftMargin = wdApp.InchesToPoints(0.25)
      .RightMargin = wdApp.InchesToPoints(0.25)
    End With
  End With
  .SaveAs2 Filename:="C:\Users" & Environ("UserName") & _
    "\Documents\MyFolder\" & _
    Split(.Tables(1).Cell(1, 1).Range.Text)(0) & ".docx", _
    FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
  .Close
End With
'wdApp.Quit
Set WdDoc = Nothing: Set wdApp = Nothing
End Sub
As coded, the macro saves the file to a folder named 'MyFolder' in the user's Documents folder. Change the path to suit your actual requirements. As you will see when you run the code, there's no need to open the SaveAs dialogue box if you know the filename & path to use.

Once you have it working correctly, you can delete/comment-out the line 'wdApp.Visible = True' and un-comment the line 'wdApp.Quit'.
 
Upvote 0

Forum statistics

Threads
1,223,796
Messages
6,174,655
Members
452,575
Latest member
Fstick546

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