Results 1 to 33 of 33

Thread: Popup in office (Macro)

Hybrid View

  1. #1
    Junior Member
    Join Date
    Jun 12
    Posts
    19

    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.

  2. #2
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,526

    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

  3. #3
    Junior Member
    Join Date
    Jun 12
    Posts
    19

    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.

  4. #4
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,526

    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:
    1. .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

  5. #5
    Junior Member
    Join Date
    Jun 12
    Posts
    19

    Re: Popup in office (Macro)

    What if I change to the specific document. How will the new code look like? (exampel)

  6. #6
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,526

    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

  7. #7
    Junior Member
    Join Date
    Jun 12
    Posts
    19

    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)

  8. #8
    Junior Member
    Join Date
    Jun 12
    Posts
    19

    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! =)

  9. #9
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,526

    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

  10. #10
    Junior Member
    Join Date
    Jun 12
    Posts
    19

    Re: Popup in office (Macro)

    Quote Originally Posted by westconn1 View Post
    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.

  11. #11
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,526

    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

  12. #12
    Junior Member
    Join Date
    Jun 12
    Posts
    19

    Re: Popup in office (Macro)

    Quote Originally Posted by westconn1 View Post
    .
    that makes sense, the document name may be some temp file
    Do you think I could go around the problem?

  13. #13
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,526

    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

  14. #14
    Junior Member
    Join Date
    Jun 12
    Posts
    19

    Re: Popup in office (Macro)

    How would you do/write?

  15. #15
    Junior Member
    Join Date
    Jun 12
    Posts
    19

    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!

  16. #16
    Junior Member
    Join Date
    Jun 12
    Posts
    19

    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.

  17. #17
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,526

    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

  18. #18
    Junior Member
    Join Date
    Jun 12
    Posts
    19

    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?

  19. #19
    New Member
    Join Date
    Aug 12
    Posts
    1

    Re: Popup in office (Macro)

    Every time I open the file I am presented with a securuty pop up that tells me .

  20. #20
    Junior Member
    Join Date
    Jun 12
    Posts
    19

    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?

  21. #21
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,526

    Re: Popup in office (Macro)

    Lets open this again...
    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

  22. #22
    Junior Member
    Join Date
    Jun 12
    Posts
    19

    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

  23. #23
    Junior Member
    Join Date
    Jun 12
    Posts
    19

    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.

  24. #24
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,526

    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

  25. #25
    Junior Member
    Join Date
    Jun 12
    Posts
    19

    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?

  26. #26
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,526

    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

  27. #27
    Junior Member
    Join Date
    Jun 12
    Posts
    19

    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.

  28. #28
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,526

    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

  29. #29
    Junior Member
    Join Date
    Jun 12
    Posts
    19

    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?

  30. #30
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,526

    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

  31. #31
    Junior Member
    Join Date
    Jun 12
    Posts
    19

    Re: Popup in office (Macro)

    I thought so... Thanks for everything =)

  32. #32
    Junior Member
    Join Date
    Jun 12
    Posts
    19

    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?

  33. #33
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,526

    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
  •