|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
format the contents of word file thru coding
Dear Friends,
I have created one word document by using 'open "file.doc" for output as #1'. the contents to be written in this document are coming from the xml file. but i also want to format the contents of word file by using VB coding (examle : Bold,size, justify the whole doc also....) coz i want to print this document but I just dont know how to go abt it . if someone can please help me thanx in advance. |
|
#2
|
|||
|
|||
|
I don't know word programming, but you can create an instance of word from your vb code, and use the word formatting properties to format your document.
Search google for tutorials on office automation or using ms word from vb or something similar.
__________________
====== Doug G ====== I didn't attend the funeral, but I sent a nice letter saying I approved of it. --Mark Twain |
|
#3
|
|||
|
|||
|
how to go about that Doug sounds interesting
Please specify the steps Sony |
|
#4
|
||||
|
||||
|
hey searching this on google doesnot give what i want.so if someone could please provide a sample code of how to do this. i have tried some code for formatting but it gives only bold and italics effect. i dont know how to change the font style or font size. Another problem with this is that is applies to whole document bit i want to apply to only some part of the document.
if someone can please help me. This is what i have tried dim oWord as new word.application private sub command1_click() oWord.Visible = True Set tmpdoc = oWord.Documents.Add With tmpdoc .Content.Italic = True .Content.Bold = True .Content = .Content & "To," .Content = .Content & vbNewLine .Content = .Content & Space(3) & "hithere" & Space(1) & "How r u" .Content.CheckGrammar .Content.CheckSpelling .Content.Case = wdUpperCase .Content.Italic = False .Content.Bold = False .Content = .Content & Space(1) & "Yours truly" End With A = InputBox("Enter full file name", "Word file") tmpdoc.SaveAs A End End Sub |
|
#5
|
|||
|
|||
|
I know how to open a word file and add the text but with the .content all document is affected by the change of format, have you tried richtextbox component (rtf documents), it's a lot easer than word (with the exception of justificated format paragraph that does'nt exist on it) or it has to be word.
|
|
#6
|
|||
|
|||
|
Hi,
Manipulating MS-Word documents (or any office document in general) is easy if you just dig into the subject of Office. Microsoft has a great deal of information about how to do magic to any office program. Look in the MSDN Library look under Office Solutions Development. In general, your code will look something along the lines of this: Code:
oDocument.Range("WhateverRange").Font.Name = "Tahoma"
Manipulating pieces of the document becomes easy. I have build several systems where the output was a fully formatted document straight from code. Good luck!. Grtz.© M. |
|
#7
|
|||
|
|||
|
Dim WRD As Word.Document
Function IsRunning(EXE_FILE As String) Dim wmi As Object, sQuery As String, Processes As Collection Set wmi = GetObject("winmgmts:") sQuery = "select * from win32_process where name='" & EXE_FILE & "'" IsRunning = (wmi.execquery(sQuery).Count > 0) End Function Function StartApp(AppName As String) As Object Dim olApp As Object If IsRunning(AppName & ".exe") Then If AppName = "WINWORD" Then Set olApp = GetObject(, "WORD.Application") Else Set olApp = GetObject(, AppName & ".Application") End If Else If AppName = "WINWORD" Then Set olApp = CreateObject("WORD.Application") Else Set olApp = CreateObject(AppName & ".Application") End If End If Set StartApp = olApp Set olApp = Nothing End Function Private Sub Command1_Click() Set WRD = CreateWordDocument("test.doc") With WRD LNG = Len(.Content) .Content.Italic = True .Content.Bold = True .Content = .Content & "To," .Content = .Content & vbNewLine .Content = .Content & Space(3) & "hi there" & Space(1) & "How r u" .Range(LNG, Len(.Content)).Font.Bold = True .Range(LNG, Len(.Content)).Font.Italic = True LNG = Len(.Content) .Content = .Content & Space(1) & "Yours truly" .Range(LNG, Len(.Content)).Font.Bold = False .Range(LNG, Len(.Content)).Font.Italic = False .Range(LNG, Len(.Content)).Font.Color = wdColorBlue .Range(Len(.Content) - 1, Len(.Content)).Font.Color = wdColorBlack End With End Sub Function CreateWordDocument(NAME As String) As Word.Document Dim objWord As Word.Application Dim WordDoc As Word.Document Set objWord = StartApp("WINWORD") objWord.Visible = True If objWord.Documents.Count = 0 Then Set WordDoc = objWord.Documents.Add Else Set WordDoc = objWord.ActiveDocument End If objWord.DisplayAlerts = False 'delete content if word doc is opened before. For i = 1 To objWord.Documents.Count If objWord.Documents.Item(i).NAME = NAME Then objWord.Documents.Item(i).Content = "" End If Next i WordDoc.Select WordDoc.SaveAs NAME 'Turn back on alerts so user will be notified to save on exit. objWord.DisplayAlerts = False Set objWord = Nothing Set objDocuments = Nothing Set CreateWordDocument = WordDoc End Function But still why not richtextbox (it's easer and IMO less memory loss >> object on the form, not opening an application etc.)? |
|
#8
|
||||
|
||||
|
hey i understood this code.but what i want to do is something different. i am having a template file and i want the tags in it to be replaced by database fields and still preserve the formatting. This will be done for the total no. of records in the databse. i have done the coding for it. i copy the contents of the template file and copy it in the word document and then find for the tag names and replace them with database fields. but the formatting is not preserved.
so how to go about it Quote:
|
|
#9
|
|||
|
|||
|
Hi,
If I understand you correctly, you have x amount of records that result in the same x amount of documents. If that is the case, use the mailmerge functionality in MS-Word. Create a csv file or an excel file that you put the data in, and make a template in word that will contain the fieldnames. Then, just splash the contents of the recordset in excel and open the word template. Very easy to do, and I've done it for the company I work for (every year we send about 2500 christmas cards for example) and it hasn't given an itch in almost 6 years (code is still good). For information about how to use mailmerge and more specific information, take a look at the MSDN library. Grtz.© M. |
|
#10
|
||||
|
||||
|
Here's a novel concept, have you checked out WOTSIT? http://www.wotsit.org/
If you dont want to depend on having Word installed, you can probably find the format of a word file here and write to it directly. |
|
#11
|
|||
|
|||
|
Hi,
While wotsit is a very usefull resource to find information about different formats, it caries a hefty cost on development. You need to write a complete parser/writer for the format itself. With respect to all that browse these forums, but writing a parser/writer that size is simple out of the question. Most of us programmers just don't have the knowledge or the time to build such a thing. There might be some cool control floating around on the internet that does exactly that, but it does involve risks you need to calculate. What if the format for MS-Word files changes from the current XML to "TNML" (The Next Markup Language) &tm;? In this specific case, it is much easier to use a mailmerge setup to accomplish a certain task. Manipulating Microsoft Office applications is documented, and you can find much examples on how to do it. Remember one of Murphy's laws: If you find yourself doing complex tasks to solve one problem, the solution is often obtained by simple inspection. The problem at hand here is how to get x amount of records into x amount of pre-defined documents. Mailmerge is the obvious solution to this trivial problem. Grtz.© M. |
![]() |
| Viewing: ASP Free Forums > Programming > Visual Basic Programming > format the contents of word file thru coding |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|