PDA

View Full Version : Notepad text to Word document macro....



Chuck Wintle
03-28-2015, 7:21 AM
What I want to do it to be able to move some notepad text into a Word document using a macro. They way we do it now is to copy and paste this text into word but the formatting is lost. I then need to fiddle with the text to get it to look right. How can I make this macro?

Robert LaPlaca
03-28-2015, 8:07 AM
Virtual Basic for Appliations (VBA) is the tool of choice, it will allow you to automate your process.. He is a link describing https://msdn.microsoft.com/en-us/library/office/ff604039%28v=office.14%29.aspx

Chris Merriam
03-28-2015, 10:16 AM
Instead of opening your text file in Notepad, just open it from Word. Is there a repetitive operation you are doing with these text files?

Robert LaPlaca
03-28-2015, 10:40 AM
Instead of opening your text file in Notepad, just open it from Word. Is there a repetitive operation you are doing with these text files?

The VBA magic would happen in Word the way the processing happens kind of depends on the type pf process... If its batch, each of the Notepad text files to need to have some commonality in extension/file name and reside in a common folder location to make the coding easier.. Going to need to iteratively open the Notepad file, process the file in Word and or print or save the file in Word..

With VBA one can do almost anything..

Chuck Wintle
03-28-2015, 11:06 AM
Instead of opening your text file in Notepad, just open it from Word. Is there a repetitive operation you are doing with these text files?

that is a good idea. I will need to talk to the company that has made this software.

Chuck Wintle
03-28-2015, 11:07 AM
The VBA magic would happen in Word the way the processing happens kind of depends on the type pf process... If its batch, each of the Notepad text files to need to have some commonality in extension/file name and reside in a common folder location to make the coding easier.. Going to need to iteratively open the Notepad file, process the file in Word and or print or save the file in Word..

With VBA one can do almost anything..

this approach may work also.

Chuck Wintle
03-28-2015, 11:09 AM
Instead of opening your text file in Notepad, just open it from Word. Is there a repetitive operation you are doing with these text files?

yes this operation is repetetive and time consuming...open notepad with the text file, copy and paste into word to maintain formatting. When notepad is opened the text is there but if i save it and then reopen the text is all over the place in notepad.

Robert LaPlaca
03-28-2015, 11:16 AM
yes this operation is repetetive and time consuming...open notepad with the text file, copy and paste into word to maintain formatting. When notepad is opened the text is there but if i save it and then reopen the text is all over the place in notepad.

Chuck, you will want to not overwrite the original Notepad file with the Word formatted document, save to a new filename or extension.. Notepad operates on standard ASCII formatted files, chances are good that after you perform formatting magic in Word, the file will no longer be a ASCII formatted document to properly support the new formatting, more than likely it will be either MS-Word formatted binary file, or the dreaded Rich Text Formatted (RTF) file, that Notepad doesn't understand..

Chuck Wintle
03-28-2015, 11:22 AM
Instead of opening your text file in Notepad, just open it from Word. Is there a repetitive operation you are doing with these text files?

yes notepad opens within another program and contains a summary of the test run. The main program is a program that we use to make test runs on various products that we make. the summary is useful for the engineers to know what happened during a test.

Art Mann
03-28-2015, 12:46 PM
I hate to be a bearer of bad news but what you are wanting to do may be possible. Notepad provides almost zero formatting attributes. I am speculating that what you are seeing when you import the text is not "formatting" in the traditional sense but rather the manually inserted carriage return/line feed code that was put into the notepad file by hitting the <Enter> key. When Word attempts to repaginate the text, it sees the CR/LF control and starts the succeeding text on a new line. If this is what is going on, then you will probably have to manually delete the CR/LF control characters. I hope I am wrong.

Chris Merriam
03-28-2015, 5:01 PM
I hate to be a bearer of bad news but what you are wanting to do may be possible. Notepad provides almost zero formatting attributes. I am speculating that what you are seeing when you import the text is not "formatting" in the traditional sense but rather the manually inserted carriage return/line feed code that was put into the notepad file by hitting the <Enter> key. When Word attempts to repaginate the text, it sees the CR/LF control and starts the succeeding text on a new line. If this is what is going on, then you will probably have to manually delete the CR/LF control characters. I hope I am wrong.

I was confused about that too, Notepad doesn't offer any formatting options, that's why I suggested just opening directly in Word.

Either way, VBA can pretty much automate anything as long as you can come up with a concrete rule set, ie, search for a particular word in the txt file, or a particular line number, and copy it over to Word or whatever. You mention though that the text file is opened by another program. Your VBA code will be MUCH simpler if you save that txt file to your PC first. Otherwise in your VBA code you will need to search for any running Notepad applications (hopefully you only have one open!), and you'll be limited to essentially replicating keystrokes. If you save the file first, you can open it directly in VBA and leverage all the text file tools available.