PDA

View Full Version : Solved: Automation of a document... macrobuttonfields



IkEcht
04-09-2008, 04:34 AM
Hi,

I have a document that is full of macrobutton-fields. The macro's associated with these fields generally copy data from a large excell-workbook into the documented.

Then I have a vba-loop that goes through the document clicking every field there is and acting on them.

This all works fine except for two points...
1) The index of the document is also a field of course. But I only want my vba-loop to act on the macrobutton-fields is there a way to achieve this?

2) More important. Once filled with data the fields have been replaced with text, graphs etc. I paste the data as either text or bitmap and don't want the data to be directly linked to the source. Now then when the source changes and I'm content with the changes, I want to update certain graphs and pieces of text. But as mentioned the fields are gone, so I'm back to copying and pasting by hand right now. There must be an easier way to do this, preserving the fields, or maybe setting up my document in an completely different way. Can you help me?

thanks!

fumei
04-09-2008, 10:07 AM
"I have a document that is full of macrobutton-fields."

No offense, but bleeech. I am showing my prejudice here. I am not a fan of macrobuttons.

Anyway.....hard to know what to suggest. Can you post a sample file? Some code? Particularly, what - EXACTLY - is the code behind the macrobuttons?

RBarrett
04-09-2008, 11:41 AM
Try this to act only on macro button fields:

dim f as field
For Each f in ActiveDocument.Fields
If f.Type = wdFieldMacroButton then
' your code here
End If
Next

fumei
04-10-2008, 12:23 PM
RBarrett, please use the VBA code tags.

Yes, that will work for the just-macro-button issue.

IkEcht, you are going to have to give better details. "these fields generally copy data from a large excell"

How?

Are you using INCLUDETEXT, or INCLUDEPICTURE fields? What - exactly - are you doing?

IkEcht
04-10-2008, 11:44 PM
Ok, thanks for the way to use only macrofields.

For some sample code..

Sub kaartautoroutes()


padgemeente = pad & gemeente & "\rapportage\"
Selection.InlineShapes.AddPicture FileName:= _
padgemeente & "kaarten\kaart autoroutes " & gemeente & ".jpg" _
, linktofile:=False, savewithdocument:=True
End Sub

Sub routetabel()

xl.Sheets("verpltabel").Select
xl.Range("a1:c19").Select
xl.Selection.Copy
plakken

End Sub

Sub plakken()

Selection.PasteSpecial link:=False, DataType:=wdPasteBitmap, _
Placement:=wdInLine, DisplayAsIcon:=False
End Sub

Sub vriaandeel()
xl.Sheets("data").Select
xl.Range("b17").Select
xl.Selection.Copy
Selection.PasteSpecial DataType:=wdPasteText
End Sub


And further Fumei I must agree with you that a document full of macrobuttons is far from beautifull. I got it this way from my colleague. But am willing to completely rebuild the document later on. But will have to catch up on Word/VBA (got more experience with both Access VBA and Excell VBA) before doing that, as I don't see any other solution for the current task either.

IkEcht
11-05-2008, 02:44 AM
Refreshing this thread. As I meet new problems. You gave me a good way to use only the macrobutton-fields before. It worked fine for me for quite a while, up until now. What is the news?

Well I try to use in macro-buttons in headers, footers and footnotes as well. And somehow it doesn't "click" them. Anyone who can help me with this?

RBarrett
11-05-2008, 07:06 AM
The fields collection in this case only "reaches" the main document story. You can access the fields collection of headers/footers explicitly, or you can cover all document stories as follows:

dim aStory
dim f as field
for each aStory in ActiveDocument.StoryRanges
for each f in astory.fields
' your code for fields here
next f
next aStory

IkEcht
11-05-2008, 07:16 AM
Thanks, this seems to do the trick.

Anne Troy
11-05-2008, 08:01 AM
Interesting. I'd like to offer an alternative method:
http://vbaexpress.com/kb/getarticle.php?kb_id=381

This way, there's no need to keep the resulting document...just the Excel file. :)

IkEcht
11-19-2008, 08:15 AM
After all the code below doesn't seem to do the trick. Still macrobutton-fields in headers and footers as well as footnotes won't change and perform the associated vba.

For Each astory In ActiveDocument.StoryRanges
For Each f In ActiveDocument.Fields
On Error Resume Next
If f.Type = wdFieldMacroButton Then
f.DoClick
End If
On Error GoTo 0
Next f
Next astory

Any ideas what's wrong with this code?

And Anne Troy nice ideas, it will take too much time to recode the huge document that I'm working with right now (my predecessor left me with this huge macrofield-document), but will have a look at it again with future work, might be nice to do).

RBarrett
11-19-2008, 11:37 AM
Is your document protected? If so, headers and footers are locked. (OnError Resume Next would suppress the related error message.) Try this: Unprotect the document at the top of the code, and reprotect at the end.

fumei
11-19-2008, 12:38 PM
You are still only actioning the document (MainStory) fields.
Dim astory As Range
Dim f As Field
For Each astory In ActiveDocument.StoryRanges
For Each f In astory.Fields
' NOT ActiveDocument.Fields !
On Error Resume Next
If f.Type = wdFieldMacroButton Then
f.DoClick
End If
On Error GoTo 0
Next f
Next astory



Please use the VBA code tags. Thanks.