|
-
Jun 20th, 2012, 04:39 AM
#1
Thread Starter
Junior Member
Popup in office (Macro)
Hello. Im new here and a newbie on VB.
I got like 100+ documents (word 2007 and excel 2007) and the main thing will be, to have the opportunity to choose a footer when you open a doc from a popup. im this case, three different offices.
This is my code:
Private Sub Document_New()
Call Document_Open
End Sub
Private Sub Document_Open()
Application.ScreenUpdating = False
Dim Rng As Range, Str As String, Fld As Field, i As Long
i = CLng(InputBox("Which Office?" & vbCr & "1: Gothenburg, 2: Stockholm, 3: Malmö"))
If i = 0 Or i > 3 Then Exit Sub
With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary)
Set Rng = .Range.Characters.First
Rng.Collapse wdCollapseStart
For Each Fld In .Range.Fields
With Fld
If .Type = wdFieldQuote Then
Set Rng = Fld.Result
.Delete
Exit For
End If
End With
Next
Select Case i
Case 1
Str = "Gothenburg AB, Tel 010-123456, www.gothenburg.com"
Case 2
Str = "Stockholm AB, Tel 010-123456, www.stockholm.com"
Case 3
Str = "Malmö AB, Tel 010-123456,www.malmö.com"
End Select
Set Fld = ActiveDocument.Fields.Add(Range:=Rng, Type:=wdFieldQuote, _
Text:="""" & Str & """", PreserveFormatting:=False)
End With
Set Fld = Nothing: Set Rng = Nothing
Application.ScreenUpdating = True
End Sub
As it it now, the script change my footer on page 2 and forward. Is it possibly to change the first page as well?
Could you have different footer on different pages at the same time? In a script?
/KenZu
Last edited by KenZu; Jun 20th, 2012 at 05:01 AM.
-
Jun 20th, 2012, 06:38 AM
#2
Re: Popup in office (Macro)
with word you can have 3 different footers in a document
first page, odd pages and even pages
you can change all in your script by adding the same code for each footer as required
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
-
Jun 20th, 2012, 06:54 AM
#3
Thread Starter
Junior Member
Re: Popup in office (Macro)
Ok. Sorry to ask, but how will it look if i want one foot as first page and one for the rest?
And one thing else... When I tried it on the intranet, it didnt work. The popup showed, but then something got wrong.
"Wrong nr '4248'... This command is not available because no document is open. "
And this code were marked yellow: With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary)
Last edited by KenZu; Jun 20th, 2012 at 08:40 AM.
-
Jun 20th, 2012, 04:25 PM
#4
Re: Popup in office (Macro)
And this code were marked yellow: With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary)
avoid using activedocument, specify the document you want to work with, i do not see where you open a document to work with
but how will it look if i want one foot as first page and one for the rest?
vb Code:
.Footers(wdHeaderFooterFirstPage)
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
-
Jun 21st, 2012, 01:15 AM
#5
Thread Starter
Junior Member
Re: Popup in office (Macro)
What if I change to the specific document. How will the new code look like? (exampel)
-
Jun 21st, 2012, 07:28 AM
#6
Re: Popup in office (Macro)
you can use documents("documentname.doc"), this guarantees you are working with the document you intended
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
-
Jun 21st, 2012, 08:41 AM
#7
Thread Starter
Junior Member
Re: Popup in office (Macro)
So that row will look like this?
With Documents("Brevmall utan rubrik_110101.doc").Sections(1).Footers(wdHeaderFooterFirstPage)
instead of:
With ActiveDocument.Sections(1).Footers(wdHeaderFooterFirstPage)
-
Jul 2nd, 2012, 12:52 AM
#8
Thread Starter
Junior Member
Re: Popup in office (Macro)
the first one didnt work.
or how will the new code look like? Should I add more codes?
Im very glad for all help I get! =)
-
Jul 2nd, 2012, 05:27 AM
#9
Re: Popup in office (Macro)
the first one didnt work.
i do not know why, it certainly looks correct
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
-
Jul 2nd, 2012, 06:17 AM
#10
Thread Starter
Junior Member
Re: Popup in office (Macro)
 Originally Posted by westconn1
