I have a huge documents (.doc) in c:\sample folder. I want to save them in .docx, .pdf, .txt to c:\sample_converted folder after changing the font.
I have VBA script to change the font and it works fine..However got stuck in saving them in different format as well as without save dialog
I want to do this conversion entirely without user intervention. If not possible, at-least user can be requested for the source and target folder once.
strName = Left(oDoc.FullName, InStrRev(oDoc.FullName, ".") - 1) & ".txt"
oDoc.Range.Font.Name = strFont
oDoc.Save
oDoc.SaveAs2 FileName:=strName, FileFormat:=wdFormatUnicodeText
strName = Left(oDoc.FullName, InStrRev(oDoc.FullName, ".") - 1) & ".docx"
oDoc.Save
oDoc.SaveAs2 FileName:=strName, FileFormat:=wdFormatXMLDocument
oDoc.Close SaveChanges:=wdDoNotSaveChanges
WordBasic.DisableAutoMacros 0
Please advise how to do this.
Thanks.
SaveAs2 does not work with Word 2007. For 2007 and previous change the line
oDoc.SaveAs2 FileName:=strName, FileFormat:=wdFormatUnicodeText, Encoding:=65001
to
oDoc.SaveAs FileName:=strName, FileFormat:=wdFormatUnicodeText, Encoding:=65001
<=2007 is SaveAs and >2007 is SaveAs2
Now it works well.
Related
I have a longer code and in the final part there is an ActiveX button for saving and closing the file. Also, for efficiency, I've added an vbYesNo command for creating a new document based on the template in use.
The code:
Sub macrosave ()
Dim doc As Document
Dim strDosar As String
Dim Ret As Variant
Set doc = Application.ActiveDocument
strDosar = Range.Paragraphs(1).Range.Text
Ret = MsgBox("Do you want to create a new document?", vbYesNo)
If Ret = vbYes Then
Documents.Add Template:=ActiveDocument.AttachedTemplate.Path & Application.PathSeparator & ActiveDocument.AttachedTemplate.Name
End If
doc.SaveAs "\\server\Public\" & strDosar & ".doc", FileFormat:=wdFormatDocument = 0
doc.Close
End Sub
If I click yes, a new document is created, the last one is saved and closed afterwards.
If I click no, the active document is saved, but it is not closed afterwards.
I suspect it has something to do with the file format (wdFormatDocument) because this way it eliminates all VBA codes.
The file format is needed because I want to get rid of all content controls after saving the file.
Why doc.Close is not being executed in the second case and what are my options in order to achieve the purpose?
This argument is invalid:
FileFormat:=wdFormatDocument = 0
Perhaps:
Sub macrosave()
Dim doc As Document, strDosar As String, Ret As Variant
Set doc = ActiveDocument
strDosar = Split(doc.Range.Paragraphs(1).Range.Text, vbCr)(0)
Ret = MsgBox("Do you want to create a new document?", vbYesNo)
If Ret = vbYes Then Documents.Add Template:=doc.AttachedTemplate.FullName
doc.SaveAs "\\server\Public\" & strDosar & ".doc", FileFormat:=wdFormatDocument
doc.Close False
End Sub
I believe the problem comes from a disagreement between the file extension - doc and the specified file format wdFormatDocument.
Starting with Word 2007 the default file format (wdFormatDocument) refers to the Word Open XML file format, docx. To save as the doc file format requires using wdFormatDocument97. The reason it may have intermittently worked is because the numerical value of this enumeration is 0.
Try
doc.SaveAs "\\server\Public\" & strDosar & ".doc", FileFormat:=wdFormatDocument97
Or change the extension to docx
doc.SaveAs "\\server\Public\" & strDosar & ".docx", FileFormat:=wdFormatDocument
However docx will not remove the content controls the next time the document is opened. (The doc format will because content controls were introduced at the same time as the new file format, so they aren't supported in the old format.)
If the additional purpose is " it eliminates all VBA codes" then it would make sense to change the attached template before closing the document to Normal.dotm. This removes the link to the attached template and has the added advantage that the document will "find" its template no matter who opens it, later. (When the attached template is not present it can delay opening by a considerable time under some circumstances.)
doc.AttachedTemplate = NormalTemplate
Note that it will also make things simpler to pick up the file path using the FullName poperty of the template:
Documents.Add Template:=ActiveDocument.AttachedTemplate.FullName
I'm trying to add a few custom document properties to a folder of word documents.
I know that the loop itself works fine, because I used the same loop with different code to modify and then update pre-existing custom document properties.
The code to add custom document properties also works, I tested it by running it in it's own macro for a single document, which worked fine.
Since the loop works and the code within the loop also works, I just can't figure out what's wrong with it.
Here's the code:
Sub add_custom_docproperties()
Dim file
Dim path As String
Dim filepath As Variant
filepath = InputBox("Please enter the filepath for the files you want to
update.", "Input Filepath", "Copy filepath here...")
Select Case StrPtr(response)
Case 0
endednotification = MsgBox("The macro has been ended.", , "Notification")
Exit Sub
Case Else
End Select
path = filepath & "\"
file = Dir(path & "*.*")
'Application.ScreenUpdating = False
Do While file <> ""
Documents.Open FileName:=path & file
Check = MsgBox(path & file, , "Check")
ActiveDocument.CustomDocumentProperties.Add Name:="firstdocprop",
_LinkToContent:=False, Type:=msoPropertyTypeString, Value:="The First One"
ActiveDocument.CustomDocumentProperties.Add Name:="seconddocprop",
_LinkToContent:=False, Type:=msoPropertyTypeString, Value:="Second"
ActiveDocument.CustomDocumentProperties.Add Name:="thirddocprop",
_LinkToContent:=False, Type:=msoPropertyTypeString, Value:="Third"
'original example from:
'https://msdn.microsoft.com/en-us/vba/office-shared-vba
/articles/documentproperties-add-method-office
ActiveDocument.Save
ActiveDocument.Close
'set file to next in Dir
file = Dir()
Loop
'Application.ScreenUpdating = True
MsgBox "The macro is complete."
End Sub
As you can see I have a comment there with the first example I tried from msdn, which I modified.
Thanks in advance for any help, even if you could just point me to a resource explaining where I've gone wrong or something like that.
Word does not recognise the changes to the CustomDocumentProperties as being sufficiently important to actually save the document when you execute the Save command - unless you had made other changes it just decides to ignore the Save.
You can force a save by telling Word that the document has not been saved since it was last changed:
ActiveDocument.Saved = False
ActiveDocument.Save
ActiveDocument.Close
I have a question on how to edit the following macros to save MS word (.docx) file as PDF on macOS Sierra without .docx in the file name. That is, the macros works perfectly BUT it always saves files as, for example, letter.docx.pdf. It is a very easy option to save in pdf with one click only.
And I would appreciate if someone could help to me amend the macros below so that there is no .docx in the file name. I have tried to hide extensions in Finder, but that does not seem to help:
Sub SaveActiveDocAsPDF()
On Error Resume Next
ActiveDocument.SaveAs _
fileName:=ActiveDocument.FullName & ".pdf", _
FileFormat:=wdFormatPDF
End Sub
Thank you very much in advance.
You need to remove the existing file extension before adding the new one. Like this:
Sub SaveActiveDocAsPDF()
On Error Resume Next
Dim saveName As String
saveName = ActiveDocument.FullName
saveName = Left(saveName, Len(saveName) - 5) & ".pdf"
ActiveDocument.SaveAs fileName:=saveName, _
FileFormat:=wdFormatPDF
End Sub
I have the following code at the end of a project:
'Save the Document
Dim Directory As String, FileName As String
Directory = "C:\Users\" & (Environ$("Username")) & _
"\Desktop\STL\"
If Len(Dir(Directory, vbDirectory)) = 0 Then
MkDir Directory
End If
FileName = sDNUM & " " & Format(Date, "YYYY-MM-DD") & ".docx"
SaveAs Directory & FileName
MsgBox "File saved to:" & vbNL & Directory & FileName
and I am trying to get the file to save as a docx (non-macro enabled) after running a macro-enabled workbook.
The problem is by using the above method to save my file, upon attempting to open the newly saved file I receive the following error message:
The file < filename > cannot be opened because there are problems with the contents.
What method should I use to properly save these documents?
Miscellaneous notes for the curious:
vbNL is just a function for vbNewLine. I use it quite often and I guess I am just too lazy to type it out all the time so I made a function to shorten the text.
sDNUM is also just another function that is irrelevant to the issue.
Saving the file with a docx extension does not automatically convert it as non-macro-enabled.
Change
SaveAs Directory & FileName
to
SaveAs2 Directory & FileName, wdFormatXMLDocument
For more information on the SaveAs2 method, see here.
I work in an entirely offline facility; it has an intranet site, and is VERY limited to programs available (MS Word is the only application available).
I need to be able to open a Microsoft Word document, edit it, and then, when you save the document, it saves the file, and then saves it in .html format as well.
To elaborate, I work in a military Facility that has a network that we cannot have anything installed. On this network is a series of computers. On these computers we keep a log. We want to be able to display this log to anyone who wants to see it. The log is in a .doc format. I want someone to open the log.doc then save it. Once saved it will save an .html file. That file will be displayed for ANYONE on the network to see it.
This is a simplified version of what Word 2010's macro recorder produces when you save a file as MHT (single-file html).
ActiveDocument.SaveAs2 FileName:="c:\temp\test.mht", FileFormat:= _
wdFormatWebArchive
Adapt as needed.
I'm not sure if Word 2013 supports saving as HTML any longer. Not all of the office apps do.
The following is adapted slightly from Microsoft's Developer Network. Save this as a Macro in the copy of MS Word used by the client. This Macro will:
Save the document as a .doc(x) file, then as a .HTML file.
Sub AutoOpen()
Saver
End Sub
Sub Saver()
ActiveDocument.Save
Document_Save
End Sub
Sub Document_Save()
Dim strDocName As String
Dim intPos As Integer
'Find position of extension in filename
strDocName = ActiveDocument.Name
intPos = InStrRev(strDocName, ".")
If intPos = 0 Then
'If the document has not yet been saved
'Ask the user to provide a filename
strDocName = InputBox("Please enter the name " & _
"of your document.")
Else
'Strip off extension and add ".html" extension
strDocName = Left(strDocName, intPos - 1)
strDocName = strDocName & ".html"
End If
'Save file with new extension
ActiveDocument.SaveAs FileName:=strDocName, _
FileFormat:=wdFormatHTML
End Sub