|
-
Feb 10th, 2008, 04:58 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Strange problem with Msword using vb code
Hi
I have office2000 installed with vb6 on my computer
The problem is in my prject code when i type
Dim appWord As Word.Application
It will be like that
Dim appWord As WORD.Application
The word automaticlly become capital
Also when itype this
Dim objDoc As Word.Document
It will come
Dim objDoc As WORD.Document
---------------------------------------------------
Code:
Dim appWord As WORD.Application
Dim objDoc As WORD.Document
Dim FileName As String
Dim TempFileName As String
Dim i As Integer
Dim oFooter As Range
Dim oRange As Range
Dim oEntry As AutoTextEntry
Dim BoolFlag As Boolean
BoolFlag = False
SHCreateDirectoryEx Me.hWnd, "c:\Documents and Settings\All Users\Desktop\NEW FOLDER\", ByVal 0&
Set appWord = CreateObject("Word.Application")
appWord.Visible = False
appWord.Documents.Add
'----------------------------
objDoc.SAVE ' Here also the word Save is Capital
'----------------------------
appWord.Documents.Close
appWord.Application.Quit
Set appWord = Nothing
Set objDoc = Nothing
I'm trying to creat folder on the desktop then create a word document inside the folder
But when i go to the folder there is no word document
Last edited by jamal464; Feb 10th, 2008 at 05:02 AM.
-
Feb 10th, 2008, 05:34 AM
#2
Re: Strange problem with Msword using vb code
as you are using late binding to using createobject you do not need a reference to word and appword and objdoc should be declared as objects
objdoc.save will save to the default folder for excel, with the default file name, also have you set a value to objdoc as i don't see it, though i am sure there is much more code
use objdoc.saveas then define the path and filenames as required
also you might need to check if the folder already exists before creating
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Feb 10th, 2008, 10:09 AM
#3
Thread Starter
Addicted Member
Re: Strange problem with Msword using vb code
Thank's westconn1
As i'm typing now with my pc where there is no office installed only vb6 the same problem happend i try to change the WORD.Application to Word.Application after i finshe it retun to WORD.Application
Yes there is a lot of code's in my project
here is
VB Code:
Option Explicit
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const ES_UPPERCASE = &H8&
Private Const GWL_STYLE = (-16)
Private Declare Function SHCreateDirectoryEx Lib "shell32" Alias "SHCreateDirectoryExA" _
(ByVal hWnd As Long, ByVal pszPath As String, ByVal psa As Any) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
I'm also using amodules to save the data to ini files
clsINI
clsSection
If i start bew project and write the code Word.Application then there is no problem
Thank's
Last edited by jamal464; Feb 10th, 2008 at 10:42 AM.
-
Feb 10th, 2008, 01:28 PM
#4
Re: Strange problem with Msword using vb code
It sounds to me as if you may have created a variable/sub/function which is called WORD, and the IDE (understandably) is getting confused by that.
-
Feb 10th, 2008, 05:28 PM
#5
Thread Starter
Addicted Member
Re: Strange problem with Msword using vb code
Thank's si_the_geek
You are right i just rename all Commanbutton who got the same name(WORD, SAVE And LOAD) and it's ok
Dim appWord As Word.Application ' ok
Dim objDoc As Word.Document ' ok
The problem was in the modules
clsINI
clsSection
The problem now is when i press the commandbutton to creat the folder who containe the word document the folder is empty
In the Windows Task Manager the Msword still running ((WINWORD.EXE))
I make new project and rename the modules but still the same proble
I run the Regedit and search for any key for clsINI and clsSection and delete thim but still there is no document in the folder
Thank's
-
Feb 10th, 2008, 05:40 PM
#6
Re: Strange problem with Msword using vb code
The reason Word hasn't closed is that you have made mistake(s) in your code. In theory you should get errors, but for some reason it doesn't work like that (instead you create duplicate objects without realising).
One problem, which westconn1 pointed out earlier, is that you don't ever set up objDoc, you just seem to assume that VB will somehow guess what it is supposed to be.
Instead of this:
Code:
appWord.Documents.Add
you should have this:
Code:
Set objDoc = appWord.Documents.Add
The reason that the folder is empty is something else that westconn1 mentioned - you aren't telling Word to save to that folder (and you also aren't telling it what to call the file!). Rather than using .Save , use .SaveAs and specify a path & name for the file.
-
Feb 10th, 2008, 06:02 PM
#7
Thread Starter
Addicted Member
Re: Strange problem with Msword using vb code
Thank's si_the_geek
Here is the Full code
VB Code:
On Error Resume Next
Dim appWord As Word.Application
Dim objDoc As Word.Document
Dim FileName As String
Dim TempFileName As String
Dim i As Integer
Dim oFooter As Range
Dim oRange As Range
Dim oEntry As AutoTextEntry
'Dim MkDir As String
'Dim appWord As Object
Dim BoolFlag As Boolean
BoolFlag = False
' Dim appWord As Object
SHCreateDirectoryEx Me.hWnd, "c:\Documents and Settings\All Users\Desktop\NEW FOLDER\", ByVal 0&
Set appWord = CreateObject("Word.Application")
appWord.Visible = False
Set objDoc = appWord.Documents.Add
If Dir("c:\Documents and Settings\All Users\Desktop\NEW FOLDER\", vbDirectory) = "" Then
MkDir "c:\Documents and Settings\All Users\Desktop\NEW FOLDER\"
End If
FileName = "c:\Documents and Settings\All Users\Desktop\NEW FOLDER\NEW FOLDER" & "-" & _
Format(Now, "DD-MM-YY") & "-" & "NEW0"
TempFileName = FileName & ".doc"
For i = 1 To 100 ' 100 tries (just to avoid using a While Loop)
If Dir(TempFileName) <> vbNullString Then
TempFileName = FileName & CStr(i) & ".doc"
Else
Exit For
End If
Next
objDoc.SaveAs
appWord.Selection.Font.Name = "Times New Roman"
appWord.ActiveDocument.PageSetup.Orientation = wdOrientPortrait
appWord.ActiveDocument.PageSetup.TopMargin = 30
appWord.ActiveDocument.PageSetup.BottomMargin = 10
appWord.ActiveDocument.PageSetup.LeftMargin = 50
appWord.ActiveDocument.PageSetup.RightMargin = 50
Set objDoc = appWord.ActiveDocument
appWord.ActiveDocument.SaveAs FileName:=TempFileName, FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
appWord.Documents.Close
appWord.Application.Quit
Set appWord = Nothing
Set objDoc = Nothing
I made the changes but still the same problem
Thank's
Last edited by jamal464; Feb 10th, 2008 at 06:06 PM.
-
Feb 10th, 2008, 06:34 PM
#8
Re: Strange problem with Msword using vb code
Think about line 45.. where is it saving the file to, and what is it calling that file? (you don't need that line, as you save properly later on, after making changes).
Note also that you should not be using "appWord.ActiveDocument", instead use "objDoc". (from this, you should realise that line 54 is not needed)
Instead of using "appWord.Documents.Close", you should simply have "objDoc.Close".
By the way, VB wants to tell you where you have made mistakes, and try to tell you what needs to be changed.. but you've decided to completely ignore it, and instead just hope your code works (and when it doesn't, you have no idea why that is).
I'd recommend reading the article What is wrong with using "On Error Resume Next"? from our Classic VB FAQs (in the FAQ forum, which is shown near the top of our home page)
This is definitely a situation where you should not be using it at all, even when the code is finished and tested.
-
Feb 11th, 2008, 04:32 AM
#9
Thread Starter
Addicted Member
Re: Strange problem with Msword using vb code
Thank's si_the_geek
I made the changes and i got run time errore 428 .dll not found i think i have to reinstall office2000
I test the code on other pc with office2007 and it's ok
Thank's
Last edited by jamal464; Feb 11th, 2008 at 12:38 PM.
-
Feb 11th, 2008, 06:16 AM
#10
Re: [RESOLVED] Strange problem with Msword using vb code
Was a line of code highlighted when you got the error? (if so, which)
In your References, what version of Word is listed?
-
Feb 11th, 2008, 12:33 PM
#11
Thread Starter
Addicted Member
Re: [RESOLVED] Strange problem with Msword using vb code
Thank's si_the_geek
The error is 48 not 248
This line of code is highlighted
VB Code:
Set appWord = CreateObject("Word.Application")
In References
Microsoft Word 9.0 object library
Last edited by jamal464; Feb 11th, 2008 at 12:37 PM.
-
Feb 11th, 2008, 02:51 PM
#12
Re: [RESOLVED] Strange problem with Msword using vb code
take out the reference to word, as you do not need it with createobject
but you would need to change the declare to
dim appword as object
does word run if you start it manually?
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Feb 11th, 2008, 06:21 PM
#13
Thread Starter
Addicted Member
Re: [RESOLVED] Strange problem with Msword using vb code
Thank's westconn1
Yes word run normal of it is manuall
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|