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!

How to rename a document name in macro using MS Word 2002?

Status
Not open for further replies.

babysophia

Programmer
Jan 16, 2006
17
0
0
US

Hi Everyone! I'm just new in Macro using MS Word 2002. I already have an existing macro. I called the macro from VB 6.0. The document I was calling is to open a .dot file. When it opened, I just want to rename it as Document1 instead of the .dot file I was opened. Please help me if this is possible. Thanks.
 
Record a new macro that loads the DOT file, and then add the SAVE AS code. Then, you can open it up, and see what code you have to add when you edit the old macro. It should be simple, if that's all you need to do.

-David
2006 Microsoft Valued Professional (MVP)
 


BTW, bs, posts like this (regarding VB code for an MS Application like MS Word) would be more appropriate in VBA Visual Basic for Applications (Microsoft) Forum707

Skip,
[sub]
[glasses] [red]Be Advised![/red] For those this winter, caught in Arctic-like calamity, be consoled...
Many are cold, but few are frozen![tongue][/sub]
 

Thanks for replying. I actually have no idea in creating macro. Could you give me steps in creating Macro? This project when assigned to me already exists and I'm just modifying it. I just did some search in order for me to open the existing macro which is the .dot file. All I need to do now is to rename it to Document1 found in the upper left corner of the Word for me not to modify/save the .dot file and to save it as .doc file. =) Please let me know if this is possible. Thanks again. 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 in order for me not to save 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--->

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

 

Can you give me steps on how to create a new macro? Can you give me the syntax on how to use SAVE AS? Thanks!
 
Are you talking about VBA in Word or VB6? Your questions seem to be very ambiguous. If your questions are about VBA then go to forum707 as advised earlier, or the Office forum forum68 if you don't know how to create a 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
 
babysophia,

If you have a document template, then you should be opening a document based on that template, rather than opening the template itself. This VBA code will do that;

Code:
Documents.Add Template:= _
    "C:\Program Files\DAS\Report.dot", _
    NewTemplate:=False, DocumentType:=0

That should create your unsaved "Document1". If you want to automatically save that file, I would use dglienna's suggestion of recording a new macro and looking at / modifying the VBA code to suit your needs. To do THAT;

1. Open Word and go to Tools --> Macro --> Record new macro. Note the Macro name (or change it to something meaningful) and click OK... You're now recording everything you do.
2. Perform whatever actions you want. For example, open your template, (or better, create a new document based on your template), do your "Save As...", etc.
3. Click the "Stop Recording" button from the toolbar.
4. Finally, open the macro by going to Tools --> Macro --> Macros..., click the macro name, and click the Edit button, and there you have the VBA code behind the actions you took in step 2.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top