i do not know why, it certainly looks correct
Hmm.
I get the same error, "Wrong nr '4248'... This command is not available because no document is open. "
It doesnt work when I'm open the file from the intranet, but if I save the file first and then opends it work.
-
Jul 2nd, 2012, 07:43 AM
#11
Re: Popup in office (Macro)
It doesnt work when I'm open the file from the intranet, but if I save the file first and then opends it work
.
that makes sense, the document name may be some temp file
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
-
Jul 2nd, 2012, 09:03 AM
#12
Thread Starter
Junior Member
Re: Popup in office (Macro)
 Originally Posted by westconn1
.
that makes sense, the document name may be some temp file
Do you think I could go around the problem?
-
Jul 2nd, 2012, 04:43 PM
#13
Re: Popup in office (Macro)
Do you think I could go around the problem?
while i very much refer not to use active anything, this maybe a case to do so
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
-
Jul 3rd, 2012, 12:55 AM
#14
Thread Starter
Junior Member
Re: Popup in office (Macro)
-
Aug 7th, 2012, 02:03 AM
#15
Thread Starter
Junior Member
Re: Popup in office (Macro)
Hmm..
What if we only want the possibility to save the document and not open. What would you change in the code?
I'm really thankful for all the help!
-
Aug 14th, 2012, 07:19 AM
#16
Thread Starter
Junior Member
Re: Popup in office (Macro)
I guess there's something wrong with my settings in IE8 or word.
I tried to open the file in FF and it work perfect.
Now i just have to search in IE8 & word after my problem.
What if we only want the possibility to save the document and not open. What would you change in the code?
Last edited by KenZu; Aug 14th, 2012 at 08:44 AM.
-
Aug 14th, 2012, 04:53 PM
#17
Re: Popup in office (Macro)
What if we only want the possibility to save the document and not open.
you have to open to make changes
but you can close and save
at the end add thisdocument.close true
ideally you should have all the code in some other document, open each document in folder, make appropriate changes, close and save, open next
not have the code in the open event for every document
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
-
Aug 23rd, 2012, 02:41 AM
#18
Thread Starter
Junior Member
Re: Popup in office (Macro)
Hmm,didnt work 
It works in FF but not in IE. Do you think the setting problem is in word or the browser?
-
Aug 25th, 2012, 02:15 AM
#19
New Member
Re: Popup in office (Macro)
Every time I open the file I am presented with a securuty pop up that tells me .
-
Oct 4th, 2012, 08:02 AM
#20
Thread Starter
Junior Member
Re: Popup in office (Macro)
Lets open this again...
I got it to work... But when I tried to copy code to other documents it won't work.
I get the popup to work, but it doesnt change office.
Guess im back to basic... How/what do I have to write in the foot, so that the script knows what to change?
-
Oct 4th, 2012, 04:30 PM
#21
Re: Popup in office (Macro)
ok, what code have you got now?
I got it to work... But when I tried to copy code to other documents it won't work.
why do yo want to copy it to other documents?, see post #17
I get the popup to work, but it doesnt change office.
what office do you want to change? i thought it was just to edit some documents
How/what do I have to write in the foot, so that the script knows what to change?
like the script i am not a mind reader, you need to explain more, what you want to do
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
-
Oct 5th, 2012, 12:27 AM
#22
Thread Starter
Junior Member
Re: Popup in office (Macro)
Let's try again. My code below.
It worked fine in document 1. But when I copied the code to doc. 2, the foot dont change. I got the popup to work, but after i choose office (from the popup) the foot doesnt change.
Let's say I start a new document. Is it only to copy the code and paste it in "ThisDocument" or should I write something In the document (foot) before?
Private Sub Document_New()
Call Document_Close
End Sub
Private Sub Document_Open()
Application.ScreenUpdating = False
Dim Rng As Range, Str As String, Fld As Field, i As Long
i = CLng(InputBox("Välj kontor" & vbCr & "1: Samtliga, 2: Göteborg, 3: Stockholm"))
If i = 0 Or i > 3 Then Exit Sub
With ActiveDocument.Sections(1).Footers(wdHeaderFooterFirstPage)
Set Rng = .Range.Characters.First
Rng.Collapse wdCollapseStart
For Each Fld In .Range.Fields
With Fld
If .Type = wdFieldQuote Then
Set Rng = Fld.Result
.Delete
Exit For
End If
End With
Next
Select Case i
Case 1
Str = "Göteborg"
Case 2
Str = "Sthlm"
Case 3
Str = "Stockholm"
End Select
Set Fld = ActiveDocument.Fields.Add(Range:=Rng, Type:=wdFieldQuote, _
Text:="""" & Str & """", PreserveFormatting:=False)
End With
Set Fld = Nothing: Set Rng = Nothing
Application.ScreenUpdating = True
End Sub
-
Oct 5th, 2012, 01:32 AM
#23
Thread Starter
Junior Member
Re: Popup in office (Macro)
I think I needed a night sleep. Solved the problem the first thing this morning 
The "error" were that Different firstpage should be active, and it wasnt before. Happy!
But... The scipt tells only the foot on the first page. If I would like to add that the other pages should have one "standard" foot, how should I do that?
= You open the document, popup shown and you choose office, it change only on the first page, and on the other pages it should be a standard foot.
Last edited by KenZu; Oct 5th, 2012 at 01:48 AM.
-
Oct 5th, 2012, 05:25 AM
#24
Re: Popup in office (Macro)
If I would like to add that the other pages should have one "standard" foot, how should I do that?
your script should only change the first page footer, if you want to create a footer for the other pages, try wdHeaderFooterPrimary
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
-
Oct 5th, 2012, 07:40 AM
#25
Thread Starter
Junior Member
Re: Popup in office (Macro)
It worked on the first page.
But if I want to decide both first and the other pages in the script? The first will be as the code above, then for the other pages, I need to write a code for that?
-
Oct 5th, 2012, 03:57 PM
#26
Re: Popup in office (Macro)
I need to write a code for that?
of course
Code:
With ActiveDocument.Sections(1).Footers(wdHeaderFooterFirstPage)
'your code as above
end with
With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary)
'your new code for other pages
end with
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
-
Oct 8th, 2012, 07:38 AM
#27
Thread Starter
Junior Member
Re: Popup in office (Macro)
Thanks a lot westconn1..
Sorry if Im stupid. But the first code have a lot of "text" (below).... What would i write/add if i want the other pages to have "Text" as foot?
Code:
With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary)
'text?
'text?
end with
Code:
With ActiveDocument.Sections(1).Footers(wdHeaderFooterFirstPage)
Set Rng = .Range.Characters.First
Rng.Collapse wdCollapseStart
For Each Fld In .Range.Fields
With Fld
If .Type = wdFieldQuote Then
Set Rng = Fld.Result
.Delete
Exit For
End If
End With
Next
Select Case i
Case 1
Str = "Göteborg"
Case 2
Str = "Sthlm"
Case 3
Str = "Stockholm"
End Select
Set Fld = ActiveDocument.Fields.Add(Range:=Rng, Type:=wdFieldQuote, _
Text:="""" & Str & """", PreserveFormatting:=False)
Last edited by KenZu; Oct 8th, 2012 at 08:00 AM.
-
Oct 8th, 2012, 03:23 PM
#28
Re: Popup in office (Macro)
try
Code:
With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary)
.Range.Text = "Text"
End With
it really depends what you want to put in the footer
if you want pagenumber, you have to insert the appropriate field
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
-
Oct 9th, 2012, 07:01 AM
#29
Thread Starter
Junior Member
Re: Popup in office (Macro)
Thanks mate! Have learned a lot from you!
Now, i think, it won't be anymore problems. I hope.
But... I tried to paste it in excel (in ThisWorkbook), and of cause, it didnt work. Do you have any clue how to do that?
-
Oct 10th, 2012, 05:18 AM
#30
Re: Popup in office (Macro)
this is a word macro so it will not work in excel, as is
if you want to do this from excel you should read the tutorial for automating office applications, in faq thread at the top of this forum
you would need to create an object of word then convert the code to work with the word object, all ranges etc would need to be fully qualified and any word constant used would have to be declared, or use literal values
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
-
Oct 10th, 2012, 06:49 AM
#31
Thread Starter
Junior Member
Re: Popup in office (Macro)
I thought so... Thanks for everything =)
-
Oct 17th, 2012, 12:49 AM
#32
Thread Starter
Junior Member
Re: Popup in office (Macro)
If i choose "Dont print hidden text" (in settings), then both head and footer won't print. But if I want footer to print but not the head. Is there any code for that?
-
Oct 17th, 2012, 06:10 AM
#33
Re: Popup in office (Macro)
temporally remove the header, or set its font to hidden, change back after printing
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
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
|