Solved

Opening a word document from within VB6.0

Posted on 2000-03-02
7
1,132 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
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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

685 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