Results 1 to 13 of 13

Thread: [RESOLVED] Strange problem with Msword using vb code

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2003
    Posts
    233

    Resolved [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.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2003
    Posts
    233

    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:
    1. Option Explicit
    2. Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    3. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    4. Private Const ES_UPPERCASE = &H8&
    5. Private Const GWL_STYLE = (-16)
    6. Private Declare Function SHCreateDirectoryEx Lib "shell32" Alias "SHCreateDirectoryExA" _
    7.     (ByVal hWnd As Long, ByVal pszPath As String, ByVal psa As Any) As Long
    8. 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.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2003
    Posts
    233

    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

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Nov 2003
    Posts
    233

    Re: Strange problem with Msword using vb code

    Thank's si_the_geek

    Here is the Full code

    VB Code:
    1. On Error Resume Next
    2. Dim appWord As Word.Application
    3. Dim objDoc As Word.Document
    4. Dim FileName        As String
    5. Dim TempFileName    As String
    6. Dim i As Integer
    7.  
    8.     Dim oFooter As Range
    9.     Dim oRange As Range
    10.     Dim oEntry As AutoTextEntry
    11.    
    12. 'Dim MkDir As String
    13. 'Dim appWord              As Object
    14.  
    15.  
    16. Dim BoolFlag As Boolean
    17. BoolFlag = False
    18.  
    19. ' Dim appWord As Object
    20.    
    21.     SHCreateDirectoryEx Me.hWnd, "c:\Documents and Settings\All Users\Desktop\NEW FOLDER\", ByVal 0&
    22.  
    23.     Set appWord = CreateObject("Word.Application")
    24.     appWord.Visible = False
    25.    Set objDoc = appWord.Documents.Add
    26.    
    27.     If Dir("c:\Documents and Settings\All Users\Desktop\NEW FOLDER\", vbDirectory) = "" Then
    28.         MkDir "c:\Documents and Settings\All Users\Desktop\NEW FOLDER\"
    29.     End If
    30.    
    31.    
    32.     FileName = "c:\Documents and Settings\All Users\Desktop\NEW FOLDER\NEW FOLDER" & "-" & _
    33.     Format(Now, "DD-MM-YY") & "-" & "NEW0"
    34.    
    35.     TempFileName = FileName & ".doc"
    36.    
    37.     For i = 1 To 100 ' 100 tries (just to avoid using a While Loop)
    38.         If Dir(TempFileName) <> vbNullString Then
    39.             TempFileName = FileName & CStr(i) & ".doc"
    40.         Else
    41.             Exit For
    42.         End If
    43.     Next
    44.    
    45.     objDoc.SaveAs
    46.  
    47. appWord.Selection.Font.Name = "Times New Roman"
    48. appWord.ActiveDocument.PageSetup.Orientation = wdOrientPortrait
    49. appWord.ActiveDocument.PageSetup.TopMargin = 30
    50. appWord.ActiveDocument.PageSetup.BottomMargin = 10
    51. appWord.ActiveDocument.PageSetup.LeftMargin = 50
    52. appWord.ActiveDocument.PageSetup.RightMargin = 50
    53.  
    54.     Set objDoc = appWord.ActiveDocument
    55.  
    56.     appWord.ActiveDocument.SaveAs FileName:=TempFileName, FileFormat:= _
    57.     wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:= _
    58.     True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
    59.     False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
    60.     SaveAsAOCELetter:=False
    61.    
    62.     appWord.Documents.Close
    63.     appWord.Application.Quit
    64.     Set appWord = Nothing
    65.     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.

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Nov 2003
    Posts
    233

    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.

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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?

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Nov 2003
    Posts
    233

    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:
    1. Set appWord = CreateObject("Word.Application")

    In References

    Microsoft Word 9.0 object library
    Last edited by jamal464; Feb 11th, 2008 at 12:37 PM.

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Nov 2003
    Posts
    233

    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
  •  



Click Here to Expand Forum to Full Width