I'm using a macro to create a CSV file from a source file then creating a TXT file and opening with Word.
I got this much to work. See Macro flow below. But I have to browse to last file name, edit it to new name and save it all manually, mainly because the filename of the text file is hard-coded to Delete.txt right now.
And now because of another process I'm adding this to, I need to be able to save the TXT file (with the same name as the initial macro uses) automatically as a Word file and silently close it.
Once the csv is created using the initial macro here how we save the text file and open it with Word.
It appears it is possible!
This is close.
http://excel-macro.tutorialhorizon.c...word-document/
This one has all kinds of issues it appears:
http://stackoverflow.com/questions/3...-doc-and-excel
Also posted here yesterday: Automatically saving a text file (created with a macro from csv) as Word.doc silently
I got this much to work. See Macro flow below. But I have to browse to last file name, edit it to new name and save it all manually, mainly because the filename of the text file is hard-coded to Delete.txt right now.
And now because of another process I'm adding this to, I need to be able to save the TXT file (with the same name as the initial macro uses) automatically as a Word file and silently close it.
Once the csv is created using the initial macro here how we save the text file and open it with Word.
Code:
Sub FromCSVFileOnlyMakeWordFile()
'Select last row in worksheet.
Selection.End(xlDown).Select
Do Until ActiveCell.Row = 1
'Insert blank row.
ActiveCell.Resize(5).EntireRow.Insert Shift:=x1Down
'Move up one row.
ActiveCell.Offset(-1, 0).Select
Loop
Call SaveAsTextFileFixedSilentClose
Application.DisplayAlerts = False
ActiveWorkbook.Close
Application.DisplayAlerts = True
Call OpenFixedTextFileInMSWord
End Sub
Code:
Sub SaveAsTextFileFixedSilentClose()
MyPath = "C:\ACTIVE\"
newfname = "Delete"
Set MySheets = ActiveWindow.SelectedSheets
Application.DisplayAlerts = False
For Each ws In MySheets
ws.Copy
Suffix = VBA.Right(ws.name, 3)
ActiveWorkbook.SaveAs Filename:=MyPath & newfname & ".txt", _
FileFormat:=xlText, CreateBackup:=False
ActiveWorkbook.Close
Next ws
Application.DisplayAlerts = True
End Sub
Code:
Sub OpenFixedTextFileInMSWord()
Dim s As Variant
Dim i As Integer
s = Shell("WINWORD.EXE ""C:\ACTIVE\Delete.txt""", vbNormalNoFocus)
End Sub
It appears it is possible!
This is close.
http://excel-macro.tutorialhorizon.c...word-document/
Code:
Function FnWriteToWordDoc()
Dim objWord
Dim objDoc
Dim objSelection
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add
objWord.Visible = True
Set objSelection = objWord.Selection
objSelection.TypeText ("Saving this file after this text")
objDoc.SaveAs ("D:\MyFirstSave")
End Function
This one has all kinds of issues it appears:
http://stackoverflow.com/questions/3...-doc-and-excel
Code:
Sub OpenManual()
Dim objWord, doc
'We need to continue through errors since if Word isn't
'open the GetObject line will give an error
On Error Resume Next
Set objWord = GetObject(, "Word.Application")
'We've tried to get Word but if it's nothing then it isn't open
If objWord Is Nothing Then
'No need to do anything
Else
'It's good practice to reset error warnings
On Error GoTo 0
'Open your document and ensure its visible and activate after openning
objWord.Visible = True
objWord.Activate
For Each doc in objWord.Documents
doc.SaveAs("C:\Temp\SavedDocuments\" & doc.Name)
doc.Close
Next
Set doc = Nothing
objWord.quit
End If
Set objWord = Nothing
End Sub
Also posted here yesterday: Automatically saving a text file (created with a macro from csv) as Word.doc silently