Save a word document to a specific folder - vba

I am trying to create a macro to save a word document to a specific file. Here is the vba macro I have so far...
Sub Test1()
Dim path As String
path = "C:\Users\aroldan\OneDrive\Documents\02 - PENDING\"
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs FileName:=path
Application.DisplayAlerts = True
ActiveWorkbook.Close
End Sub
I am running this on a Word, while many tutorials show this on Excel.
Thank you!

You just need to make the changes already suggested. Adding the file name to the path is enough:
Sub SaveAFile()
Dim path As String
path = "C:\Users\User\Test.docx"
With ActiveDocument
.SaveAs FileName:=path
.Close
End With
End Sub
When searching for VBA specific to Word, search on "Word" VBA -Excel, plus the subject keywords.

Related

MS Word VBA to select any word file to open from folder path for copy/paste

I created 2 macros in MS Word VBA, the 1st one to select any docx file from a specified folder below as follows:
Macro 1
Sub test()
Dim intChoice As Integer
Dim strPath As String
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
intChoice = Application.FileDialog(msoFileDialogOpen).Show
'if the user selects a file
If intChoice <> 0 Then
'get the path selected
strPath = Application.FileDialog( _
msoFileDialogOpen).SelectedItems(1)
'opens the document
objWord.Documents.Open (strPath)
End If
End Sub
Then the 2nd word VBA macro that I'm working on is where I want to open the master document which is document A and then call the above macro to open document b that I selected from a directory path so that I can copy contents from document B into document a which is at the end of this post.
However, the code is not working and been stuck on this for the past 8 hours and no luck finding the right combination anywhere online. The 1st macro works fine as i'm able to select any docx file and it opens successfully.
The second macro which is supposed to open the document a and then run the 1st macro which is call test and that works. but where the code is not working is after I run the call test macro, there is no copying & pasting happening such as I was under the impression that selection.whole & selection.copy would work once I run the call test macro that opens up the document b file.
so in the end, I want to open up document b from call test macro, select the data from document b to copy onto document a that's open as well.
Any help would be greatly appreciate it and not that familiar with word vba and 1st time ever doing it. Thanks in advance.
Sub test6()
Application.ScreenUpdating = False
Dim strFile As String
strFile = "C:\Users\test\Desktop\tar sheet test\documenta.docx"
If Dir(strFile) <> "" Then
Documents.Open strFile
End If
Call test
Selection.WholeStory
Selection.Copy
Documents("documenta.docx").Activate '
Selection.EndKey wdStory '
Selection.PasteAndFormat wdPasteDefault
End Sub
Since you are working within Word, you do not need to create a new Word.Application as you can use the existing instance.
I suggest you convert sub test to a function so that you can return the Document object that you opened in test back to your calling sub.
I also recommend to work with the Range object rather than relying on Selection as it is unnecessary most of the time.
Please try the code below:
Sub test6()
Dim strFile As String
strFile = "C:\Users\test\Desktop\tar sheet test\documenta.docx"
Dim destDocument As Document
If Dir(strFile) <> "" Then
Set destDocument = Application.Documents.Open(strFile)
Dim srcDocument As Document
Set srcDocument = test
If Not srcDocument Is Nothing Then
srcDocument.Content.Copy
destDocument.Range(destDocument.Range.End - 1).PasteAndFormat wdPasteDefault
End If
End If
End Sub
Function test() As Document
Dim intChoice As Integer
Dim strPath As String
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
intChoice = Application.FileDialog(msoFileDialogOpen).Show
'if the user selects a file
If intChoice <> 0 Then
'get the path selected
strPath = Application.FileDialog( _
msoFileDialogOpen).SelectedItems(1)
'opens the document
Set test = Application.Documents.Open(strPath)
End If
End Function

Copy all text from word document to antoher word document without formating

I would like to copy all the text from the active Word document (docx) into another new Word document (docx), the thing is that in the original word document I got a lot of formatting and I just need the raw text !
I've try to do something but I'm kinda loss... :
Sub extract_text()
Dim originalDoc As Document, targetDocAs Document,
Dim myPath As String, myPath1 As String, myPath2 As String
Dim rng As Range
myPath = ThisDocument.Path
myPath1 = myPath & "\1.docx"
Set originalDoc = Documents.Open(myPath)
Set targetDoc= Documents.Open(myPath1)
originalDoc.Content.Copy
Set rng = targetDoc.Content
rng.Collapse Direction:=wdCollapseEnd
rng.Paste
targetDoc.SaveAs myPath2
originalDoc.Close SaveChanges:=False
targetDoc.Close SaveChanges:=False
End Sub
Could you help me ?
You really need to learn to use the Object Browser and the macro recorder. You would have got the answer to this in less time than it took to post your question.
Use PasteAndFormat.
rng.PasteAndFormat (wdFormatPlainText)

