+ Reply to Thread
Results 1 to 24 of 24

Problems running a word macrofile from within excel!

  1. #1
    Registered User
    Join Date
    01-09-2013
    Location
    Helsingborg, Sweden
    MS-Off Ver
    Excel 2010
    Posts
    47

    Problems running a word macrofile from within excel!

    Hi there!

    Ill do my best to try to explain the process im struggling with. feel free to ask followupp Qs.

    I have A excel workbook with data that i work in. to this i have a button on first sheet that has a macro connected that i cant get to work.
    The Macro should rund a wordmacro file.

    The word file works as follows. When i run the word file "Intern syn NCC_Hornbach.dotm" in the folder. ( by dubbelklicking it) it runs, then gets a standard template from another file called "Intern_syn_NCC.dotm" och makes a new document with the standard template as page one. then it looks in an excel file wich has say 10 rows with data. and populates the first page with row one. then gets the template again and makes page 2, populates it with data from row 2 in the excel fil and so on untill page 10.

    then the word file "Intern syn NCC_Hornbach.dotm" closes leavin a new document with no title open with 10 templatepages populated with data from the 10 rows it got from the excel datafile.

    My problem is that when i run the macro. i get runtime error and can klick terminate. allthough the macro opens the wordmacro file.
    but doesnt runt it. so it remains open with 1 page.

    the code i use is as follows

    Sub GetWordDoc()

    Dim myDocPath As Selection
    'Opens a Word Document from Excel

    Dim objWord As Object

    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True

    'Change the directory path and file name to the location
    'of your document

    Set objDoc.Selection = objWord.Documents.Open(Filename:=ThisWorkbook.Path & "\Intern syn NCC_Hornbach.dotm")

    End Sub

    how do i solve this so the macro just runs the wordfile, and doesnt open in? so it will work as it does when i dubble klick it in the folder?

    Many thnx in advance and i hope its not to confusing!

    cheers

  2. #2
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Problems running a word macrofile from within excel!

    Try

    Please Login or Register  to view this content.
    If you like my contribution click the star icon!

  3. #3
    Registered User
    Join Date
    01-09-2013
    Location
    Helsingborg, Sweden
    MS-Off Ver
    Excel 2010
    Posts
    47

    Re: Problems running a word macrofile from within excel!

    Thanks for the reply OllieB!

    Now i dont get any error msg att all, so macro runs without fault. but it still opens the wordfile "Intern syn NCC_Hornbach.dotm" and leavs it open with no further action.

    When i run the wordfile alone from folder, it starts, runs its macro and closes. and leavs a new document unsaved with all the templates populated from another excel file. Thats what i want the excel macro to do aswell.

    Many thanks!

  4. #4
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Problems running a word macrofile from within excel!

    That is because you are opening the template itself. What you want to do (based on your feedback in the last post), is create a NEW document based on the specified template. You should thus be using the new method as opposed to open

  5. #5
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Problems running a word macrofile from within excel!

    Try using the following code

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    01-09-2013
    Location
    Helsingborg, Sweden
    MS-Off Ver
    Excel 2010
    Posts
    47

    Re: Problems running a word macrofile from within excel!

    Hi there again!

    Thanks for your time and help m8!

    Im attaching the wirdfile im trying to run via excel macro, and a excel datafile the word file uses, easier if you se how it works, maybe then its easier to know why atm it wont run as intended from excel macro!

    Hope it helps!

    if u extract and keep bot files together and dubbleklick the word macrofile u can se what it does. in it there are 3 macros that runs upon running the word file. "KopplaData", "Makro1" and "Document_New"

    Cheers!
    Attached Files Attached Files

  7. #7
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Problems running a word macrofile from within excel!

    Dear Lavinus,

    I have tested the solution I have provided to you in post #5 with the copies of the Word template and workbook provided, and the Word makro1 gets executed as desired!

    So the code below should work without a problem.

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    01-09-2013
    Location
    Helsingborg, Sweden
    MS-Off Ver
    Excel 2010
    Posts
    47

    Re: Problems running a word macrofile from within excel!

    Hi!

    Very strange that it works for u, the button and macro assigned doesnt work for me. it runs the wordfile, and the macro1 in i. but it cant find the NCCExport file to import data.
    I get an error when it opens the word file, and cant find the file. it for some strange reason starts looking in sysmtem32 folder for it. and says it cant find the local file NCCExport.

    As when u run just the wordfile itself by dubbleklicking it it has no problem locating the NCCExport in the same folder and running the macro.

    Im very confused :S

    Thanks for the help!
    Attached Files Attached Files

  9. #9
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Problems running a word macrofile from within excel!

    Ah,

    but that is a different problem then the one I was looking at. We started with opening the template document itself, and not creating a blank document based on the template and running the macro. We got that fixed now, the second problem is that it cannot find the excel object when running the macro. Like I said that is a different problem altogether, as it is a problem in the WORD macro.

    To solve that problem I would suggest to add a CHDIR statement to your code in Excel.


    Please Login or Register  to view this content.
    This will force your current directory to become the directory where the Excel workbook running this code is located. When you make sure that your Word template and Excel wrokbook containing the data are also in this directory, it should all work.

  10. #10
    Registered User
    Join Date
    01-09-2013
    Location
    Helsingborg, Sweden
    MS-Off Ver
    Excel 2010
    Posts
    47

    Re: Problems running a word macrofile from within excel!

    Then im verry sorry for not being clear on the problem at hand, And you should know that i appriciate your time and effort helping me very much! Cant thank you enough!

    Allthough it still doesnt work dispite adding that line of code and keeping all 3 files in same folder. the word file is not to be opened, if u just dubblecklick it it works fine and crates the unsaved document as wanted. thats what i want the macro to do, to run the word file, not open it.

    I still get the same error, that it runs word file but sticks as it cant find the correcr files, wich for me is strange because when i dubblecklick just the wordfile it has no problem executing its onw macros and crating the new doc. So the macros in the wordfile works fine when u double klick the word file and run it. but when i run it via excel macro it doesnt work.

    Cheers!

  11. #11
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Problems running a word macrofile from within excel!

    Lavinius,

    The problem is that the "current directory" gets changed by opening the template document to base the new document on. As a result it cannot find the Excel workbook when running the Word macro, and the mail merge fails.

    The easiest solution is to stored the Excel workbook in a fixed path and change the MailMerge statement in the template, or run the mail merge from within the Excel workbook that you want to control the process from, in which case you can use the path of the Excel workbook.

    I have moved all the necessary coding into an Excel workbook (named MASTER) and have removed all coding from the template. When I test it, it works. have a go

    I have attached the workbook, but cannot attach the template. Just create a copy and remove all coding from the template
    Attached Files Attached Files

  12. #12
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Problems running a word macrofile from within excel!

    Just to be sure I have attached the zip file with the changed objects. I am eager to hear whether it works. I have conducted multiple tests, all OK
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    01-09-2013
    Location
    Helsingborg, Sweden
    MS-Off Ver
    Excel 2010
    Posts
    47

    Re: Problems running a word macrofile from within excel!

    Hi!

    I get runtime error 9105, string longer than 225 in row 39 atm, trying to figure out how to break the string

  14. #14
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Problems running a word macrofile from within excel!

    I read about this problem on the Internet. The string cannot be longer than 255 characters. This is a Word limitation/problem that I cannot solve. Maybe (for testing purposes) put the objects in a shorter path like for example C:\TEST?

  15. #15
    Registered User
    Join Date
    01-09-2013
    Location
    Helsingborg, Sweden
    MS-Off Ver
    Excel 2010
    Posts
    47

    Re: Problems running a word macrofile from within excel!

    Ok Now!

    It works as intended!

    But it crates to word document for some strange reason. maybe u know why? it makes one called "Document 1" that i dont need and want. with only 2 pages. ant then it crates the one i want called "Standardbrev1" or "standardletter1" i have swedish office . with the correct data in it. and with all the pages matching the datarows in NCCExport

    Many thanks m8! You are the best!!
    Moving it to a shorter path worked! i have all the files in folder "Test" on C:

    Cheers

  16. #16
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Problems running a word macrofile from within excel!

    Lavinius,

    the Document1 is created because we create a new document based on the template. In that document we set the mailmerge parameters and run the merge, which creates the Standardbrev1 that we are interested in. Add the following code to the bottom of the macro to automatically close/remove the Document1

    Current statement

    Please Login or Register  to view this content.
    replace by

    Please Login or Register  to view this content.
    and that should take care of that problem...

  17. #17
    Registered User
    Join Date
    01-09-2013
    Location
    Helsingborg, Sweden
    MS-Off Ver
    Excel 2010
    Posts
    47

    Re: Problems running a word macrofile from within excel!

    Hi!

    Thank you verry much! Your effort in helping me just made my day! Works perfect now!

    Cheers m8!

  18. #18
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Problems running a word macrofile from within excel!

    You are very welcome. I always enjoy messing around with the interoperability of Excel, Word and Powerpoint.

  19. #19
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Problems running a word macrofile from within excel!

    Please remember to mark the thread as solved, and to click the star icon to add to my reputation if you are happy with my contribution!

  20. #20
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Problems running a word macrofile from within excel!

    Dear Lavinius,

    As Word limits the connection string to 255 characters we need to reduce the size of the connection string. I have played around a bit and found that the Data Source parameter is not needed in the Connection parameter as the data source is already mentioned in the name part. Hence you can replace the original .OpenDataSource statement with the following statement and it will still work even if you moved the objects to a location with a longer path

    Please Login or Register  to view this content.

  21. #21
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Problems running a word macrofile from within excel!

    And this one is without a connection string altogether

    Please Login or Register  to view this content.

  22. #22
    Registered User
    Join Date
    01-09-2013
    Location
    Helsingborg, Sweden
    MS-Off Ver
    Excel 2010
    Posts
    47

    Re: Problems running a word macrofile from within excel!

    Hi OllieB!

    And once again thank you! i replaced the current code with the sorter one you posted. works like a charm in a path that was to long before!

    Many thanks!

    Cheers!

  23. #23
    Registered User
    Join Date
    01-09-2013
    Location
    Helsingborg, Sweden
    MS-Off Ver
    Excel 2010
    Posts
    47

    Re: Problems running a word macrofile from within excel!

    Just wondering. whats the big diff from the 2 code strings you posted?
    I used the shorter string, but its no difference in the by how the document is crated and the data from NCCExport is transfered?

    Cheers

  24. #24
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Problems running a word macrofile from within excel!

    As far as I know there is no difference, I just came across the shorter notation after my first post.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1