Solved

Opening a word document from within VB6.0

Posted on 2000-03-02
7
1,130 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Validating VB6 Function 19 59
How to debug this code 7 61
Access query that references subform 5 47
TT Auto Dashboard 13 92
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

803 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