macro to "save as" and don't open the saved document

I have the following macro that "Save As" a copy of the excel doc.
Sub STEP3SaveAs()
Application.GetSaveAsFilename
End Sub
BUT, every time I "Save As", I land on the doc I just saved...
I need assistance with a Macro that will "Save As", and leave me on the doc that I was working on.
Any assistance will be highly appreciated.
The issue here is that GetSaveAsFilename does not save at all. This just shows a dialog to choose a filename, but does not save it.
You can use the Workbook.SaveCopyAs Method to save a copy of your actual workbook.
Public Sub SaveCopy()
Dim Filename As String
Filename = Application.GetSaveAsFilename 'choose filename
ThisWorkbook.SaveCopyAs(Filename)
End Sub
You can do .SaveAs twice, thus it would return you the original workbook. The ThisWorkbook.FullName is a way to obtain the path:
Sub TestMe()
Dim saveName As String
Dim oldName As String
With ThisWorkbook
oldName = .FullName
saveName = Application.GetSaveAsFilename
.SaveAs saveName
.SaveAs oldName
End With
End Sub

Workbook should be automatically saved as a .xlsx file in user defined folder and close macro book without saving

I recorded vba code to do some conditional formatting. The result is stored in the workbook itself. Now I want to force the user not to save the workbook, instead after the code is run, it should automatically save the workbook using "Save As" into a non macro file using some unique identifier such as "yyyymmmdd, hhmm.xlsx" and it should also ask the user where to save.
Additionally, it should close the workbook without saving it and open the last saved as .xlsx file. I found some codes, but they are not exactly what I am looking for. Please help.
How about this
Option Explicit
Sub SaveAs()
Dim sDate As String
Dim FileName As String
'// format Date
sDate = Format(Now, "YYYYMMDD HHMM")
'// Save As Name
FileName = sDate
'// Save path
Application.Dialogs(xlDialogSaveAs).Show FileName
End Sub
add this code below your code
Per OP Comment
This should do it - Tested on Excel 2010
Option Explicit
Sub SaveAs()
Dim xlSaveAs As String
Dim xlPath As Variant
Application.ScreenUpdating = False
'// Save As Name
xlSaveAs = "Weekly Report - " & Format(Now, "YYYYMMDD HHMM") & ".xlsx"
'// Save path
Application.DisplayAlerts = False
xlPath = Application.GetSaveAsFilename( _
InitialFileName:=xlSaveAs, _
FileFilter:="Excel Files (*.xlsx), *.xlsx", _
Title:="My Save Dialog")
If xlPath <> False Then
ThisWorkbook.SaveAs xlPath, xlOpenXMLWorkbook
Else
MsgBox "Not Valid Path" '// Cancel
End If
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Finally, You may find the Getting Started with VBA in Office 2010 article in MSDN helpful.
edit : I rewrite the code to do what you want
Public Sub SaveNewFile()
' Create a new file basing the name of the current file (without extension if it's an xlsm) and the creation time
Dim filename As String
filename = ThisWorkbook.Path & "\" & CreateObject("scripting.filesystemobject").getbasename(ThisWorkbook.Name) & Format(Now, "yyyyMMdd hhmm") & ".xlsx"
' Save the file under the new name in xlsx format
' This action close the file and reopen it with the new name
Application.DisplayAlerts = False
ThisWorkbook.SaveAs filename:=filename, FileFormat:=xlOpenXMLWorkbook
Application.DisplayAlerts = True
End Sub

Auto save & close workbooks, except VBAWorkbooks

Currently trying to figure out how to have my code that auto saves and closes open workbooks to avoid vba project workbooks without naming the vba project workbooks. Is there a way to get your code to recognize vba workbooks vs the other open workbooks I'm trying to save and close?
Option Explicit
Public ThisFile As String
Public Path As String
Sub CloseAndSaveOpenWorkbooks()
Dim Wkb As Workbook
Path = [D1]
With Application
.ScreenUpdating = False
'Loop through the workbooks collection
For Each Wkb In Workbooks
With Wkb
'If NOT on Macro workbook then
If .Name <> ThisWorkbook.Name Then
'If the book is read-only
'don't save but close
If Not Wkb.ReadOnly Then
'Save current workbook with current workbooks cell A1 as file name
.SaveAs Filename:=(Path & "\" & Wkb.Sheets(1).Range("A1").Value & ".xls"), FileFormat:=xlExcel8
End If
'Closing here leaves the app running, but no books
.Close
End If
End With
Next Wkb
.ScreenUpdating = True
End With
End Sub
A follow up question to thread: VBA: Auto save&close out of all current workbooks except for Macro workbook
Got my answer!
Reading through Article Provided by J_V most definitely helped.
I replaced
If .Name <> ThisWorkbook.Name Then
with
If Wkb.HasVBProject = False Then
Hope this ends up helping others. Thanks again J_V!