Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tab Spacing all messed up.

Status
Not open for further replies.

dswaff

IS-IT--Management
Aug 10, 2015
21
US
I have a text file that I need some users to open in word.
I created the file via VBScript.
In notepad or notepad++ the file looks great.

Open the same file in word and the tab spacing is all messed up. Some times items in a row are single tabbed, or triple tabbed.

What can I do to make it format / read right?
 
Just in case anyone is curious, the script in vb to create the word file and set the expanded spacing is as follows:


Function ConvertTxtToWord(TxtFilePath)
Dim objshell,ParentFolder,BaseName,wordapp,doc,WordFilePath,objDoc
Dim objSelection
Set objshell= CreateObject("scripting.filesystemobject")
ParentFolder = objshell.GetParentFolderName(TxtFilePath) 'Get the current folder path
BaseName = objshell.GetBaseName(TxtFilePath) 'Get the text file name
WordFilePath = parentFolder & "\" & BaseName & ".doc"
Dim objWord
set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add()
Set objSelection = objWord.Selection
With objWord
.Visible = False
End With
With objdoc
.PageSetup.Orientation = 1
.PageSetup.TopMargin = 1
.PageSetup.BottomMargin = 1
.PageSetup.LeftMargin = 0
.PageSetup.RightMargin = 0

end with

set objSelection=objWord.Selection
objSelection.InsertFile TxtFilePath
Set objRange = objDoc.Range(txtfilepath)
objRange.Font.Name = "Courier New"
objRange.Font.Size = "8"
objRange.Font.Spacing = "1"
objSelection.ParagraphFormat.LineSpacing = "2"
objSelection.ParagraphFormat.Alignment= "0"
objDoc.saveas(WordFilePath)
objDoc.close
objWord.Quit
End Function
 
Expand/Condense seems to make no difference in my Word. It all lines up.

I did looked at your BadExample.png and it looks like Times Roman or some such variable pitch font.

Are you sure that you are...

1) selecting ALL DATA in your documant

2) then selecting the Courier New font while ALL DATA is selected?

If so, can you do a redacted .png with Courier.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
dswaff,

There is nothing wrong with what Word is doing with your NotePad data which, by the way, contains no tabs. You should note too, that a NotePad file doesn't use either a monospaced font or a proportional one. In fact, a NotePad file contains no font information at all. Your monospaced font display in NotePad is just that - a display setting, not a format of the text itself.

In Word, characters in a given monospaced font are all given exactly the same width, in proportion to the point size. Simply expanding all the widths by 0.176389mm as you have tried to do has no effect whatsoever on whether you'll get a fixed width. All that will do is add a constant amount to every character's width, regardless of whether the underlying character uses a monospaced font or a proportional one. They'll have exactly the same width differences after applying the adjustment as they did before. If you want Word to display your data monospaced, format the content in Word with a monospaced font.

Your 'bad Word' example doesn't even use a monospaced font - it's using a proportional one (TNR), so it's a particularly poor example of Word supposedly not displaying a monospaced font correctly.

Notwithstanding any of the above, what can mess things up with the appearance of monospaced fonts in Word, though, is applying a Style that uses:
• paragraph justification and having content that wraps to the next line.
• text distribution
If the text file also contains tabs, instead of being spaced-out as yours is, the tab-stops in Word can result in a different tabbed appearance.

Your Function could be simplified and its efficiency improved with:
Code:
Function ConvertTxtToWord(TxtFileFullName As String)
Dim objWord As Object, objDoc As Object
Set objWord = CreateObject("Word.Application")
With objWord
 .Visible = False
  Set objDoc = .Documents.Open(TxtFileFullName)
  With objDoc
    With .PageSetup
      .Orientation = 1
      .TopMargin = 1
      .BottomMargin = 1
      .LeftMargin = 0
      .RightMargin = 0
    End With
    With .Range
      .Font.Name = "Courier New"
      .Font.Size = "8"
      .ParagraphFormat.LineSpacing = "2"
      .ParagraphFormat.Alignment = "0"
    End With
    .SaveAs Replace(.FullName, ".txt", ".doc")
    .Close
  End With
  .Quit
End With
Set objWord = Nothing: Set objDoc = Nothing
End Function

Cheers
Paul Edstein
[MS MVP - Word]
 
Actually, I suspect you'll find that the issue is because you have enabled support for Asian languages. As a result I suspect that the way Word handles Asian fonts is affecting how the monospaced font is displayed.

One possible fix for this is to UNCHECK the "Balance SBCS characters and DBCS characters". You'll have to check your documentation for where to find this in your particular version of Word. In Word 2010 it is under File/Options/Advanced/Compatibility Options .../Layout Options

 
Thanks All. You are correct. The screenshot has the wrong font type. My Mistake. Although it does look the same way using a Monospaced font.
Strongm you are correct. "Balance SBCS characters and DBCS characters" seems to be the issue. Any Idea how to script turning that off. I can only find how to in an RTF file.
I dont see the word command for it.

Macropod. Thank you for your example. I tried doing it this way, and using your script. However I get returned an invalid doc type when opening the file it generates.
 
Code:
[blue]    wdDontBalanceSingleByteDoubleByteWidth = 16
    objDoc.Compatibility(wdDontBalanceSingleByteDoubleByteWidth) = True[/blue]
 
Instead of:
.SaveAs Replace(.FullName, ".txt", ".doc")
try:
.SaveAs2 Replace(.FullName, ".txt", ".doc"), 0

Cheers
Paul Edstein
[MS MVP - Word]
 
You might want to look up the SaveAs method to see what macropod is getting at; it's quite important, given what you are trying to do.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top