Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-24-2019, 06:49 PM
jkathir jkathir is offline How to Save a word as text file after applying font with UTF-8 Windows 10 How to Save a word as text file after applying font with UTF-8 Office 2016
Novice
How to Save a word as text file after applying font with UTF-8
 
Join Date: Aug 2019
Posts: 12
jkathir is on a distinguished road
Default How to Save a word as text file after applying font with UTF-8

Hi,

i have word file which shows boxes when i opened. However when choosing a particular font, the text is being displayed. I selected all the content of the document with ctr+a and changed the font and save as .txt file with UTF-8.

Everything works fine and all are good.

Is there is any macro can do this automatically? Basically applying a font for entire document and save as .txt file with UTF-8?

Any tool or software does this?

Thanks.
Reply With Quote
  #2  
Old 08-24-2019, 08:38 PM
gmayor's Avatar
gmayor gmayor is offline How to Save a word as text file after applying font with UTF-8 Windows 10 How to Save a word as text file after applying font with UTF-8 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 3,311
gmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to all
Default

How about the following. Change the font name as appropriate.

Code:
Sub SaveUnicode()
Dim strName As String
Const strFont As String = "Times New Roman"    'the font to apply
    If Len(ActiveDocument.Path) > 0 Then
        strName = Left(ActiveDocument.FullName, InStrRev(ActiveDocument.FullName, ".") - 1) & ".txt"
    Else
        strName = Options.DefaultFilePath(wdDocumentsPath) & "\MyDocument.txt"
    End If
    ActiveDocument.Range.Font.Name = strFont
    ActiveDocument.SaveAs2 FileName:=strName, FileFormat:=wdFormatUnicodeText
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 08-24-2019, 10:59 PM
jkathir jkathir is offline How to Save a word as text file after applying font with UTF-8 Windows 10 How to Save a word as text file after applying font with UTF-8 Office 2016
Novice
How to Save a word as text file after applying font with UTF-8
 
Join Date: Aug 2019
Posts: 12
jkathir is on a distinguished road
Default How to execute the given VBA script

How to execute the given VBA script for all the documents

I have huge documents...Do i need to manually open each and every document and execute the VBA

Please let me know how to apply VBA for huge documents.
Reply With Quote
  #4  
Old 08-24-2019, 11:06 PM
jkathir jkathir is offline How to Save a word as text file after applying font with UTF-8 Windows 10 How to Save a word as text file after applying font with UTF-8 Office 2016
Novice
How to Save a word as text file after applying font with UTF-8
 
Join Date: Aug 2019
Posts: 12
jkathir is on a distinguished road
Default

i'm looking for a way to apply for all documents in a folder. I don't find a sample or way for word ...(ex: VBA Run Macro on All Files in a Folder / All Worksheets in a Workbook - Analyst Cave for excel)
Reply With Quote
  #5  
Old 08-25-2019, 12:57 AM
gmayor's Avatar
gmayor gmayor is offline How to Save a word as text file after applying font with UTF-8 Windows 10 How to Save a word as text file after applying font with UTF-8 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 3,311
gmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to all
Default

I hesitate to recommend this as it may create as many problems as it resolves, but assuming the previous macro worked and I don't see why it would as text files don't support fonts, then the following will process all the documents that match the parameters in the selected folder. Password protected documents are ignored.
The original files are unaltered.

Code:
Sub ProcessFolder()
'Graham Mayor - https://www.gmayor.com - Last updated - 25 Aug 2019
Dim strFile As String
Dim strPath As String
Dim oDoc As Document
Dim fDialog As FileDialog
Dim strName As String
Dim oColl As Collection
Dim iCol As Integer
Const strPassword As String = "?#nonsense@$" 'do not change
Const strExt As String = "doc"    'the extension of the files to process - here word 97-2003 DOC format.
Const strFont As String = "Times New Roman"    'the font to apply (text files do not support fonts?)

    Set oColl = New Collection
    Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
    With fDialog
        .TITLE = "Select folder and click OK"
        .AllowMultiSelect = False
        .InitialView = msoFileDialogViewList
        If .Show <> -1 Then
            MsgBox "Cancelled By User", , "List Folder Contents"
            Exit Sub
        End If
        strPath = fDialog.SelectedItems.Item(1)
        Do Until Right(strPath, 1) = Chr(92)
            strPath = strPath & Chr(92)
        Loop
    End With
    strFile = Dir$(strPath & "*.*")
    While strFile <> ""
        If LCase(Right(strFile, Len(strFile) - InStrRev(strFile, "."))) = strExt Then
            oColl.Add strPath & strFile
        End If
        DoEvents
        strFile = Dir$()
    Wend
    For iCol = 1 To oColl.Count
        On Error Resume Next
        WordBasic.DisableAutoMacros 1
        Set oDoc = Documents.Open(FileName:=oColl(iCol), _
                                  AddToRecentFiles:=False, _
                                  Visible:=True, _
                                  PasswordDocument:=strPassword, _
                                  ReadOnly:=True, _
                                  WritePasswordDocument:=strPassword)
        If oDoc Is Nothing Or Err.Number > 0 Then
            Err.Clear
            GoTo NextDoc:
        End If
        strName = Left(oDoc.FullName, InStrRev(oDoc.FullName, ".") - 1) & ".txt"
        oDoc.Range.Font.Name = strFont
        oDoc.SaveAs2 FileName:=strName, FileFormat:=wdFormatUnicodeText
        oDoc.Close SaveChanges:=wdDoNotSaveChanges
        WordBasic.DisableAutoMacros 0
NextDoc:
        DoEvents
    Next iCol
    MsgBox "Processing complete"
lbl_Exit:
    Set oDoc = Nothing
    Set fDialog = Nothing
    Set oColl = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #6  
