Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Debug Print producing unexpected and seemingly random output

Tags:

ms-word

excel

vba

Can anyone shed some light on this. In MS Word VBA I have a trivial Sub that produces rather unexpected results. [Edit: this issue has been reproduced on several different installations of MS Word but so far only Win10]

Repro Environments:

  • Win10, Word 2010 (32-bit) PC
  • Win10, Word 2010 (32-bit) VM-Azure
  • Win10, Word 2016 (32-bit) PC
  • Win10, Word 2010 (64-bit) VM-Azure
  • Win10, Word 2016 365 @VincentG
  • Win10, Word 2007 (32 bit) @Ambie
  • Win10(64), Word XP @ThunderFrame
  • Win10(64), Excel XP @ThunderFrame
  • Win8.1(64), Word 2013 @miroxlav (first repro outside of Win10)
  • Win8.1(64), Office 2016 (32) @GSerg
  • Win10(64), Word 2007 (x86) @PartricK
  • Win10(64), Excel 2007 (x86) @PartricK

But no repro in:

  • Win7 (64), Word 2007 (32-bit) VM Azure
  • Win7 (64), Word 2010 (32?) @JohnColeman
  • Win7 (64), Word 2007 (32) @Slai
  • Win7 (64), Word 2003 (32) @GSerg
  • WinXP (32), Word 2010 (32) @GSerg
  • Win7 (64), Office 2010 (32)

Steps to reproduce: Open new Word document Open IDE (Alt + F11) Enter this code

Option Explicit

Sub test()
Dim i As Integer
For i = 1 To 100
Debug.Print vbCr
Next
End Sub

Run the test Sub.

My output varies each time the loop is run:

]Œ[p^"î;{Ñ[

]Œ[p^"î;{Ñ[

\ÒZžUÖè;qÑZ

or

ÿÿ

þþ@øø”¶(3

ÿÿ

or

>O

>O

>O

or

p

G   P‚j Ž¶&3@

p

Originally I thought I could recognize some content from my document, but now I get similar results for a blank document with nothing else open in Word.

It was quite disconcerting until I reproduced in the other Word installations, now its more of a distraction & puzzlement.

Any ideas about what's going on?

like image 590
SlowLearner Avatar asked Feb 01 '17 03:02

SlowLearner


1 Answers

@Comintern and I have long speculated that the Immediate Window is in fact a pipe, or a conduit for stdIn/stdOut.

If we work on that basis, then this answer has some insights:

https://stackoverflow.com/a/25843879/5757159

A text stream is an ordered sequence of characters composed into lines, each line consisting of zero or more characters plus a terminating new-line character.

Characters may have to be added, altered, or deleted on input and output to conform to differing conventions for representing text in the host environment.

So it seems that Windows 10, might be getting something wrong when it processes the streams?

The workaround is to ensure that Debug.Print statements never end in vbCr, Chr(10) or Chr$(10)

like image 127
ThunderFrame Avatar answered Nov 15 '22 19:11

ThunderFrame