Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing value from VB6.0 to Marco in Microsoft Word 2002 1

Status
Not open for further replies.

babysophia

Programmer
Jan 16, 2006
17
0
0
US

Hi Everyone,

I'm just new in macro using Microsoft Word 2002. I just want to know if it's possible to pass a value coming from VB6.0 to macro using Microsoft Word 2002? Please help me. Thanks.
 
If you are controlling Word directly from VB using the Word object library then simply translate the macro functionality into straight VB and ignore the macro.

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 

Hi guys! Thanks for replying. I already solved my problem in passing value from VB to Macro. My another problem now is to rename the document as Document1 when I open the .dot file. Here's my code below in VB and Macro:

---VB 6.0---
I've placed this code in one of the modules in VB:
Function OpenReport()
Dim wrdApp
Set wrdApp = New Word.Application
wrdApp.Visible = True
wrdApp.Documents.Open "C:\Program Files\DAS\Report.dot"
wrdApp.ActiveDocument.ShowText (strExportFilename) '-----> This is just a path for the text file I need to open to pass it in Macro
Set wrdApp = Nothing
End Function

---Macro in MS Word 2002---
VB called this function to pass the location of the text I need to open from VB. I have placed this code in ThisDocument under TemplateProject:
Public Sub ShowText(strPath As String)
Call Main(strPath)
End Sub

This is the function called in the ShowText above. This function will just pass the value(path name) from the ShowText "strPath" to open the text file I've created in my local. The values found in the text file will populate all the values in each column/row in the word. The word will display the .dot file name in the upper left corner of the Word. All I need to do now in order for me not to save/modify the .dot file. I just need to rename it. Is this possible? I have placed this code in modules:
Sub Main(strPath)
Dim sFileName As String
...
...
Const FUNCTION_NAME = "Main()"

On Error GoTo Error_Handler

sFileName = strPath

If sFileName = "" Or Right(sFileName, 1) = "\" Then
'User did not select any file!!!
Exit Sub
End If

'Open the file
nFileNum = FreeFile
Open sFileName For Input As #nFileNum
...
...
...
...
'--->Maybe I can place the renaming of .dot file to .doc file here. But how? I still can't find the syntax in web or in any forum. =( --->

If Not bSuccess Then
MsgBox "The Report was not created successfully.", vbCritical
Else
MsgBox "The Fumiguide Report was created successfully.", vbInformation
End If

'Go to the first field
ActiveDocument.Bookmarks("BeginCursor").Select

Exit Sub

Error_Handler:
sErrorString = FUNCTION_NAME & vbCrLf & vbCrLf & "Error: " & Err.Number & " - " & Err.Description
MsgBox sErrorString, vbCritical, "Error"

End Sub
 
hi..
well i had to alter your VB code to run the macro...
i changed it and tested it...here it is

Function OpenReport()

Dim wrdApp As Word.Application

Set wrdApp = New Word.Application

With wrdApp
.Visible = True
.Documents.open "d:\abc.dot"

' this line runs the macro and passes the path
.Run "MyMacro", "d:\done.doc"

End With

Set wrdApp = Nothing

End Function
================================================

And here is the macro...

Sub myMacro(myFile As String)
'
' myMacro Macro

Word.ActiveDocument.SaveAs myFile

End Sub

this worked..
cheers
-tryp
 

hi tryp,

it's working now. thanks for replying. =)
i just inserted the saveas command in the function below: i'm just wondering when i click on the file-->save as, the value of the save as type is Document Template (*.dot) and the default file name is Document.dot. i tried using the command (Word.ActiveDocument.SaveAs FileName:="Document1.doc", FileFormat:=wdFormatDocument) in the code but it's not working. is this the right command?

Sub Main(strPath)
Dim sFileName As String
...
...
Const FUNCTION_NAME = "Main()"

On Error GoTo Error_Handler

sFileName = strPath

If sFileName = "" Or Right(sFileName, 1) = "\" Then
'User did not select any file!!!
Exit Sub
End If

'Open the file
nFileNum = FreeFile
Open sFileName For Input As #nFileNum
...
...
...
...
'--->Maybe I can place the renaming of .dot file to .doc file here. But how? I still can't find the syntax in web or in any forum. =(

Word.ActiveDocument.SaveAs "Document1.doc"
'--->

If Not bSuccess Then
MsgBox "The Report was not created successfully.", vbCritical
Else
MsgBox "The Fumiguide Report was created successfully.", vbInformation
End If

'Go to the first field
ActiveDocument.Bookmarks("BeginCursor").Select

Exit Sub

Error_Handler:
sErrorString = FUNCTION_NAME & vbCrLf & vbCrLf & "Error: " & Err.Number & " - " & Err.Description
MsgBox sErrorString, vbCritical, "Error"

End Sub

 
So are you saying you want the user to hit the "Save As..." menu item?
not sure exactly what you mean...

-tryp
 
yes, user can also click on the save as under file menu but when the user click it, the save as type should be Word Document (*.doc) instead of Document Template (*.dot) and the default file name is Document1.doc. thanks! =)
 

does anyone knows my problem? i'm almost done with my project. the only problem i have now is to make the SAVE AS TYPE as Word Document(*.doc) instead of Document Template(*.dot) and the default file name should be Document1.doc instead of Document1.dot. please help me. thanks. =)
 
Just record a macro and edit the code produced

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
>does anyone knows my problem?

Yes. The easiest solution to this is to open your template in a more correct fashion for this requirement.

Instead of this:
Code:
wrdApp.Documents.Open "C:\Program Files\DAS\Report.dot"
do this:
Code:
wrdApp.Documents.Add "C:\Program Files\DAS\Report.dot", False
Now, when they click on Save As (or Save) they will be offered a .doc extension
 

thanks for replying guys! i added it in my code
(wrdApp.Documents.Open "C:\Program Files\DAS\Report.dot", False) but it is still the same when i clicked on the file>save as. =(
 
You could always TRY what was suggested!

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
... particularly as I now note that the same solution to much the same question from the same poster in thread222-1180133 seems to have been ignored ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top