retaining Textpad syntax coloring in Microsoft Word

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, MudGuard

Post Reply
emeier
Posts: 1
Joined: Wed Aug 29, 2007 3:44 pm

retaining Textpad syntax coloring in Microsoft Word

Post by emeier »

I would like to be able to provide code snippets in a Microsoft Word document and retain the syntax coloring from Textpad (version 5.03). Anyone have a convenient method for doing this?

emeier
ACRobin
Posts: 134
Joined: Fri Nov 04, 2005 9:51 pm
Location: Northumberland,UK
Contact:

Post by ACRobin »

Easiest way I can think of is to press "print screen" to copy the screen image to clipboard, use MS-Paint to crop what you want, select what is left and paste into word.

This fine if your code fits on one screen when you capture the screen to clipboard.

Or just past the text into Word and use Word's tools to colour it.
User avatar
MudGuard
Posts: 1295
Joined: Sun Mar 02, 2003 10:15 pm
Location: Munich, Germany
Contact:

Post by MudGuard »

Select all, then Context-Menu - Copy Other - As Html Page.

Should work when inserted into Word ...
bveldkamp

Post by bveldkamp »

Hm, somehow pasting in Word results in the actual html tags for me. I had to paste in a new textdocument, save with the .html extension, open in IE (not Firefox), then Select All | Copy, and finally paste in Word
baryh1
Posts: 2
Joined: Sat Aug 09, 2008 10:44 pm

Post by baryh1 »

This is something I've wanted to be able to do easily for a long time. I commonly copy code fragments and paste them into e-mail messages or word documents, and it would be very nice to retain the syntax coloring.

Images take up too much space, and it takes too many steps to use Copy Other -> as HTML Page.

I've determined a way to automate most of the steps using a TextPad Macro and an external script, but I cannot determine any way to get a Macro to invoke an external script or executable.

Following are the steps necessary:

0. Starting point: text to copy is selected in Textpad
1. Copy Other -> As HTML Page
2. File -> New
3. Edit -> Paste (in new document)
4. File -> Save As...
5. enter "%temp%\Document1.html" for filename and select Save, then Overwrite.
(Steps 1-5 can be done as a TextPad Macro)

6. Open "%temp%\Document1.html" in Internet Explorer
7. Edit -> Select All
8. Edit -> Copy
(Steps 6-8 can be done in VB using the Microsoft Internet Controls library - Shdocvw.dll)

I'm looking for a way to connect all of this into one step, but I seem to be lacking a way to get a Macro to invoke an external script or program. If there is a way to do that, please let me know.

Thanks,
BaryH
baryh1
Posts: 2
Joined: Sat Aug 09, 2008 10:44 pm

Two Step Solution

Post by baryh1 »

I haven't been able to determine how to do this in one just one action, but I have a solution that can be done in two steps.

I realized that steps 2 - 8 from the previous post can all be done in VB, so that eliminates the need for a TextPad macro at all.

Following is the VB6 code to do this:

Code: Select all

Public Sub Main()

Dim oFS As FileSystemObject
Dim oFile As TextStream
Dim tmpFileName As String, textAsHTML As String

'Grab the text from the System Clipboard
textAsHTML = Clipboard.GetText

'Put the temporary file in the user's TEMP directory
tmpFileName = Environ("TEMP") & "\Document1.html"

Set oFS = CreateObject("Scripting.FileSystemObject")
'Create the file (Overwrite = True), write the text from the Clipboard, and Close
Set oFile = oFS.CreateTextFile(tmpFileName, True)
oFile.Write textAsHTML
oFile.Close

Dim oIE As InternetExplorer
Set oIE = CreateObject("InternetExplorer.Application")

'Open the temporary file in the browser
oIE.Navigate (tmpFileName)

' Ensure the browser is ready
Do While oIE.Busy
Loop

' Do Select All, then Copy Commands in browser
oIE.ExecWB OLECMDID_SELECTALL, OLECMDEXECOPT_DODEFAULT
oIE.ExecWB OLECMDID_COPY, OLECMDEXECOPT_DODEFAULT

oIE.Quit

'One can delete the temporary file here, if desired.
'oFS.DeleteFile tmpFileName

End Sub
This can be compiled into an executable, and then configured as an External Tool in TextPad.

Then you can assign a shorcut key to it. For example, you could assign Cntl+1 to EditCopyAsHTML and then Cntl+2 to this external tool. Thus, with two quick keystrokes, you have something ready to paste into Word or e-mail that retains the syntax coloring.

Note: I wanted to do this in VBScript which would be easier to distribute, but I could not get the ExecWB statements to work.

BaryH
Post Reply