PDA

View Full Version : Solved: Keep Source Formatting



petedw
06-08-2005, 07:16 AM
Hi,

Is there any way of using VBA to tell Word to insert a file (*.doc) and keep that files formatting??

Many Thanks

Pete

fumei
06-08-2005, 09:08 AM
Please clarify. Are you asking about inserting an existing document into another document with DIFFERENT format?

Is the document being inserted have Styles? Does the document it is being inserted INTO have different Styles?

Or do they both have manual format?

MOS MASTER
06-08-2005, 09:38 AM
Hi, :yes

Cave-at's depend on the type of Formatting applied to the combined documents.

But in most case you'll be fine if you put each document into its own section!

So make sure you insert a Section brake after you insert a file. (Or arrange it in the page-setup):whistle:

fumei
06-08-2005, 10:05 AM
Joost, this depends a lot on whether their are real templates in action, or not.

MOS MASTER
06-08-2005, 10:22 AM
Joost, this depends a lot on whether their are real templates in action, or not.
Yes this is true and that's one of the cave-at's but I'm sure there are a lot more because Word has so many formatting capabillity's.

But for starters deviding them in sections will work. :hi:

petedw
06-09-2005, 12:33 AM
The Document that the file is being inserted into is;

Font Name = Arial
Font Size = 20

The Documents that are being inserted are also;

Font Name = Arial
Font Size = 20

Occasionally when the file is inserted, 1 of the following 3 things tends to happen,

the font changes into Times New Roman
the font size changes to 12 and
the text in bold changes to regular, and the regular text changes to bold.:banghead:

Any Idea's how to sort this??

Copy and Pasting the Document and selecting the "keep source formatting" button will sort the problem but i have a macro setup to INSERT a chosen file from a folder. Why couldn't Microsoft give us a "keep source formatting" button when inserting files!! :dunno

petedw
06-09-2005, 03:15 AM
If there is a solution to this then i would like to hear it, but don't worry yourselves about it now. I have come up with a different solution to my problem.

Thanks

Pete

MOS MASTER
06-09-2005, 09:33 AM
If there is a solution to this then i would like to hear it, but don't worry yourselves about it now. I have come up with a different solution to my problem.

Thanks

Pete
Well pete the sollution is to work with sections like I said before. (Did you try this?)

But its good to hear you've found a workaround! :yes

Would you be so kind to share your sollution with us? :whistle:

petedw
06-10-2005, 12:22 AM
My solution was to tell vba to open the file i want to insert, copy the text, close the file and paste keeping source formatting into the original document.

Not the best solution but it works so that'll do me!:yes

MOS MASTER
06-10-2005, 02:30 PM
Hi Pete, :yes

Thanks for sharing.

I still wonder if you've tried the Insert Break (Section) option?

Would you like a little demo? :whistle:

EricFletcher
06-14-2005, 06:26 AM
Joost: The section break will allow the page formatting to be retained but not the formatting differences due to styles. The clue here is that the anomolous formatting ended up being Times New Roman 12 pt: the typical "default" used by Word. I expect that the documents were manually formatted, and styles were not being used.

fumei
06-14-2005, 07:05 AM
Which is why I asked about styles in the first place. If styles are used.....and all Word documents should...then a LOT of problems (not just this one) could be handle much easier.

EricFletcher
06-14-2005, 07:58 AM
Which is why I asked about styles in the first place. If styles are used.....and all Word documents should...then a LOT of problems (not just this one) could be handle much easier.

I couldn't agree more. :thumb The way many people use Word, I wonder why they don't save themselves a lot of cash and just use WordPad instead. Word is extreme overkill if all someone wants is a text editor.

fumei
06-14-2005, 08:20 AM
Hahahaha, thanks Eric. Too true. Too true. I teach Word and I get very frustrated at times with the "Using styles is too complicated" attitude.

I point out that you can not use Word without styles. Normal IS a style. EVERY paragraph uses a style...so why not use them correctly?

Frankly, it is the very rare person who actually has need for even most of the features in Word. As for using all of the features of Word....there is no one. Simply because there are some features of Word that are simply weird and dumb. I love Word, but that does not prevent me from recognizing that some of it IS weird and dumb.

For example, Word is a word processor. Pretending it is an HTML editor is marketing and nothing more. It bloat its HTML so much that it does not walk to a browser, it waddles and rolls like a fat duck. Excuse me, but six lines of code to make a space????!!! In most cases, writing HTML is BETTER with WordPad.

However, with Styles, Bookmarks, FormFields, proper use of templates, and excellent coding capabilities (although sometime also weird and dumb) with VBA, and other tools, as a word processor, Word is top notch.

Oh, and another rant. Word is not a graphic layout application. it does a fair job, but really....you want pixel level layout...get Quark XPress.

MOS MASTER
06-14-2005, 10:03 AM
Joost: The section break will allow the page formatting to be retained but not the formatting differences due to styles. The clue here is that the anomolous formatting ended up being Times New Roman 12 pt: the typical "default" used by Word. I expect that the documents were manually formatted, and styles were not being used.
Hi Eric and Gerry, :yes

Yes guys I allready agreed that this was one of the cave-ats I mentioned before. :rofl:

But like I said before Word has so many formatting capibillities that you can expect a whole lot more problems depending on what you're using. (not only style issues)

I would however not state that you can't make this work due to formatting differences in styles!

The InsertFile method brings in a lot more then just pagesetup it also brings allong the styles or formats you've used. (And the paramarkers that hold the formats as well breaks who have the same function)
If documents are no longer bound to a template (They originated from) Word will still hold the Styles that where used in that template. (Up to date or not) Word will always remember the used formats in Strict formating styles or those styles we don't like of manual formatting.

Yes again I totally agree strict formatting is o whole lot better then manual formatting and how bigger the document gets the more happy you'l be using strict formatting. So that's not the issue here.

You state that all will default back to Words love for Times New Roman 12 points. Depending on how you program things and depending on the documents used that might be true or false. In my opinion it could work.

A little demonstration of how it could work. I've attached a "test.doc" which will insert all the documents in the attachment into a new document. Some have strict formatting some have manual formatting. All of them will attach to Normal.dot because the right templates aren't there.

The code you could use could look something like this:
Option Explicit
Const sExt As String = "doc"

Sub InsertDocsOk()
Dim oDoc As Word.Document
Dim sRoot As String
Dim sFile As String

Application.ScreenUpdating = False
Set oDoc = Application.Documents.Add
sRoot = ThisDocument.Path & Application.PathSeparator
sFile = Dir(sRoot & "*." & sExt)

Do While Len(sFile) > 0
With Selection
.PageSetup.SectionStart = wdSectionNewPage
.InsertFile FileName:=sRoot & sFile

sFile = Dir()
If Len(sFile) <> 0 And sFile <> "Test.doc" Then
.InsertBreak Type:=wdSectionBreakNextPage
End If

.EndKey Unit:=wdStory, Extend:=wdMove
End With
Loop

Set oDoc = Nothing
End Sub


The result over here was that all formatting was nicely preserved during concenation.

Again I'm not saying this is the best method of the book. Using templates and styles and strict formatting would be my favorite as well! (just that it could work)

Later...:whistle:

MOS MASTER
06-14-2005, 10:04 AM
I couldn't agree more. :thumb The way many people use Word, I wonder why they don't save themselves a lot of cash and just use WordPad instead. Word is extreme overkill if all someone wants is a text editor.
And I totally agree on this one as well! :thumb