Solved

Opening a word document from within VB6.0

Posted on 2000-03-02
7
1,135 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

696 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