Posts by ZappAstrim

    Re: word document with embedded spreadsheet linkiing field problem


    Yes, that's the code I mean. I recommend that you download the example I gave and take the code from there as that is the latest (I am trying to keep that up to date)


    http://dl.dropbox.com/u/15735073/Permanent/Fruit.docx
    http://dl.dropbox.com/u/15735073/Permanent/Fruit.xlsm


    http://"http://dl.dropbox.com/u/15735073/Permanent/Fruit.xlsm"Let me know how it goes.

    Re: word document with embedded spreadsheet linkiing field problem


    I find linked files from word to excel and vice versa to be extremely unreliable. The problem isn't so bad in 2010 any more though. To get around the issue I created a vba script to automatically copy bits from my spreadsheets from named ranges into bookmarks within the word document. I'm on my mobile right now, but if you check this forum for a recent post from me you will find some code that should work. Give me a shout if you need more guidance.

    Re: Excel VBA to cycle through Word bookmarks and insert ranges (text, graphs & table


    All the charts are not linked on my end. They are all set to update manually rather than automatically. The only time they would update is if you selected all the text in the document and hit alt+F8.


    But even so, you could just change the copy and paste method that I have employed to one of the following:


    Code
    'Paste as GIF
    Selection.PasteSpecial Link:=False, DataType:=13, Placement:=wdInLine, DisplayAsIcon:=False
    
    
    'Paste as JPEG
    Selection.PasteSpecial Link:=False, DataType:=15, Placement:=wdInLine, DisplayAsIcon:=False
    
    
    'Paste as PNG
    Selection.PasteSpecial Link:=False, DataType:=14, Placement:=wdInLine, DisplayAsIcon:=False


    I haven't tested this myself, but I don't see any reason as to why this shouldn't work with the program that I provided here.

    Re: Excel VBA to cycle through Word bookmarks and insert ranges (text, graphs & table


    I found some more errors in the code above. Please see the following downloads for some more slightly better code:
    http://dl.dropbox.com/u/15735073/Permanent/Fruit.docx
    http://dl.dropbox.com/u/15735073/Permanent/Fruit.xlsm


    http://"http://dl.dropbox.com/u/15735073/Permanent/Fruit.xlsm"Edit: Just documented the hell out of the code. Same download.


    I thought I would add the pretty much final code here, as the files might be removed at some point in time.


    Re: Excel VBA to cycle through Word bookmarks and insert ranges (text, graphs & table


    Hi there Mr Plow. What issues are you having with the code? What version of Excel and Word are you running?


    I will upload an example word and excel document when I get into the office for you to play around with that works on my end. I've tested the code in Office 2007. I've tried running it on my home computer (Office 2010) but there is a small problem with pasting charts.

    Re: Excel VBA to cycle through Word bookmarks and insert ranges (text, graphs & table


    Just noticed that there is a small error in the code:
    wdApp.WordBasic.OpenOrCloseParaBelow


    needs to be:


    wdApp.WordBasic.CloseParaBelow


    And yes, it totally makes me the geek of my department. But they love me for it, as what used to take half an hour of BORING copy + paste jobs now can be done in 30 seconds. Reports can be generated really quickly with less errors :D

    Re: Excel VBA to cycle through Word bookmarks and insert ranges (text, graphs & table


    Please be prepared for some horrendously crap code:





    It's bloody horrible, but it works very well. Any questions give me a shout!

    Re: Excel VBA to cycle through Word bookmarks and insert ranges (text, graphs & table


    Hi Richard. I just wanted to let you know that I managed to do it!!! I can now copy and paste tables and text with great formatting results. All there needs to be is a bookmark in the word document and a named range in excel with the same.


    I will post the code here shortly. It's a bit of a mess at the moment and could probably really do with a good clean-up. I'll try as best I can to make the code readable and then post it here in the next couple of days.

    Re: Excel VBA to cycle through Word bookmarks and insert ranges (text, graphs & table


    Hello Richard.


    I know that Excel can grab all Named Ranges. So I have around 10 tables that need copying from Excel to word and around 10 charts that need copying too. Added to that there are probably around 10 text strings that need copying. Ideally I could do it by loading the list of bookmarks from Word:


    Code
    With wdDoc        ReDim sbkmk(1 To .Bookmarks.Count)
             
             'populates array (a list) with bookmark names
            For tmp1 = 1 To .Bookmarks.Count
                sbkmk(tmp1) = .Bookmarks(tmp1).Name
            Next


    So now I have the list of bookmarks in the word document where I would like to insert various bits of text, tables or graphs from my Excel document. If the bookmark has the same name as a named range I should be able to match these.


    I know that I can load the text in a String as the code by Mr. Plow does this. I was wondering if there is a way that I can load an excel chart into something like a string or a variable, hold it in memory and then dump it into where the word document bookmark is.


    I know what format each Bookmark is going to be in so I could create a list something like this:


    Code
    'Insert all text items
    For "ARText1", "ARText2", "ARText3" etc.
         Use Function "Insert Text"  
    'Insert all chart items
     For "ARChart1", "ARChart2", "ArChart3" etc. 
         Use Function "Insert Chart"
    'insert all tables
    For  "ARTable1", "ARTable2", "ArTable3" etc. 
         Use Function "Insert Table"


    (This is obviously not real code)


    Mr Plow uses the following Function to insert text, so we have this covered:



    The important part of this function is that it re-adds the bookmark allowing the user to update the word document at a future point in time. So the bookmark needs to cover the entire word, chart or table when updating.


    The biggest problem I have with Mr Plow's code is the following item:


    Code
    strValue = Trim(Application.Range(strName).Text)


    I am unsure of how one could modify this code to work with charts or tables, or whether that is even possible or not.

    Re: Excel VBA to cycle through Word bookmarks and insert ranges (text, graphs & table


    Hello Richard. Many thanks for your support. Actually the document I put together was just an example of what I need to be able to do.


    Your code is nice and I can understand most of it. Unfortunately it doesn't quite do what I need it to. I'm really sorry to say this as I really appreciate all your help that you have provided me so far.

    The nice thing about the code that Mr. Plow provided is that you don't need to manually provide all the bookmark names and named ranges and is very universal. The word document that I use in combination with the Excel file might not just be created once, but also updated if any changes occur in the word document. There are quite a lot of tables and charts in the report. Sometimes some of the tables might be removed as they are not required and then the corresponding bookmark would be removed too - so I can't go around updating the VBA manually as this would take more time than a manual copy paste job.


    I'm scratching my head at the moment as to what I can do to make some universal code that will work nicely with updating and also to copy charts and tables. I've bought myself Excel VBA for Dummies and have now started reading that in the hope of being able to come up with a nice solution.


    I'll definitely come back here when I have a result. If you (Richard), or anyone else, has any further ideas I am always appreciative.


    Thanks so much for all your help and support!

    Re: Excel VBA to cycle through Word bookmarks and insert ranges (text, graphs & table


    Thanks again for all your support Richard.


    I went and cycled through the code as you suggested. The issue I am having is that the tables and charts I am trying to copy are not text. So I end up with this part of the code giving me a null value:


    (Application.Range(strName).Text)


    I somehow need to be able to distinguish between text, charts and tables in order to be able to copy them into my report. Unfortunately I am still very much a beginner at coding VBA and am unsure of how this can be best achieved.


    strName in this case is the Named Range/Bookmark for the table in the report (ARTableFruit in the attached example).

    Re: Excel VBA to cycle through Word bookmarks and insert ranges (text, graphs & table


    Thanks for the extra code Richard. I still however have the issue with the current code that I am trying to run. I get stuck on this line with the code that I am using from the post that I referenced. It's fantastic code as it does the following:


    Looks for bookmarks in word document and lists them in memory. Finds ranges in excel with the same name. Automatically copies these ranges and re-inserts the bookmark. The code is SUPER fast and the formatting for text is fantastic. But I just don't understand how I can adapt it for use with tables and charts.


    This is the line I truly don't understand as to what it does and how to get around it:


    strValue = Trim(Application.Range(strName).Text)


    I keep on getting the error 94 and I am starting to go mad!

    Hello everyone - I have managed to use Mr. Plow's fantastic script for using a VBA script to cycle through Word bookmarks and insert Excel range value. I have managed to get the script to work fantastically on text only copies. However I would also like to copy and paste charts as well as tables from named ranges. I was wondering how I could achieve this with a modification of the script that is found in this thread:


    http://www.ozgrid.com/forum/showthread.php?t=148798&page=1 (post number 6)


    Currently when I try to "autocopy" from a range that is a table I get the error: "Run-time error '94': "Invalid use of Null" on the following line:
    strValue = Trim(Application.Range(strName).Text)


    I don't believe it should be too hard to modify the script, but all my attempts have failed so far. If someone could advise me at least where I could start looking that would be fantastic.

    Re: Excel VBA to cycle through Word bookmarks and insert Excel range value


    That's absolutely brilliant! Thanks so much for that.


    Is there a way to be able to modify this particular bit of VBA to be able to paste graphs and tables from the Excel document. Currently when I try to "autocopy" from a range that is a table I get the error: "Run-time error '94': "Invalid use of Null" on the following line:


    strValue = Trim(Application.Range(strName).Text)


    I guess I would need to build an if statement for the vba script to be able to determine between text and between tables/graphs. I am a bit lost at the moment as to how I could do this. Any help here would also be massively appreciated.

    Re: Excel VBA to cycle through Word bookmarks and insert Excel range value


    Hello, I know that this thread has been inactive for a long time, but this is EXACTLY what I am looking for and I unfortunately cannot get it to work. I have copied & pasted the code from the 6th post into a new VBA module in my Microsoft Excel 2007 Spreadhseet and I have ensured that I have the Microsoft Word 12 Object Library selected in the references section.


    My first issue is that when I try to run the sub PopulateWordBookmarks I can't. My Excel doesn't like the (strDoc As String) in brackets. When I remove that and just write "Dim strDoc As String" I can run the script, but I get stuck on this line:

    If RangeNameExists(strName) Then


    with the error "Compile Error: Sub or Function not defined". I am a bit of a noob to VBA, so any help that you can offer would really be appreciated. I am probably doing something really basic and really simple wrong, but I just can't figure it out. Thanks!