Old 08-25-2019, 08:26 PM
jkathir jkathir is offline How to Save a word as text file after applying font with UTF-8 Windows 10 How to Save a word as text file after applying font with UTF-8 Office 2016
Novice
How to Save a word as text file after applying font with UTF-8
 
Join Date: Aug 2019
Posts: 12
jkathir is on a distinguished road
Default

Thank you so much...I will test and let you know...How to do the same process to preserve the changes in docx format itself?
Reply With Quote
  #7  
Old 08-25-2019, 10:23 PM
Guessed's Avatar
Guessed Guessed is offline How to Save a word as text file after applying font with UTF-8 Windows 10 How to Save a word as text file after applying font with UTF-8 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 1,983
Guessed is a name known to allGuessed is a name known to allGuessed is a name known to allGuessed is a name known to allGuessed is a name known to allGuessed is a name known to all
Default

Change the line
oDoc.Close SaveChanges:=wdDoNotSaveChanges

to
oDoc.Close SaveChanges:=True
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #8  
Old 08-26-2019, 03:56 AM
gmayor's Avatar
gmayor gmayor is offline How to Save a word as text file after applying font with UTF-8 Windows 10 How to Save a word as text file after applying font with UTF-8 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 3,311
gmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to all
Default

That won't work. If you save as TXT, oDoc is then the text file that you have just saved.

To save the file as docx, you would either have to forget saving as txt and save as docx, or simply save oDoc instead of saving it as txt. e.g.

Code:
        'strName = Left(oDoc.FullName, InStrRev(oDoc.FullName, ".") - 1) & ".txt"
        'oDoc.Range.Font.Name = strFont
	oDoc.Save
        'oDoc.SaveAs2 FileName:=strName, FileFormat:=wdFormatUnicodeText
        oDoc.Close SaveChanges:=wdDoNotSaveChanges
If you want it as txt as well as docx then

Code:
        strName = Left(oDoc.FullName, InStrRev(oDoc.FullName, ".") - 1) & ".txt"
        oDoc.Range.Font.Name = strFont
	oDoc.Save
        oDoc.SaveAs2 FileName:=strName, FileFormat:=wdFormatUnicodeText
        oDoc.Close SaveChanges:=wdDoNotSaveChanges
Or SaveAs again e.g.

Code:
        strName = Left(oDoc.FullName, InStrRev(oDoc.FullName, ".") - 1) & ".txt"
        oDoc.Range.Font.Name = strFont
        oDoc.SaveAs2 FileName:=strName, FileFormat:=wdFormatUnicodeText
	strName = Left(oDoc.FullName, InStrRev(oDoc.FullName, ".") - 1) & ".docx"
	oDoc.SaveAs2 FileName:=strName, FileFormat:=wdFormatXMLDocument
        oDoc.Close SaveChanges:=wdDoNotSaveChanges
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #9  
Old 08-28-2019, 07:37 PM
jkathir jkathir is offline How to Save a word as text file after applying font with UTF-8 Windows 10 How to Save a word as text file after applying font with UTF-8 Office 2016
Novice
How to Save a word as text file after applying font with UTF-8
 
Join Date: Aug 2019
Posts: 12
jkathir is on a distinguished road
Default

Thanks a lot. I will test and get back
Reply With Quote
  #10  
Old 09-01-2019, 09:49 PM
jkathir jkathir is offline How to Save a word as text file after applying font with UTF-8 Windows 10 How to Save a word as text file after applying font with UTF-8 Office 2016
Novice
How to Save a word as text file after applying font with UTF-8
 
Join Date: Aug 2019
Posts: 12
jkathir is on a distinguished road
Default

1. I understand the above macro
2. How to run this macro? should i open any word document
3. I tried but not sure how to run the above macro. Help me with screenshot if possible
Reply With Quote
  #11  
Old 09-01-2019, 11:00 PM
gmayor's Avatar
gmayor gmayor is offline How to Save a word as text file after applying font with UTF-8 Windows 10 How to Save a word as text file after applying font with UTF-8 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 3,311
gmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to allgmayor is a name known to all
Default

See Installing Macros
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #12  
Old 09-07-2019, 03:50 AM
Lugh's Avatar
Lugh Lugh is offline How to Save a word as text file after applying font with UTF-8 Windows 10 How to Save a word as text file after applying font with UTF-8 Office 2016
Competent Performer
 
Join Date: May 2019
Location: USA
Posts: 137
Lugh is on a distinguished road
Default

Quote:
Originally Posted by jkathir View Post
2. How to run this macro? should i open any word document
I'm no expert, but I expect the following will work:

Open the Word template you are using—Normal is the default.
Open the VBA editor—Alt+F11.
Paste Graham's code into a module—Module1 is the default.
Save the code in the editor.
If asked to save changes to template, select whatever corresponds to 'Yes'.
Close the template.

If you like, customize your Ribbon by adding the macro to a new button on it.

Open a doc in the folder, then run the macro.
Reply With Quote
Reply

Tags
save

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Save a word as text file after applying font with UTF-8 Word does not detect any text when applying style reference Stingerhawk Word 3 04-26-2019 01:33 AM
Save Document as Text File gerison Word VBA 5 11-27-2017 07:15 AM
Having Issues Applying a Macro Across Multiple Files. Changes Just Don't Seem To Save. Kajex Word VBA 2 09-08-2017 06:37 AM
Applying layout to existing slides without affecting font mgw130 PowerPoint 3 12-15-2012 04:38 AM
How to Save a word as text file after applying font with UTF-8 Word Macro: Save file as text with current file name jabberwocky12 Word VBA 2 10-22-2010 12:23 PM

Other Forums: Access Forums - Senior Forums

All times are GMT -7. The time now is 05:03 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2020 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft