Using this VBA code, is there anyway to affect the font style of the Word file produced?

wittonlin

Board Regular
Joined
Jan 30, 2016
Messages
144
For some reason, maybe because it's created from a text file?

The type style is always saved as Courier New with a font size of 10.5, often too small for people to read, especially on a mobile device.

Code:
Sub SaveAsTextFileFixedSilentClose()
       
        Application.DisplayAlerts = False
        
        ActiveWorkbook.SaveAs FileName:=strFile & "_Word" & ".txt", _
            FileFormat:=xlText, CreateBackup:=False
        SaveasDoc
        strSavedFiles = strSavedFiles & ";;" & strFile1 & ".doc"
        Application.DisplayAlerts = False
        ActiveWorkbook.Close
        Kill strFile & "_Word" & ".txt"
        
End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
What is the code for SaveasDoc?
 
Upvote 0
Oh crud, because there was no 'Call' I missed that!

Code:
Sub SaveasDoc()
    
    Dim WordApp As Object, WordDoc As Object
    Dim fullname As String
    Dim fullnametxt As String
    
    'fullname = Application.ActiveWorkbook.fullname
    'fullnametxt = Left(fullname, Len(fullname) - 4) & ".txt"
        
    Set WordApp = CreateObject("Word.Application")
    Application.DisplayAlerts = False
    
    WordApp.Visible = True
    
    WordApp.Documents.Open FileName:=strFile & "_WORD" & ".txt", ReadOnly:=True
    
    Application.DisplayAlerts = False
    
    WordApp.activedocument.SaveAs2 strFile1 & ".doc"
    
    Application.DisplayAlerts = False
    
    WordApp.activedocument.Close
    WordApp.Quit
    
    Set WordApp = Nothing
    Set WordDoc = Nothing
    
End Sub
 
Last edited:
Upvote 0
With the help of the additional code, does anyone have any ideas how to change the typestyle of the produced Word doc? :)
 
Upvote 0
Change the font and size before saving (e.g., before the line WordApp.activedocument.SaveAs2 strFile1 & ".doc")

Code:
[COLOR=#333333]WordApp.[/COLOR]ActiveDocument.Range.Font.Name = "Arial"
[COLOR=#333333]WordApp.[/COLOR]ActiveDocument.Range.Font.Size = 12

 
Upvote 0
Change the font and size before saving (e.g., before the line WordApp.activedocument.SaveAs2 strFile1 & ".doc")

Code:
[COLOR=#333333]WordApp.[/COLOR]ActiveDocument.Range.Font.Name = "Arial"
[COLOR=#333333]WordApp.[/COLOR]ActiveDocument.Range.Font.Size = 12


Well initially it worked, I saw the format change! But opening the file it's back to Courier 10.5.

Even trying to manually save the file in a new format it won't preserve the new format!

It's like our code is coding the Word doc as a text doc, just savinging is a .doc

Manually saving the produced DOC gives this message....

FILE.doc may contain features not compatible with Plain Text format. Do you want to save the document in this format?
-To save, click Yes
-To preserve formatting, click No. Then save a copy in the latest Word format.


Maybe this line is the culprit!

Code:
WordApp.Documents.Open FileName:=strFile & "_WORD" & ".txt", ReadOnly:=True
 
Last edited:
Upvote 0
You're probably right about the format not changing. Though the issue is with saving, not opening.

Per the discussion HERE, you can specify the new format, which should be what you need to do. In the FileFormat section, there's a link to the different WdSaveFormats you can use. Since you're using DOC as the extension, I'm not sure if you're using an old version of Word or if you simply prefer to convert to the older format. It looks like wdFormatDocument is what you'd be looking for.

Code:
[COLOR=#333333]WordApp.activedocument.SaveAs2 strFile1 & ".doc", wdFormatDocument[/COLOR]
 
Upvote 0
You're probably right about the format not changing. Though the issue is with saving, not opening.

Per the discussion HERE, you can specify the new format, which should be what you need to do. In the FileFormat section, there's a link to the different WdSaveFormats you can use. Since you're using DOC as the extension, I'm not sure if you're using an old version of Word or if you simply prefer to convert to the older format. It looks like wdFormatDocument is what you'd be looking for.

Code:
[COLOR=#333333]WordApp.activedocument.SaveAs2 strFile1 & ".doc", wdFormatDocument[/COLOR]


Unreal! I couldn't figure this out for a year, off on on of course, lol... and you did it with ONE word. A HUGE thank you shknbk2!!!

I wish I could give you MORE than one like! God I love this site!!!
 
Last edited:
Upvote 0
You're very welcome and thanks for the feedback. That's why I like helping on this site.
 
Upvote 0

Forum statistics

Threads
1,213,537
Messages
6,114,216
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top