Solved

Opening a word document from within VB6.0

Posted on 2000-03-02
7
1,127 Views
Last Modified: 2008-02-20
How do you open a word document from within VB6.0, and pass in a variable (like a reference number "Y0123465") and have this reference number appear in the word document that you open?
0
Comment
Question by:TGB
  • 4
  • 2
7 Comments
 
LVL 22

Expert Comment

by:ture
ID: 2576201
TGB,

Something like this, perhaps?

Private Sub Command1_Click()
  'For this to work, you must set a reference to
  'the Microsoft Word 8.0 (or 9.0) Object Library
   
  'Declare object variables
  Dim wd As New Word.Application
  Dim doc As Word.Document
   
  'Open a document and set variable to refer
  'to the document
  Set doc = wd.Documents.Open("c:\test\test.doc")
 
  'Type text in a new paragraph at end of document
  wd.Selection.EndKey wdStory
  wd.Selection.TypeParagraph
  wd.Selection.TypeText "This is a text"
 
  'Close document, saving it
  doc.Close SaveChanges:=True
   
  'Quit Word
  wd.Quit
   
  'Reset object variables
  Set doc = Nothing
  Set wd = Nothing
End Sub

Ture Magnusson
Karlstad, Sweden
0
 

Author Comment

by:TGB
ID: 2576238
Adjusted points to 80
0
 

Author Comment

by:TGB
ID: 2576239
Thanks for the above. It goes someway to answering our question, but our main problem is this: We want to define a field at a set position in a given Word document, and pass this over from the VB program. (So, for example, a reference number given in the VB program, appears on the second line in the Word Document, next to the heading "Reference:") Any ideas?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 22

Expert Comment

by:ture
ID: 2576263
How about this, then?

Private Sub Command1_Click()
  'For this to work, you must set a reference to
  'the Microsoft Word 8.0 (or 9.0) Object Library
   
  'Declare object variables
  Dim wd As New Word.Application
  Dim doc As Word.Document
   
  'Open a document and set variable to refer
  'to the document
  Set doc = wd.Documents.Open("c:\test\test.doc")

  'Move just after 'Reference:'
  Selection.HomeKey Unit:=wdStory
  With Selection.Find
    .ClearFormatting
    .Text = "Reference:"
    .Forward = True
    .Format = False
  End With
  Selection.Find.Execute
  Selection.MoveRight Unit:=wdCharacter, Count:=1
   
  'Type text at current location  wd.Selection.TypeText "This is a text"
   
  'Close document, saving it
  doc.Close SaveChanges:=True
   
  'Quit Word
  wd.Quit
   
  'Reset object variables
  Set doc = Nothing
  Set wd = Nothing
End Sub

/Ture
0
 
LVL 2

Expert Comment

by:Glen Richmond
ID: 2576265
wd.Selection.TypeText "This is a text" + str(VBRefNo)

Where VBRefNo is a variable generated by your app.
0
 
LVL 22

Accepted Solution

by:
ture earned 80 total points
ID: 2576271
Ooops... Another try:

Private Sub Command1_Click()
  'For this to work, you must set a reference to
  'the Microsoft Word 8.0 (or 9.0) Object Library
     
  'Declare object variables
  Dim wd As New Word.Application
  Dim doc As Word.Document
     
  'Open a document and set variable to refer
  'to the document
  Set doc = wd.Documents.Open("c:\test\test.doc")

  'Move just after 'Reference:'
  wd.Selection.HomeKey Unit:=wdStory
  With wd.Selection.Find
    .ClearFormatting
    .Text = "Reference:"
    .Forward = True
    .Format = False
  End With
  wd.Selection.Find.Execute
  wd.Selection.MoveRight Unit:=wdCharacter, Count:=1
   
  'Type text at current location
  wd.Selection.TypeText "This is a text"
   
  'Close document, saving it
  doc.Close SaveChanges:=True
     
  'Quit Word
  wd.Quit
     
  'Reset object variables
  Set doc = Nothing
  Set wd = Nothing
End Sub

0
 
LVL 22

Expert Comment

by:ture
ID: 2576309
Thanks, TGB! I'm glad that I could help you.

/Ture
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now