Saveas active word document in specific folder

Chandresh

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

 

Some videos you may like

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,039
Office Version
365
Platform
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,099,364
Messages
5,468,189
Members
406,570
Latest member
Ktvu2006

This Week's Hot Topics

Top