Log in

View Full Version : Solved: Error 4248 from file on intranet



KenZu
08-09-2012, 01:40 AM
Hello. Im new here and a newbie on VB.
Background: 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. in this case, three different offices.

Everything works just fine, if you open the file direct from the server. But if you try to open it from the intranet, it fails.
I get the popup to work, and after i chose office the error appear.

I've google and tried, but with no success.

Question: Can you help? :)


Code:

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, 4: Malmö"))
If i = 0 Or i > 4 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 = "Samtliga"
Case 2
Str = "Göteborg"
Case 3
Str = "Stockholm"
Case 4
Str = "Malmö"
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

Frosty
08-09-2012, 12:12 PM
You can use the VBA tags to make your code a little more readable.

It doesn't sound like you're troubleshooting your code, but rather the process by which Word is launched and then subsequently runs the code...

Unfortunately, the 4248 isn't a particularly helpful error message, as on my desktop it is simply "Application or object defined error"

1. Is Word already open when you launch it from the browser?
2. Which browser are you using to open from your intranet?
3. Do you have Word integrated with your browser (i.e., is the document opening *in* the browser or is it opening in a separate Word application window?)
4. Do you have the ability to see which line of code is causing the error? Obviously, if your input box is appearing, then you are at least triggering the code... so what code is failing? If you don't know which line is failing, then I would suggest commenting out all of the lines of code after the input box, and start uncommenting each one, one by one, to find out what line of code is actually failing.

That will start to point us in the right direction.

KenZu
08-10-2012, 05:47 AM
Don't know how to tag my code, sorry.
The error says: "Wrong nr '4248'... This command is not available because no document is open. "

1. Word is not open
2. I'm using IE 8
3. When you open the file it opens first in the browser, and then in word.
4. The error should be in the:
With ActiveDocument.Sections(1).Footers(wdHeaderFooterFirstPage)

I hope that help something.

Frosty
08-10-2012, 05:52 AM
You can tag the code using the VBA tags. There's a VBA button when you're writing a response.

Sounds like #3 is your problem. I'm not terribly familiar with IE8 settings, but do research on how to have the document open in Word directly rather than within the browser first.

KenZu
08-14-2012, 05:00 AM
Yeah. It must be something wrong with my settings in IE8.
Tried in FF and it work's perfectly :)
Guess, the easiest would be to tell everyone to install FF on their computers.

Ah, *push the button*

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, 4: Malmö"))
If i = 0 Or i > 4 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 = "Samtliga"
Case 2
Str = "Göteborg"
Case 3
Str = "Stockholm"
Case 4
Str = "Malmö"
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

KenZu
08-14-2012, 06:45 AM
Forgot.
What if we only want the possibility to save the document and not open. What would you change in the code?

Frosty
08-14-2012, 08:30 AM
Well, you have to open the document to do anything with it from Word VBA. I'm not sure how you're running your code (it looks like you're using built-in document events from this ThisDocument class).

You can change the .Visible property to False to simulate just working with the document in the background (although it will be open). I'm not sure what you'll need to hide, however... it may be the application, or it may just be the document window.

KenZu
08-22-2012, 02:56 AM
Guess I have to open it first.
Do you have any idea where to look for the settings in word or IE, If i want to open it directly in word. Like FF does.?

Frosty
08-22-2012, 06:37 AM
http://support.microsoft.com/kb/162059

KenZu
08-23-2012, 12:37 AM
I tried that, but didnt work. Same error as before.
It works in FF but not in IE. Do you think the setting problem is in word or browser?

Frosty
08-23-2012, 07:16 AM
It works fine in Firefox but not IE? I would guess that the setting is in IE. But I don't know. This really isn't a VBA question at this point, sorry.

KenZu
08-23-2012, 10:25 PM
Guess not. But thanks for the help :)
Case closed (or not)

KenZu
10-04-2012, 05:50 AM
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?

Frosty
10-04-2012, 09:09 AM
I think you're going to have to ask the question in a different way... the above thread ends up with "the code works in FireFox but not in IE" -- now you're saying copying the code to other documents doesn't work?

I think you need to re-post all of your code this time, including the Document_Close code.

fumei
10-04-2012, 03:48 PM
Yes, please post all relvant code.

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.

Copy? What copy?
Popup? What popup?
What do you mean by "change office"?

KenZu
10-04-2012, 10:25 PM
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 Documents("Brevmall utan rubrik_110101.doc").Sections(1).Footers(wdHeaderFooterFirstPage)
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

KenZu
10-04-2012, 11:32 PM
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.