Solved

Opening a word document from within VB6.0

Posted on 2000-03-02
7
1,126 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
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…

757 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

19 Experts available now in Live!

Get 1:1 Help Now