Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old January 5th, 2005, 03:01 AM
hithere's Avatar
hithere hithere is offline
Learner
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Location: India
Posts: 450 hithere User rank is Sergeant (500 - 2000 Reputation Level)hithere User rank is Sergeant (500 - 2000 Reputation Level)hithere User rank is Sergeant (500 - 2000 Reputation Level)hithere User rank is Sergeant (500 - 2000 Reputation Level)hithere User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 5 Days 12 h 13 m 21 sec
Reputation Power: 18
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.

Reply With Quote
  #2  
Old January 5th, 2005, 04:06 AM
Doug G Doug G is offline
Grumpier Old Moderator
ASP Free God 11th Plane (10000 - 10499 posts)
 
Join Date: Sep 2003
Posts: 10,143 Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 23 h 29 m 58 sec
Reputation Power: 181
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

Reply With Quote
  #3  
Old January 6th, 2005, 12:31 AM
Sony Sony is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Bangalore,India
Posts: 180 Sony User rank is Sergeant (500 - 2000 Reputation Level)Sony User rank is Sergeant (500 - 2000 Reputation Level)Sony User rank is Sergeant (500 - 2000 Reputation Level)Sony User rank is Sergeant (500 - 2000 Reputation Level)Sony User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 18 h 54 m 28 sec
Reputation Power: 15
how to go about that Doug sounds interesting
Please specify the steps

Sony

Reply With Quote
  #4  
Old January 6th, 2005, 04:37 AM
hithere's Avatar
hithere hithere is offline
Learner
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Location: India
Posts: 450 hithere User rank is Sergeant (500 - 2000 Reputation Level)hithere User rank is Sergeant (500 - 2000 Reputation Level)hithere User rank is Sergeant (500 - 2000 Reputation Level)hithere User rank is Sergeant (500 - 2000 Reputation Level)hithere User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 5 Days 12 h 13 m 21 sec
Reputation Power: 18
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

Reply With Quote
  #5  
Old January 6th, 2005, 12:03 PM
Darius Darius is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 108 Darius User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 16 m 54 sec
Reputation Power: 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.

Reply With Quote
  #6  
Old January 6th, 2005, 03:19 PM
Mythomep Mythomep is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Location: Zaandam, The Netherlands
Posts: 70 Mythomep User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 37 sec
Reputation Power: 5
Send a message via MSN to Mythomep
Cool Easy when you know where to look

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.

Reply With Quote
  #7  
Old January 6th, 2005, 04:34 PM
Darius Darius is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 108 Darius User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 16 m 54 sec
Reputation Power: 5
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.)?

Reply With Quote
  #8  
Old January 24th, 2005, 01:22 AM
hithere's Avatar
hithere hithere is offline
Learner
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Location: India
Posts: 450 hithere User rank is Sergeant (500 - 2000 Reputation Level)hithere User rank is Sergeant (500 - 2000 Reputation Level)hithere User rank is Sergeant (500 - 2000 Reputation Level)hithere User rank is Sergeant (500 - 2000 Reputation Level)hithere User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 5 Days 12 h 13 m 21 sec
Reputation Power: 18
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:
Originally Posted by Darius
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.)?

Reply With Quote
  #9  
Old January 24th, 2005, 05:13 AM
Mythomep Mythomep is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Location: Zaandam, The Netherlands
Posts: 70 Mythomep User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 37 sec
Reputation Power: 5
Send a message via MSN to Mythomep
Arrow

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.

Reply With Quote
  #10  
Old January 25th, 2005, 09:32 AM
Rich's Avatar
Rich Rich is offline
Administrator
Developer Shed Admin.
 
Join Date: Sep 2003
Location: Fort Lauderdale, FL
Posts: 152 Rich User rank is Sergeant (500 - 2000 Reputation Level)Rich User rank is Sergeant (500 - 2000 Reputation Level)Rich User rank is Sergeant (500 - 2000 Reputation Level)Rich User rank is Sergeant (500 - 2000 Reputation Level)Rich User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 11 h 47 m 28 sec
Reputation Power: 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.

Reply With Quote
  #11  
Old January 25th, 2005, 09:42 AM
Mythomep Mythomep is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Location: Zaandam, The Netherlands
Posts: 70 Mythomep User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 37 sec
Reputation Power: 5
Send a message via MSN to Mythomep
Cool

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.

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingVisual Basic Programming > format the contents of word file thru coding


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |