Saveas active word document in specific folder

Chandresh

Board Regular
Joined
Jul 21, 2009
Messages
146
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

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
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:
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,198
Members
452,616
Latest member
intern444

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