I am attempting to use your sample code for Inserting a Document During Replace (VB) shown at the bottom of this message.
I must declare and populate the MyDir variable in both the Sub InsertDocumentAtReplace() and the Class InsertDocumentAtReplaceHandler to get the code to work.
If I hard code the target folder information in both places, everything works fine.
Dim MyDir As String MyDir = "C:\inetpub\wwwroot\Documents\Florida"
If I build the MyDir variable from the querystring using Request,
Dim MyDir As String MyDir = "C:\inetpub\wwwroot\Documents" & Request("State") & ""
the sub works, but the class code fails and returns this server Compile Error Message:
BC30469: Reference to a non-shared member requires an object reference.
What must I do to get the Class InsertDocumentAtReplaceHandler to “see” the querystring? Is there a different way to pass this value to the class?
===============
Below is the “Insert a Document During Replace” Sample Code from Documentation
Public Sub InsertDocumentAtReplace()
Dim mainDoc As New Document(MyDir & "InsertDocument1.doc")
mainDoc.Range.Replace(New Regex("[MY_DOCUMENT]"), New InsertDocumentAtReplaceHandler(), False)
mainDoc.Save(MyDir & "InsertDocumentAtReplace Out.doc")
End Sub
Private Class InsertDocumentAtReplaceHandler
Implements IReplacingCallback
Private Function IReplacingCallback_Replacing(ByVal e As ReplacingArgs) As ReplaceAction Implements IReplacingCallback.Replacing
Dim subDoc As New Document(MyDir & "InsertDocument2.doc")
' Insert a document after the paragraph, containing the match text.
Dim para As Paragraph = CType(e.MatchNode.ParentNode, Paragraph)
InsertDocument(para, subDoc)
' Remove the paragraph with the match text.
para.Remove()
Return ReplaceAction.Skip
End Function
End Class
Hi Joseph,
Thanks for your inquiry. Please declare the MyDir variable as shown below. This solves your problem. Please try VB.net
samples (Examples/ExInsertDocument) from the offline samples pack for your reference. Let us know if you have any more queries.
Private Shared gMyDir As String Friend Shared Property MyDir() As String Get
Return gMyDir
End Get
Set(ByVal Value As String)
gMyDir = Value
End Set
End Property
Shared Sub New()
gMyDir = "C:\inetpub\wwwroot\Documents\"
End Sub
Public Sub InsertDocumentAtReplace()
Dim mainDoc As New Document(MyDir & "InsertDocument1.doc")
''' your code
End Sub
Private Class InsertDocumentAtReplaceHandler
Implements IReplacingCallback
Private Function IReplacingCallback_Replacing(ByVal e As ReplacingArgs) As ReplaceAction Implements IReplacingCallback.Replacing
Dim subDoc As New Document(MyDir & "InsertDocument2.doc")
''' your code
Return ReplaceAction.Skip
End Function
Your sample code works fine, but it misses my point.
I want to add a value to the MyDir variable from the query string. I am trying to retrieve, manipulate, and save Word documents to a subfolder off the /Documents folder represented by a state abbreviation (i.e., “/Documents/FL” or “/Documents/WA”) passed dynamically in the query string. It is not reasonable to write different code for each state.
I need to add a folder value at the end of the MyDir variable from the query string or from a cookie. Adding the code from the query string causes the code to break.
' This works fine.
gMyDir = "C:\inetpub\wwwroot\Documents"
' This does not work.
gMyDir = "C:\inetpub\wwwroot\Documents" & Request("State") & ""
When I add the query string information, I get this error:
Compiler Error Message: BC30369: Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.
Can you help me find a way to add the query string information to the MyDir variable?
I have include my code in the attached text file.
JH
I solved the problem.
I substituted HttpContext.Current.Request.QueryString(“State”) in the place of Request(“State”) in the class.
The class now recognizes the query string value.
Thanks for your help.
JH
Hi Joseph,
It is nice to hear from you that you have solved your problem. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.