Saveas active word document in specific folder

Chandresh

Board Regular
Joined
Jul 21, 2009
Messages
123
I am using the below code to copy values from excel and paste into word document and the macro is working fine .

I am facing the issue while doing saveas word documents could you please help me with the code which can saveas the documents in specific folder and close the original document without saving .

Thanks in advance for all the help.

code :



Sub test()
Dim objWord As Object
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
sDocName = "ABC"
Set objWord =CreateObject("Word.Application")
objWord.Visible =True
objWord.Documents.Open"C:\Users\chandresh.choudhary\Desktop\Word\Format.docx"
WithobjWord.ActiveDocument
.Bookmarks("Text1").Range.Text =ws.Range("B1").Value
.Bookmarks("Text2").Range.Text = ws.Range("B2").Value
.Bookmarks("Text3").Range.Text =ws.Range("B3").Value
End With
Set objWord =Nothing
objWord.SaveAsFilename:="C:\Users\chandresh.choudhary\Desktop\Word\WorkbookName1.Docx"
End Sub

 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,977
Office Version
  1. 365
Platform
  1. Windows
Untested - try

Code:
Sub test()
    Dim objWord As Object, ws As Worksheet, [COLOR=#ff0000]sDocName As String[/COLOR]
    Set ws = ThisWorkbook.Sheets("Sheet1")
    sDocName = "ABC"
    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True
    objWord.Documents.Open "C:\Users\chandresh.choudhary\Desktop\Word\Format.docx"

    With objWord.ActiveDocument
        .Bookmarks("Text1").Range.Text = ws.Range("B1").Value
        .Bookmarks("Text2").Range.Text = ws.Range("B2").Value
        .Bookmarks("Text3").Range.Text = ws.Range("B3").Value
[COLOR=#ff0000]        .SaveAs ("C:\Users\chandresh.choudhary\Desktop\Word\WorkbookName1.Docx")
        .Close False   [/COLOR]
    End With
[COLOR=#ff0000]    objWord.Quit[/COLOR]
    Set objWord = Nothing
End Sub


and to make the name more dynamic
Code:
add a constant 
  Const Fpath = "C:\Users\chandresh.choudhary\Desktop\Word\"
  sDocName = "ABC"
  
  objWord.Documents.Open fpath & "Format.docx"

  .SaveAs (fPath & sDocName & ".docx")
 
Last edited:

Watch MrExcel Video

Forum statistics

Threads
1,130,042
Messages
5,639,730
Members
417,108
Latest member
Thein Than

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
Top